I have tried to solve this problem for days. I am using the barycentric formula to find the height of a point within a triangle. Here is a link to the formula: https://stackoverflow.com/questions/36090269/finding-height-of-point-on-height-map-triangles. The problem I am having is that when I change just the x value of the point the height of the point is not changing. An image of the output is attached. The code that I am sharing is simplified.
Edit: Sorry about the spacing, I forgot.
Code demonstrating point's height not changing:
///////////////////////////////////
//these are the points of the triangle.
p3 = { 19,19, .0 };
p1 = { 18,20,.5 };
p2 = { 18,19, .0 };
pointx = 18.6;
pointy = 19.3;
answer = getheightbarrycentric(p1, p2, p3, pointx, pointy);
fprintf(stderr, "\n\n\ntriangle_point1_height: %f trianglepoint2_height: %f trianglepoint3_height: %f\n", p1.z, p2.z, p3.z);
fprintf(stderr, "1: pointx: %f pointy: %f height: %f\n", pointx, pointy, answer);
pointx = 18.7;
pointy = 19.3;
answer = getheightbarrycentric(p1, p2, p3, pointx, pointy);
//fprintf(stderr, "//2: triangle_point1_height: %f trianglepoint2_height: %f trianglepoint3_height: %f\n", p1.z, p2.z, p3.z);
fprintf(stderr, "2: pointx: %f pointy: %f height: %f\n", pointx, pointy, answer);
pointx = 18.7;
pointy = 19.2;
answer = getheightbarrycentric(p1, p2, p3, pointx, pointy);
//fprintf(stderr, "//3: triangle_point1_height: %f trianglepoint2_height: %f trianglepoint3_height: %f\n", p1.z, p2.z, p3.z);
fprintf(stderr, "3: pointx: %f pointy: %f height: %f\n\n", pointx, pointy, answer);
p3 = { 19,19, .1 };
p1 = { 18,20,.5 };
p2 = { 18,19, .1 };
pointx = 18.6;
pointy = 19.3;
answer = getheightbarrycentric(p1, p2, p3, pointx, pointy);
fprintf(stderr, "\n\n\ntriangle_point1_height: %f trianglepoint2_height: %f trianglepoint3_height: %f\n", p1.z, p2.z, p3.z);
fprintf(stderr, "1: pointx: %f pointy: %f height: %f\n", pointx, pointy, answer);
pointx = 18.7;
pointy = 19.3;
answer = getheightbarrycentric(p1, p2, p3, pointx, pointy);
//fprintf(stderr, "//2: triangle_point1_height: %f trianglepoint2_height: %f trianglepoint3_height: %f\n", p1.z, p2.z, p3.z);
fprintf(stderr, "2: pointx: %f pointy: %f height: %f\n", pointx, pointy, answer);
pointx = 18.7;
pointy = 19.2;
answer = getheightbarrycentric(p1, p2, p3, pointx, pointy);
//fprintf(stderr, "//3: triangle_point1_height: %f trianglepoint2_height: %f trianglepoint3_height: %f\n", p1.z, p2.z, p3.z);
fprintf(stderr, "3: pointx: %f pointy: %f height: %f\n\n", pointx, pointy, answer);
///////////////////////////
Code of the used barycentric formula:
float getheightbarrycentric(glm::vec3 p1, glm::vec3 p2, glm::vec3 p3, float pointx, float pointy)
{
float det = (((p2.y - p3.y)*(p1.x - p3.x)) + ((p3.x - p2.x)*(p1.y - p3.y)));
float lambda1 = (((p2.y - p3.y)*((pointx)-p3.x)) + ((p3.x - p2.x)*((pointy)-p3.y)));
lambda1 = (lambda1 / det);
float lambda2 = (((p3.y - p1.y)*((pointx)-p3.x)) + ((p1.x - p3.x)*((pointy)-p3.y)));
lambda2 = (lambda2 / det);
float lambda3 = (1 - lambda1 - lambda2);
float heightz = ((lambda1*p1.z) + (lambda2*p2.z) + (lambda3*p3.z));
return(heightz);
}
Also, I have shared an attached image of the triangle's coordinates and values. The circled numbers are the number of the points of the triangle (p1,p2,p3).
Please help me, I'm lost!
Josheir