in reply to Determing whether point falls inside or outside a complex polygon

I was going to say that the missing piece of information to be able to give you a relevant reply is: how is your polygon defined? There are lots of possibilities in many coordinate systems, but I bet you just have the x-y coordinates of the corners in the same coordinate system as your data points.

On that assumption, the very first post provides a very elegant method.

Here's how you might code it in Perl, in very basic style for clarity.

A further assumption is that you have already been told how to build the polygon from the corner coordinates - you know which dots are connected to which so you don't have to try every polygon possible for a given set of corner points.

#assume we start with the corner points ordered in order of drawing th +e polygon @x = (x1, x2, x3, ....); @y = (y1, y2, y3, ....); # determine the equations of each side for $i (0 .. $#@x-1) { $m[$i] = ($y[$i+1] - $y[$i])/($x[$i+1] - $x[$i]); $b[$i] = $y[$i] - $m[$i]*$x[$i]; } #test each point @data_x= (dx1, dx2, dx3, ...); @data_y= (dy1, dy2, dy3, ...); for $i (0 .. $#data_y) { #find intersection of the line y = $data_y[$i] with each polygon +side for $j (0 .. $#x-1) { if ($m[$j] != 0) { $x_intsxn = ($data_y[$i] - $b[$j])/$m[$j]; #does that intersection occur within the side's le +ngth? if (($x_intsxn > $x[$j] && $x_intsxn < $x[$j+1]) | +| ($x_intsxn > $x[$j] && $x_intsxn < $x[$j+1])) { #is this intersection to the right or left of + the point? if ($x_intsxn > $data_x[$i]) {$right++;} else {$left++;} } } } #test odd/even interesections if ($left % 2 && $right % 2) {odd number means point inside polyg +on, do 'insidepoint' stuff or flag point as inside} else {flag point as outside} }
  • Comment on Re: Determing whether point falls inside or outside a complex polygon
  • Download Code