11

I know the start and end points on a line segment. For this example say that the line segment has a distance of 5. Now I want to know the point that has a distance of three away from the end point. Any idea how to do this with math?

Start Point (0,0) End Point (0,5)

Point I want to find (0,2)

Mel
  • 2,055
  • 2
  • 26
  • 45

1 Answers1

31

If your points are (x1, y1) and (x2, y2), and you want to find the point (x3, y3) that is n units away from point 2:

d = sqrt((x2-x1)^2 + (y2 - y1)^2) #distance
r = n / d #segment ratio

x3 = r * x2 + (1 - r) * x1 #find point that divides the segment
y3 = r * y2 + (1 - r) * y1 #into the ratio (1-r):r
Artelius
  • 48,337
  • 13
  • 89
  • 105
  • 1
    +1 - After hours and hours of trying to get this working I finally thought, "Hey, why not check SO." 30 seconds later I have a fully working solution. For my purposes I swapped the x's and y's in the x3,y3 calculation so that it would represent the distance from the starting point. – Peter Sep 11 '11 at 23:49