Page 1 of 1

Function, windingIntersect()

Posted: Wed Sep 30, 2015 7:26 am
by atmosk
Straightforward winding number function for determining if a point is within the bounds of a regular or irregular polygon.
Needed it with a radial number selector thingaling and the basic intersect (and pointIntersect from it's answers) didn't mesh too well to my problem without 'dirty' fixes.
Anyway hope it helps someone.

Code: Select all

answer windingIntersect(mouseLoc(),the points of grc "polygonmess")

function windingIntersect p0, inPoints
   put 0 into winding
   split p0 by comma
   repeat with i = 1 to (the num of lines in inPoints-1)
      put line i of inPoints into p1
      put line i+1 of inPoints into p2
      split p1 by comma
      split p2 by comma
      if (p1[2] <= p0[2]) then
         if (p2[2] > p0[2]) then
            if ((((p2[1]-p1[1])*(p0[2]-p1[2]))-((p0[1]-p1[1])*(p2[2]-p1[2]))) > 0) then
               add 1 to winding
            end if
         end if
      else
         if (p2[2] <= p0[2]) then
            if ((((p2[1]-p1[1])*(p0[2]-p1[2]))-((p0[1]-p1[1])*(p2[2]-p1[2]))) < 0) then
               subtract 1 from winding
            end if
         end if
      end if
   end repeat
   if (winding <> 0) then
      return true
   else
      return false
   end if
end windingIntersect

Re: Function, windingIntersect()

Posted: Wed Sep 30, 2015 7:54 am
by FourthWorld
Nice . You do good work.

Re: Function, windingIntersect()

Posted: Wed Sep 30, 2015 11:42 am
by atmosk
FourthWorld wrote:Nice . You do good work.
Thanks :)

Btw, did a quick test and the speed seems to be okayish as well at 0.019ms per iteration with a 15 point polygon.

Re: Function, windingIntersect()

Posted: Wed Sep 30, 2015 2:23 pm
by dunbarx
Hi.

Very nice indeed. And it might be a great general tool for threads that have dealt with this inside/outside issue for years.

But there is a problem, which I have illustrated in the attached stack. I made an arbitrary freehand polygon, some line graphics, a button and a field. In the button script:

Code: Select all

on mouseUp
   repeat until the optionKey is down
      put windingIntersect(mouseLoc(),the points of grc "polygonmess") into fld 1
   end repeat
end mouseUp


function windingIntersect p0, inPoints
   put 0 into winding
   split p0 by comma
   repeat with i = 1 to (the num of lines in inPoints-1)
      put line i of inPoints into p1
      put line i+1 of inPoints into p2
      split p1 by comma
      split p2 by comma
      if (p1[2] <= p0[2]) then
         if (p2[2] > p0[2]) then
            if ((((p2[1]-p1[1])*(p0[2]-p1[2]))-((p0[1]-p1[1])*(p2[2]-p1[2]))) > 0) then
               add 1 to winding
            end if
         end if
      else
         if (p2[2] < p0[2]) then
            if ((((p2[1]-p1[1])*(p0[2]-p1[2]))-((p0[1]-p1[1])*(p2[2]-p1[2]))) < 0) then
               subtract 1 from winding
            end if
         end if
      end if
   end repeat
   if (winding <> 0) then
      return true
   else
      return false
   end if
end windingIntersect
This is just a running variation of your own code. Click the button and move the cursor anywhere on the "y" value of the line graphics. A "true" appears, though the cursor is outside the bounds of the freehand. The lines are at the same "y" value as certain intersections, but not others. I think there are only three.

Why this should be so I leave to you. Please do figure it out, as I would love to add this to my library of cool tools.

Craig Newman

EDIT. I changed the stack here as well.

Re: Function, windingIntersect()

Posted: Wed Sep 30, 2015 6:27 pm
by bn
Craig,

I am afraid the stack is the wrong stack

Kind regards

Bernd

Re: Function, windingIntersect()

Posted: Wed Sep 30, 2015 8:30 pm
by dunbarx
Bernd.

Sloppy. It is the right stack, but the right card is card # 2.

Anyway, here is a new .zip file with the right card right in front.

Craig

Re: Function, windingIntersect()

Posted: Wed Sep 30, 2015 8:50 pm
by bn
Craig,

thanks, you know I can not count up to 2 :)

Kind regards
Bernd

Re: Function, windingIntersect()

Posted: Wed Sep 30, 2015 9:33 pm
by bn
Craig,

I would have never found the "problem" since I only tested on mouseUp for all kinds of places.
Even with you continous testing it is hard to find, so Hats off

Kind regards
Bernd

Re: Function, windingIntersect()

Posted: Thu Oct 01, 2015 9:47 am
by atmosk
Yup, like a pro I wasn't checking if "<=", just "<" but, all good now.
Fixed it in the first post and should work properly now!

Thanks for pointing it out btw, probably would've stumbled across it eventually but, eventually would've arrived with a handful of complaints :)
Also found out how easy img/imagedata manipulation is in LC which was a very pleasant surprise.
Image
(top to bottom: original, failed and fixed.)