0

I wish to write a C code where I can find the position of a point on a line between two points in a 2D coordinate grid. The two points positions I know, as well as each of the points' distances to the new desired point. However, I do not wish to use trig functions as that proves to be more expensive than I would like. I have heard of a simple solution using 1D interpolation, but I am unfamiliar with this solution. Could someone shed light on this? Thanks!

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

1 Answers1

3

Assuming the following:

  • p1 is the first point and has coordinates (p1.x, p1.y);
  • p2 is the second point and has coordinates (p2.x, p2.y);
  • pi is the interpolated point on the line connecting p1 and p2;
  • d1i is the given distance from p1 to pi;
  • di2 is the given distance from pi to p2.

Let d12 be the distance from p1 to p2. Then: d12 = d1i + di2.

(Alternatively, d12 = sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y)) via the theorem of Pythagorus.)

Let ratio = d1i / d12.

Then:

  • pi.x = p1.x + ratio * (p2.x - p1.x)
  • pi.y = p1.y + ratio * (p2.y - p1.y)

This also works if d1i and di2 are given as signed distances, where a positive distance is interpreted as "in the direction from p1 to p2" and a negative distance is in the opposite direction.

Ian Abbott
  • 15,083
  • 19
  • 33