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!
Asked
Active
Viewed 1,447 times
0
-
The ratio between the distances is the same as ratio between horizontal distances and the ratio between the vertical distances. Pretty much sufficient to find everything. Simple geometry, not C or ptogramming. – Eugene Sh. Jun 27 '18 at 15:21
-
1`xmid = (x0*xmid_x0_distance + x1*xmid_x1_distance)/(xmid_x0_distance + xmid_x1_distance)` should do it. Same for `y`. – chux - Reinstate Monica Jun 27 '18 at 15:53
-
You might find [Bilinear interpolation](https://en.wikipedia.org/wiki/Bilinear_interpolation) in Wikipedia useful. – StaticBeagle Jun 27 '18 at 16:02
-
Are all three points on the same line? – Ian Abbott Jun 27 '18 at 16:05
1 Answers
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