Linear interpolation to find the coordinate in a triangle

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

enter image description here

Points are always sorted according to their vertical offset, so the highest point is always A. Sometimes B and C can have the same y coordinate.

I am trying to find the x coordinate for point D. I can find the Y coordinate for D by interpolating the points Ay and Cy at (By / (Cy - Ay)). I do my interpolation using the following formula (in C ++)

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

In other words, Dy = linearInterpolation (Ay, Cy, (By - Ay) / (Cy - Ay))

So, to summarize, my question is: how do I find Dx?

thanks

-

Answer:

To clarify, here is a solution that was proposed and worked:

 Dx = Ax + (By - Ay) * (Cx - Ax) / (Cy - Ay); Dy = By; 

As the picture below:

enter image description here

+4
source share
3 answers

This is the x coordinate requiring interpolation. The Y coordinates of B and D are equal in your diagram.

 Dx = Ax + (By - Ay) * (Cx - Ax) / (Cy - Ay); Dy = By; 

You should also make a position for the case Cy == Ay, where Dx can be anywhere between Ax and Cx. One way to do this is to not draw triangles for which abs(Cy - Ay) < delta , and delta is on the order of 1 pixel.

+7
source
 Dy = By delta_x = Cx - Ax delta_y = Cy - Ay dist_y = By - Ay percent = dist_y / delta_y Dx = Ax + percent * delta_x 
+2
source

Function for AC line y = mx + b .

m = (Ay - Cy) / (Ax - Cx)

Then you can replace A with: Ay = Ax * m + b

b = Ay - Ax * m

You need to compute x from y, so replace the function around.

mx = y -b

x = (y -b) / m

These are three steps to find x from y along this side of the triangle. Note that you do not need to do any interpolation to find Dy. Just Dy = By

Note that you can probably optimize what I just wrote into a smaller sequence of steps. I think it's better to write easier to read the code though.

+1
source

Source: https://habr.com/ru/post/1391344/


All Articles