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} }

In reply to Re: Determing whether point falls inside or outside a complex polygon by punch_card_don
in thread Determing whether point falls inside or outside a complex polygon by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.