sub _pointIsInPolygon { my ($a_point, $n, $a_x, $a_y) = @_; # point coords my ($x, $y) = ($a_point->[0], $a_point->[1]); # poly coords # $n is the number of points in polygon. my @x = @$a_x; # Even indices: x-coordinates. my @y = @$a_y; # Odd indices: y-coordinates. my ($i, $j); # Indices. my $side = 0; # 0 = outside, 1 = inside. for ($i = 0, $j = $n - 1 ; $i < $n; $j = $i++) { if ( ( # If the y is between the (y-) borders ... (($y[$i] <= $y) && ($y < $y[$j])) || (($y[$j] <= $y) && ($y < $y[$i])) ) and # ...the (x,y) to infinity line crosses the edge # from the ith point to the jth point... ($x < ($x[$j] - $x[$i] ) * ($y - $y[$i]) / ($y[$j] - $y[$i]) + $x[$i] )) { $side = not $side; # Jump the fence. } } return $side ? 1 : 0; }