Link to home
Start Free TrialLog in
Avatar of Cheney
Cheney

asked on

Finding a point of a line that is an exact distance from a point (2D)

Given a 2D line segment, L, defined by two points, p1 and p2, is it possible to find a point, p4, which is a given distance from a point in space, p3, such that p4 lies on L?

(See attachment for diagram)

I'll be coding this up in Java; bonus points for code.
geoprob.gif
Avatar of margajet24
margajet24
Flag of Singapore image

is the distance of X and Y provided? or a single value for the distance?
a lot of points can be produced if X and Y distances are not given
is p4 exactly in between p1 and p2?
Avatar of Infinity08
Yes, you just need to find the intersection between the line segment, and a circle with p3 as center point and the distance as radius.
Note that there could be two intersection points.
Or to make things a bit more explicit :

A line through two points p1(x1, y1) and p2(x2, y2) has this equation :

        y = y1 + ((y2 - y1)/(x2 - x1)) * (x - x1)

A circle with p3(x3, y3) as center and radius r has this equation :

        (x - x3)² + (y - y3)² = r²

These are two equations with two unknowns (x and y), which can be solved to find two (x,y) points.

Then you simply need to check if those points are on the line segment or not.

If you need further assistance, then I'll be available again in about an hour ;)
ASKER CERTIFIED SOLUTION
Avatar of NovaDenizen
NovaDenizen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cheney
Cheney

ASKER

That's the way I was heading put somehow ended up with (p2x - p1x)^2 = -(p2x - p1x)^2 which is obviously a mistake. 50 bonus points as promised for code. Thank you!