Leser: 1
|< 1 2 3 >| | 26 Einträge, 3 Seiten |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
use strict; my @x = (0, 2, 4); my @y = (0, 2, 0); my $polygon = { x=>\@x, y=>\@y }; my $point1 = { x=>2, y=>1 }; my $point2 = { x=>5, y=>5 }; my $point3 = { x=>1, y=>1 }; sub min { return $_[0] < $_[1] ? $_[0] : $_[1]; } sub max { return $_[0] > $_[1] ? $_[0] : $_[1]; } sub isInside { # return values: -1 == draussen, 0 == auf Kante , 1 == innerhalb my $polygon = shift; my $point = shift; my $nPPoints = 0; return -1 if(($nPPoints = scalar(@{$polygon->{x}})) != scalar(@{$polygon->{y}})); return -1 unless $nPPoints; my $x = $point->{x}; my $y = $point->{y}; my $ax = $polygon->{x}->[$nPPoints-1]; my $ay = $polygon->{y}->[$nPPoints-1]; my $lx = $ax; my($bx, $by) = (0, 0); my($count, $ignore, $zx) = (0, 0, 0); for my $i (0..$nPPoints-1) { $bx = $polygon->{x}->[$i]; $by = $polygon->{y}->[$i]; if(!$ignore) { if($by == $y and $bx>=$x) { return 0 if $bx == $x; $ignore = 1; } else { if(($ay < $y) == ($y < $by)) { if($x < min($ax, $bx)) { $count++; } elsif($x > max($ax, $bx)) {} elsif($x < ($zx = (($bx-$ax)*($y-$ay)/($by-$ay))+$ax)) { $count++; } elsif($zx == $x) { return 0; } } $ax = $bx; $ay = $by; } } else { if($by == $y) { return 0 if((($lx < $x) == ($x<$bx)) or $bx == $x); } else { $count++ if(($ay < $y) == ($y < $by)); $ignore = 0; $ax = $bx; $ay = $by; } } $lx = $bx; } return ($count & 1) ? 1 : -1; } print "Point1: ", isInside($polygon, $point1),"\n"; print "Point2: ", isInside($polygon, $point2),"\n"; print "Point3: ", isInside($polygon, $point3),"\n";
|< 1 2 3 >| | 26 Einträge, 3 Seiten |