Intersection point

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Intersection point

Post by tal » Mon Oct 19, 2009 2:00 pm

Hello,

I want to find the intersection point beetween two polygons.
The "intersect" function does not return the real intersection point (coordonates) but returns the intersection beetween two areas (true or false).
PS : the polygons i want to compare are shaped by verticals or horizontals lines ( i don't use obliques lines)

Thanks

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

Post by dunbarx » Mon Oct 19, 2009 3:10 pm

I can think of a brute force solution, as long as you really have only vertical and horizontal lines in your polygon.

Get the points of your polygons (this is a function in Rev), and in a repeat loop, find every point in all the lines that comprise it. For example, if you have a square graphic from "10,10" to "20,20", you would find all the points along all four lines (for the left side of the square you would have the following: "10,10","10,11",10,12",...,"10,20").

Once you have this long list of points for each of the polygons, simply find if any points are in common among the two lists.

Craig Newman

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

Post by bn » Mon Oct 19, 2009 3:16 pm

Hi tal,

if the polygons are rects, defined by their topleft and bottomright then you could do it like this. Put the script into a button on a teststack with two of your graphics:

Code: Select all

on mouseUp
   if intersect(grc 1, grc 2) then
      put the rect of grc 1 into tfirstRect
      put the rect of grc 2 into tSecondRect
      if item 1 of tFirstRect > item 1 of tSecondRect then
         put item 1 of tFirstRect into item 1 of tCollect
      else
         put item 1 of tSecondRect into item 1 of tCollect
      end if
      if item 2 of tFirstRect > item 2 of tSecondRect then
         put item 2 of tFirstRect into item 2 of tCollect
      else
         put item 2 of tSecondRect into item 2 of tCollect
      end if
      if item 3 of tFirstRect < item 3 of tSecondRect then
         put item 3 of tFirstRect into item 3 of tCollect
      else
         put item 3 of tSecondRect into item 3 of tCollect
      end if
      if item 4 of tFirstRect < item 4 of tSecondRect then
         put item 4 of tFirstRect into item 4 of tCollect
      else
         put item 4 of tSecondRect into item 4 of tCollect
      end if
      reset the templateGraphic
      set the style of the templateGraphic to "rectangle"
      set the foregroundcolor of the templategraphic to "red"
      set the rect of the templategraphic to tCollect
      create graphic
      put "the rect of the intersection is " && tCollect
   end if
end mouseUp
If you use irregular polygons then this will not work. Please say so if that is the case.
regards
Bernd

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

Post by bn » Mon Oct 19, 2009 4:16 pm

hi,

this one works for true polygon graphics, the one made up of points, like Craig mentioned.
again two graphics, this time irregular polygons, a button with this script:

Code: Select all

on mouseUp
   if intersect(grc 1, grc 2) then
      
      put the rect of graphic 1 into tgraphicOneRect
      put the rect of graphic 2 into tgraphicTwoRect
      
      put the points of grc 1 into tfirstRect
      -- find the points that intersect with graphic 2
      repeat for each line aLine in tFirstRect
         if aLine is within tgraphicTwoRect then put aLine & return after tFirstCollect
      end repeat
      delete last char of tFirstCollect -- return

      -- just dump the intersecting points in tFirstCollect into the variable tFirstRect
      put tFirstCollect into tFirstRect
      
      put the points of grc 2 into tSecondRect
      -- find the points that intersect with graphic 1
      repeat for each line aLine in tSecondRect
         if alIne is within tgraphicOneRect then put aLine & return after tSecondCollect
      end repeat
      delete last char of tSecondCollect -- return

      -- just dump the intersecting points in tSecondCollect into the variable tSecondRect
      put tSecondCollect into tSecondRect
      
      -- now tFirstRect and tSecondRect are made up of points
      -- that intersect the other rect
      -- now we sort the points to find a rectangle that is the smallest 
      -- rect made up from those points
      
      -- we look at the x coordinates for the left of our rect
      sort numeric tFirstRect  by item 1 of each
      sort numeric tSecondRect by item 1 of each
      if item 1 of line 1 of tFirstRect < item 1 of line 1 of tSecondRect then
         put item 1 of line 1 of tFirstRect into item 1 of tCollect
      else
         put item 1 of line 1 of tSecondRect into item 1 of tCollect
      end if
      
      -- we look at the x coordinates for the right of our rect
      if item 1 of line -1 of tFirstRect > item 1 of line -1 of tSecondRect then
         put item 1 of line -1 of tFirstRect into item 3 of tCollect
      else
         put item 1 of line -1 of tSecondRect into item 3 of tCollect
      end if
      
      -- we look at the y coordinates for the top of our rect
      sort numeric tFirstRect  by item 2 of each
      sort numeric tSecondRect by item 2 of each
      if item 2 of line 1 of tFirstRect < item 2 of line 2 of tSecondRect then
         put item 2 of line 1 of tFirstRect into item 2 of tCollect
      else
         put item 2 of line 1 of tSecondRect into item 2 of tCollect
      end if
      
      -- we look at the y coordinates for the bottom of our rect
      if item 2 of line -1 of tFirstRect > item 2 of line -1 of tSecondRect then
         put item 2 of line -1 of tFirstRect into item 4 of tCollect
      else
         put item 2 of line -1 of tSecondRect into item 4 of tCollect
      end if
      
      -- rect in tCollect is now made up of the "left,top,right,bottom" coordinates of the smallest rect
      
      reset the templateGraphic
      set the style of the templateGraphic to "rectangle"
      set the foregroundcolor of the templategraphic to "red"
      set the rect of the templategraphic to tCollect
      create graphic
      put "the rect of the intersection is " && tCollect
   end if
end mouseUp
in my testing it is the smallest rect that is comprised of the two intersecting irregular polygons
regards
Bernd

tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Post by tal » Mon Oct 19, 2009 4:34 pm

thank you all,
bn it works with regular polygons, the rect of the intersection point is right, i have just to find which couple of value is my intersection point ( sometimes it is the two first numbers and other times the last numbers or the numbers in the middle).
I think it is not really difficult but if you have a clue to make it faster i am ok lol

Best regards

tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Post by tal » Mon Oct 19, 2009 4:58 pm

your first example works but the rect is not good in the second example, i am testing it, thank you it was very helpfull

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

Post by bn » Mon Oct 19, 2009 5:08 pm

tal,

you would have to tell us what kind of polygons you mean. If you are using rectangle graphics then the first example works. But rectangle graphics dont have "points", they only have a rectangle. Consequently the second example code will not work on them.
If you are working with irregular polygon graphics, or freehand graphics then they have points. You can see the points in the property inspector under basic properties. Then the second piece of code should work.

If you like you could explain a little how you make the vertical and horizontal lines/rectangles.
regards
Bernd

tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Post by tal » Tue Oct 20, 2009 9:08 am

Good morning,

In fact my graphic is a line which can have at most four segments ( verticals or horizontals segments) i want to find the intersection point(maybe many intersection points) beetween this two lines.

Thanks

tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Post by tal » Tue Oct 20, 2009 11:00 am

I try to send images to facilitate the understanding of my problem :
here a simple example (with 2 lines):
[img]C:\Documents%20and%20Settings\mtalai\Mes%20documents\Mes%20images\img1[/img]
and a complex one (with four lines)
[img]C:\Documents%20and%20Settings\mtalai\Mes%20documents\Mes%20images\img2[/img]

[/img]

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

Post by bn » Tue Oct 20, 2009 1:36 pm

tal,
images only work if you put them on a server and the forum software can retrieve it from there. If you want you can send me not the images but a sample stack with the two examples as graphics, then I look what I can do and hopefully understand better what you want.

Code: Select all

on mouseUp
   put "110,105,103,103,101,109,97,110,110,64,117,110,105,45,119,104,46,100,101" into temp
   repeat with i = 1 to the number of items of temp
      put numtochar(item i of temp) after tCollect
   end repeat
   put tCollect
end mouseUp
put this into a button and it gives you the address in the message box
regards
Bernd

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

Post by FourthWorld » Tue Oct 20, 2009 3:27 pm

While the intersect function tests the bounding rect of the polygon, the within function will test within the actual lines of the poly.

You can verify this with a new stack containing a polygon and a button, putting this in the button script and then dragging it around:

Code: Select all

local sIsDragging

on mouseDown
  put true into sIsDragging
end mouseDown

on mouseMove x,y
  if sIsDragging is true then
    set the loc of me to x,y
    put within(grc 1, the loc of me)
  end if
  pass mouseMove
end mouseMove

on mouseUp
  put empty into sIsDragging
end mouseUp

on mouseRelease
  put empty into sIsDragging
end mouseRelease
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Post by bn » Tue Oct 20, 2009 4:26 pm

thank you Richard,

that helps a lot. I had read about this on the list once but forgotten since.
regards
Bernd

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

Post by FourthWorld » Tue Oct 20, 2009 7:36 pm

It doesn't help that it seems unintuitive, at least to me. "Intersect" conjures up an image of greater precision in my mind than "within", but in usage their implementation is the opposite of that. Go figure.... :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Post by tal » Wed Oct 21, 2009 11:37 am

thank you bn, i solved my problem using the coordinates of segments to see if they have intersections, if i have another question i'll check you ;)

Best regards, see you

Post Reply