Function, windingIntersect()

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
atmosk
Posts: 32
Joined: Wed Feb 27, 2013 5:50 pm

Function, windingIntersect()

Post by atmosk » Wed Sep 30, 2015 7:26 am

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
Last edited by atmosk on Thu Oct 01, 2015 9:42 am, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10057
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Function, windingIntersect()

Post by FourthWorld » Wed Sep 30, 2015 7:54 am

Nice . You do good work.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

atmosk
Posts: 32
Joined: Wed Feb 27, 2013 5:50 pm

Re: Function, windingIntersect()

Post by atmosk » Wed Sep 30, 2015 11:42 am

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Function, windingIntersect()

Post by dunbarx » Wed Sep 30, 2015 2:23 pm

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.
Attachments
WindingFunction.livecode.zip
(1.47 KiB) Downloaded 250 times
Last edited by dunbarx on Wed Sep 30, 2015 10:54 pm, edited 4 times in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Function, windingIntersect()

Post by bn » Wed Sep 30, 2015 6:27 pm

Craig,

I am afraid the stack is the wrong stack

Kind regards

Bernd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Function, windingIntersect()

Post by dunbarx » Wed Sep 30, 2015 8:30 pm

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
Attachments
WindingFunction.livecode.zip
(1.47 KiB) Downloaded 224 times
Last edited by dunbarx on Wed Sep 30, 2015 10:53 pm, edited 3 times in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Function, windingIntersect()

Post by bn » Wed Sep 30, 2015 8:50 pm

Craig,

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

Kind regards
Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Function, windingIntersect()

Post by bn » Wed Sep 30, 2015 9:33 pm

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

atmosk
Posts: 32
Joined: Wed Feb 27, 2013 5:50 pm

Re: Function, windingIntersect()

Post by atmosk » Thu Oct 01, 2015 9:47 am

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.)

Post Reply