#assume we start with the corner points ordered in order of drawing the 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 length? 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 polygon, do 'insidepoint' stuff or flag point as inside} else {flag point as outside} }