Thu, 12/28/2006 - 07:08

Forums:

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

Thu, 12/28/2006 - 17:07

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