I have two line segments. All I have are the coordinates for these line segments. How can I find out if these two line segments intersect?
[SOLVED] Math: Shortest distance from point to line (EDITED)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[SOLVED] Math: Shortest distance from point to line (EDITED)
Last edited by XHH on Mon Jul 15, 2013 12:38 am, edited 2 times in total.
I like to draw and program
Re: [Help] Math-related: Do two line segments intersect?
Thank you sir! One more new question though. How do I find the shortest distance from a point to a line?
I like to draw and program
Re: [Help] Math: Shortest distance from point to line (EDITE
Here is some code that determines the distance of a points from a (infinite) line. If you need the distance of a points from a line segment, it has to be modiefied.
The point is given as (px,py) the lines is given by two points (x1,y1) and (x2,y2)
Disclaimer: Untested.
This snippet calculates the unit vector pointing along the line (vector of length one). Then this vector is rotated by 90°, which is achieved by swapping x- and y-component and multiplying one with -1, that is (dx,dy) -> (dy,-dx). This is the normal vector for the line. To determine the distance, then the scalar product with any vector from the line to the point p is calculated.
If you leave away the math.abs, then you get a distance with sign. This is useful, if you want to determine if a point crossed the line in one frame (compare the sign of the distance before and after the frame, if the sign is different, the line has been crossed).
The point is given as (px,py) the lines is given by two points (x1,y1) and (x2,y2)
Code: Select all
function distPointToLine(px,py,x1,y1,x2,y2)
local dx,dy = x1-x2,y1-y2
local length = math.sqrt(dx*dx+dy*dy)
dx,dy = dx/length,dy/length
return math.abs( dy*(px-x1) - dx*(py-y1))
end
This snippet calculates the unit vector pointing along the line (vector of length one). Then this vector is rotated by 90°, which is achieved by swapping x- and y-component and multiplying one with -1, that is (dx,dy) -> (dy,-dx). This is the normal vector for the line. To determine the distance, then the scalar product with any vector from the line to the point p is calculated.
If you leave away the math.abs, then you get a distance with sign. This is useful, if you want to determine if a point crossed the line in one frame (compare the sign of the distance before and after the frame, if the sign is different, the line has been crossed).
Check out my blog on gamedev
Re: [Help] Math: Shortest distance from point to line (EDITE
Can you post that too then?micha wrote:If you need the distance of a points from a line segment, it has to be modiefied.
(thanks)
I like to draw and program
Re: [Help] Math: Shortest distance from point to line (EDITE
Sure. Disclaimer again: This is from the top of my head. Please test.XHH wrote:Can you post that too then?
(thanks)
Code: Select all
function distPointToLine(px,py,x1,y1,x2,y2)
local dx,dy = x2-x1,y2-y1
local length = math.sqrt(dx*dx+dy*dy)
dx,dy = dx/length,dy/length
local posOnLine = dx*(px-x1) + dy*(px-y1)
if posOnline < 0 then
-- first end point is closest
dx,dy = px-x1,py-y1
return math.sqrt(dx*dx+dy*dy)
elseif posOnLine > length then
-- second end point is closest
dx,dy = px-x2,py-y2
return math.sqrt(dx*dx+dy*dy)
else
-- point is closest to some part in the middle of the line
return math.abs( dy*(px-x1) - dx*(py-y1))
end
end
Check out my blog on gamedev
Re: [Help] Math: Shortest distance from point to line (EDITE
Didn't completely work. Sometimes it gives me 1.0 as the distance.
I like to draw and program
Re: [Help] Math: Shortest distance from point to line (EDITE
I found a typo. This one seems to work:
Code: Select all
function distPointToLine(px,py,x1,y1,x2,y2)
local dx,dy = x2-x1,y2-y1
local length = math.sqrt(dx*dx+dy*dy)
dx,dy = dx/length,dy/length
local posOnLine = dx*(px-x1) + dy*(py-y1)
if posOnLine < 0 then
-- first end point is closest
dx,dy = px-x1,py-y1
return math.sqrt(dx*dx+dy*dy)
elseif posOnLine > length then
-- second end point is closest
dx,dy = px-x2,py-y2
return math.sqrt(dx*dx+dy*dy)
else
-- point is closest to some part in the middle of the line
return math.abs( dy*(px-x1) - dx*(py-y1))
end
end
Check out my blog on gamedev
Re: [Help] Math: Shortest distance from point to line (EDITE
Saw the typo and after correction seems to work for me.
Question:
Easy to find closest-point-on-line but pretty mess to get closest-point-on-line-segment.
Do you have a 'neat' way of doing this?
Question:
Easy to find closest-point-on-line but pretty mess to get closest-point-on-line-segment.
Do you have a 'neat' way of doing this?
- Attachments
-
- closest_point.love
- simple test
- (976 Bytes) Downloaded 151 times
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 193 guests