How to find a point between two Point coordinates at a particular distance from one of the two coordinates

Hi,

I am having two point coordinates like (x1,y1) & (x2,y2). I would like to find a point (x3,y3) at a distance 'D' from the point (x1,y1) in the line between (x1,y1) and (x2,y2).

your idea and solution with sample code is very much appreciated.

Thanks in advance.

Regards
Senthil

Rob Bachrach's picture

The solution becomes relatively simple when you cosider the parametric equation of a line:
(x,y) = (1-t)(x1,y1) + t(x2,y2)

1) Compute the length of your line:
#define sqr(x) ((x)*(x))
float len = sqrt(sqr(x2-x1)+sqr(y2-y1));

2) Get the fraction of your distance to the length
float frac = D/len;

3) Compute x3,y3 using this ratio
x3 = (1.0-frac)*x1 + frac*x2;
y3 = (1.0-frac)*y1 + frac*y2;

Rob