Linear Interpolation to find coord in triangle
Suppose you have the following three points A, B, and C as shown in the following picture:
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 coord.
I'm trying to find the x coordinate for point D. I can find the Y coordinate for D by interpolating points Ay and Cy at (By / (Cy - Ay)). 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, Dy = linearInterpolation(Ay, Cy, (By - Ay) / (Cy - Ay))
So to summarize, my question is: how do I find Dx?
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:
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 Cy == Ay, where Dx could be anywhere between Ax and Cx One way to do this is to not draw triangles, for which abs(Cy - Ay) < delta
, with the delta
being on the order of magnitude of 1 pixel.
D.y = B.y
delta_x = C.x - A.x
delta_y = C.y - A.y
dist_y = B.y - A.y
percent = dist_y / delta_y
D.x = A.x + percent * delta_x
The function for the line AC is y = mx + b
.
m = (Ay - Cy)/(Ax - Cx)
You can then substitute A in: Ay = Ax * m + b
b = Ay - Ax *m
You need to calculate x from y, so swap the function around.
mx = y -b
x = (y -b)/m
Those are three steps to find the x from the y along that side of a triangle. Note that you don't need to do any interpolation to find Dy. Simply, Dy = By
Note that you can probably optimize what I have just written into a smaller series of steps. I think its better to write easier to read code though.
链接地址: http://www.djcxy.com/p/30618.html上一篇: 通过基本矩阵校正未校准的相机
下一篇: 线性插值以找出三角形中的坐标