Finding a value at a certain point (inside) a triangle
For example I have a triangle with vertices A
B
C
. Each of these vertices is associated with a certain value (we abstract from the type of this value-the main thing is that it is subject to linear interpolation): x
, y
, z
- for each of the vertices, respectively. There is also some point D
lying inside the triangle or on one side of the triangle. How can I find the value at the point D
as easily as possible (in fewer actions)?
1 answers
The equation of the plane through 3 points is Ax+By+С=z
(in such a form that it is easier to count), we get from here A,B,C from the solution of the system of three equations
A*x1+B*y1+C == z1
A*x2+B*y2+C == z2
A*x3+B*y3+C == z3
Well, then just substitute x,y
and get...
If I didn't make a mistake in my calculations -
double tri(double x, double y,
double x1, double y1, double z1,
double x2, double y2, double z2,
double x3, double y3, double z3)
{
double q = (x2-x3)*y1 - (x1-x3)*y2 + (x1-x2)*y3;
double A = (y3-y2)*z1 + (y1-y3)*z2 - (y1-y2)*z3;
double B = (x2-x3)*z1 + (x3-x1)*z2 + (x1-x2)*z3;
double C = (x3*y2-x2*y3)*z1 - (x3*y1-x1*y3)*z2 + (x2*y1-x1*y2)*z3;
return (A*x+B*y+C)/q;
}
Of course, if there are a lot of calculations, then you can calculate A,B,C,q
once and count the values for different points...
If q==0
is bad, three points on one straight line...
For specific sides (points on the sides), you can simplify - there is no need for a third point, just write the equation of a straight line is enough.