0

I have line segment with start s(x1,y1) and end e(x2,y2). I have calculated distance between s and e by using euclidean distance d = sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2)) How to find out point on the line segment at distance d1 (0 < d1< d)?

omkar1707
  • 75
  • 1
  • 6

2 Answers2

0

The main theme of linearity is that everything is proportional.

d1 is d1/d fraction of the way from 0 to d.

Therefore, the point, p, that you are looking for is the same fraction of the way from s to e. So let r = d1/d. Then

p = (x1 + r*(x2-x1), y1 + r*(y2-y1))

Notice that when r equals 0, p is (x1 + 0*(x2-x1), y1 + 0*(y2-y1)) = (x1, y1) = s. And when r equals 1, p is e = (x2, y2). As r goes from 0 to 1, p goes from s to t linearly -- that is, as a linear function of r.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
0

parametric Line is defined like this:

x(t)=x1+(x2-x1)*t;
y(t)=y1+(y2-y1)*t;
  • where t is parameter in range <0.0,1.0>
  • if t=0.0 then the result is giving point (x1,y1)
  • if t=1.0 then the result is giving point (x2,y2)

So if you need point at d distance from start then:

D=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
x(d)=x1+(x2-x1)*d/D;
y(d)=y1+(y2-y1)*d/D;
  • where D is line length
  • and d is distance from start point
Spektre
  • 49,595
  • 11
  • 110
  • 380