Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear Interpolation to find coord in triangle

Suppose you have the following three points A, B, and C as shown in the following picture:

enter image description here

The points are always sorted according to their vertical offset, so the top most point is always A. Sometimes B and C could have the same y coordinate.

I'm trying to find the x coordinate for point D. I can find the Y coordinate for D by interpolating points A.y and C.y at (B.y / (C.y - A.y)). I'm doing my interpolation using the following formula (in C++)

float linearInterpolation(float a, float b, float t)
{
    return a + (t * (b - a));
}

So in other words, D.y = linearInterpolation(A.y, C.y, (B.y - A.y) / (C.y - A.y))

So to summarize, my question is: how do I find D.x?

Thanks

--

Answer:

Just to clarify, here's the solution that was suggested and worked:

D.x = A.x + (B.y - A.y) * (C.x - A.x) / (C.y - A.y);
D.y = B.y;

As illustrated in the image below:

enter image description here

like image 461
rodrigo-silveira Avatar asked Dec 28 '22 07:12

rodrigo-silveira


1 Answers

It it is the x coordinate that requires interpolation. The y coordinates of B and D are equal on your diagram.

D.x = A.x + (B.y - A.y) * (C.x - A.x) / (C.y - A.y);
D.y = B.y;

You should also make a provision for the case of C.y == A.y, where D.x could be anywhere between A.x and C.x. One way to do this is to not draw triangles, for which abs(C.y - A.y) < delta, with the delta being on the order of magnitude of 1 pixel.

like image 77
Don Reba Avatar answered Jan 09 '23 06:01

Don Reba