# Simplified Barycentric Code, Need a Mathematician.

## Recommended Posts

Posted (edited)

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).

Josheir

Edited by Josheir

##### Share on other sites

I'm not sure why you think your result is wrong. You have a triangle flat on it's side of least y, this means that the height increases only with y and is constant with x. As you see.

It might help to rotate your diagram for a conventional x-across, y-up.

##### Share on other sites
Posted (edited)
1 hour ago, Josheir said:

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.

You have height 2 + 3 equal in both test cases. Then the height remains constant if you move exactly horizontally or vertically.

Plus, likely you are confused about some convention issue, like the mapping 90 degrees rotated from your assumption.

Make all heights different, if it still does not seem to work as expected the convention can be made right with trial and error usually. (swapping x / y for example)

Edit: Irsuan has won the race it seems

Edited by JoeJ

## Create an account

Register a new account

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 12
• 18
• 15
• 9
• 9