## [SOLVED] Math: Shortest distance from point to line (EDITED)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
XHH
Citizen
Posts: 85
Joined: Thu Jun 20, 2013 6:43 pm
Location: US
Contact:

### [SOLVED] Math: Shortest distance from point to line (EDITED)

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?  Last edited by XHH on Mon Jul 15, 2013 12:38 am, edited 2 times in total.
I like to draw and program raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: [Help] Math-related: Do two line segments intersect?

XHH
Citizen
Posts: 85
Joined: Thu Jun 20, 2013 6:43 pm
Location: US
Contact:

### 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 micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

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

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

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).
XHH
Citizen
Posts: 85
Joined: Thu Jun 20, 2013 6:43 pm
Location: US
Contact:

### Re: [Help] Math: Shortest distance from point to line (EDITE

micha wrote:If you need the distance of a points from a line segment, it has to be modiefied.
Can you post that too then?
(thanks)
I like to draw and program micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

### Re: [Help] Math: Shortest distance from point to line (EDITE

XHH wrote:Can you post that too then?
(thanks)
Sure. Disclaimer again: This is from the top of my head. Please test.

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
XHH
Citizen
Posts: 85
Joined: Thu Jun 20, 2013 6:43 pm
Location: US
Contact:

### 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 raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: [Help] Math: Shortest distance from point to line (EDITE

micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

### 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
Ref
Party member
Posts: 701
Joined: Wed May 02, 2012 11:05 pm

### 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?
Attachments closest_point.love
simple test