finding where does a line touches a polygon

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

finding where does a line touches a polygon

Post by Rishavs »

Hi

i have a polygon made up of x1, y1, x2, y2, ... xn, yn vertices.

there is a line
y = mx + a

which goes through the polygon. How can i find the points where the line touches the polygon?

I was planning on using the physics module and the Shape:testSegment function.
But it looks like its been deprecated.

Am i supposed to use the raycast function insetad?
I tried to go through it but was unsable to understand it.

Can someone help me with a snippet or alternate implementation?

EDIT: currently, i am breaking the polygon into individual lines and then doing a slope intersection check for each side.
Trebgarta
Prole
Posts: 33
Joined: Mon May 23, 2016 5:21 pm

Re: finding where does a line touches a polygon

Post by Trebgarta »

marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Re: finding where does a line touches a polygon

Post by marco.lizza »

For any pair of point defining a segment you can

1) deduce the line formula,
2) solve the simple linear system (which is, in fact, a closed-formula) to find the intersection point, then
3) test if the intersection point is between the point enclosing the segment.

You should iterate these steps for each segment. This, I guess, it's what you are currently doing.

As an optimization, you can implement a sweeping algorithm. How many polygons/segments you need to handle?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: finding where does a line touches a polygon

Post by ivan »

Rishavs, this is a classic problem in computational geometry.
First of all you need a good segment vs segment intersection test.
http://2dengine.com/doc/gs_intersection.html
To find the exact point of intersection you have to change the "segment_vs_segment" function:

Code: Select all

return t1*dx1 + x1, t1*dy1 + y1
Then you have to iterate all edges in the polygon and check each one.

Super-duper industrial strength algorithms partition the polygon vertices,
but for everyday purposes you should be fine with the link above.
User avatar
pgimeno
Party member
Posts: 3641
Joined: Sun Oct 18, 2015 2:58 pm

Re: finding where does a line touches a polygon

Post by pgimeno »

[wiki]Shape:rayCast[/wiki] seems to do what you want, but the API is indeed confusing. However, there's an example in the wiki that is quite clarifying.

In Box2D, shapes are independent of bodies, and bodies usually hold the position and rotation within the world. Therefore if you want to intersect a line (ray) with a shape, you need to specify the shape's position and rotation, because there's no associated body. That's what tx, ty, tr are. If you have a body associated, you can obtain that info from the body.

maxFraction seems to be a scale factor of the second point with respect to the first. The example uses 1 and that seems fine; that apparently means that the ray casting will end exactly at x2, y2.

childIndex seems to apply only to [wiki]ChainShape[/wiki]'s.

As for the results, it returns a normal vector, which you can ignore if you don't need it, and the fraction between your two points. The intersection point can be calculated as: x1 + (x2 - x1)*fraction, y1 + (y2 - y1)*fraction.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: finding where does a line touches a polygon

Post by Rishavs »

Thanks everyone.
My current implementation does works but I was wondering if using the Shape object will be much faster.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest