Jump to content
  • Advertisement
Sign in to follow this  

mapping coordinates with barycentric coordinates

This topic is 2493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Following to this thread (http://www.gamedev.n...uv-coordinates/),

given a triangle ABC and a point P inside it, I'd like to find the coordinates of a point Q inside a DEF triangle, so that ABC are mapped to DEF (see the image).


I'm trying to do this using barycentric coordinates. I was quite sure that my calculations were correct, but the result don't match what expected.
The resulting Q point is not were it should be.

Here is the code I'm using to calculate barycentric coordinates:

// compute the area of a triangle using Heron's formula
float triarea(float a, float b, float c)
float s = (a + b + c)/2.0;
return sqrt(s*(s-a)*(s-b)*(s-c));

// compute the distance between two 2d points
float dist(float x0, float y0, float z0, float x1, float y1, float z1)
float a = x1 - x0;
float b = y1 - y0;
float c = z1 - z0;
return sqrt(a*a + b*b + c*c);

// compute the barycentric coordinates of a 3d point inside a triangle
// (x0, y0, z0) (x1, y1, z1) (x2, y2, z2) are the vertices of a triangle
// (vx, vy, vz) is a point inside the triangle
// u, v, w are the barycentric coordinates of (vx, vy, vz) in the triangle
void barycent(float x0, float y0, float z0, float x1, float y1, float z1, float x2, float y2, float z2,
float vx, float vy, float vz,
float *u, float *v, float *w)
// compute the area of the big triangle
float a = dist(x0, y0, z0, x1, y1, z1);
float b = dist(x1, y1, z1, x2, y2, z2);
float c = dist(x2, y2, z2, x0, y0, z0);
float totalarea = triarea(a, b, c);

// compute the distances from the outer vertices to the inner vertex
float length0 = dist(x0, y0, z0, vx, vy, vz);
float length1 = dist(x1, y1, z2, vx, vy, vz);
float length2 = dist(x2, y2, z2, vx, vy, vz);

// divide the area of each small triangle by the area of the big triangle
*u = triarea(b, length1, length2)/totalarea;
*v = triarea(c, length0, length2)/totalarea;
*w = triarea(a, length0, length1)/totalarea;

And here the main() where I run a test

float r1,r2,r3; // barycentric coordinates
Point3D A(0,0,0);
Point3D B(2,4,0);
Point3D C(4,2,0);
Point3D P(2,3,0);

barycent(A.x,A.y,A.z, B.x,B.y,B.z, C.x,C.y,C.z, P.x,P.y,P.z, &r1, &r2, &r3);

Point3D D(0,0,0);
Point3D E(3,2,0);
Point3D F(6,0,0);

Point3D Q(D.x*r1 + E.x*r1 + F.x*r1, D.y*r2 + E.y*r2 + F.y*r2, D.z*r3 + E.z*r3 + F.z*r3);

std::cout << "barycentric coordinates: " << r1 << "," << r2 << "," << r3 << std::endl;
std::cout << "Q: " << Q.x << "," << Q.y << "," << Q.z << std::endl;

After I calculate barycentric coordinates (r1,r2,r3), in order to calculate the Q point I multiply DEF coordinates by that barycentric coordinates.
I know it's an hassle but if you could help me out that would be wonderful. I really suck at math.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!