Megan Darcy
Megan Darcy

Reputation: 582

how to convert a barycentric coordinate calculation in c++ to python?

I'm looking for help with converting this piece of code which is in c++ and was wondering how it would look like in python.

void Barycentric(Point p, Point a, Point b, Point c, float &u, float &v, float &w)
{
    Vector v0 = b - a, v1 = c - a, v2 = p - a;
    float d00 = Dot(v0, v0);
    float d01 = Dot(v0, v1);
    float d11 = Dot(v1, v1);
    float d20 = Dot(v2, v0);
    float d21 = Dot(v2, v1);
    float denom = d00 * d11 - d01 * d01;
    v = (d11 * d20 - d01 * d21) / denom;
    w = (d00 * d21 - d01 * d20) / denom;
    u = 1.0f - v - w;
}

I'm not sure about the conversion b/w the 2 languages yet as I am still new to python.

Appreciate any help! :)

Upvotes: 0

Views: 228

Answers (1)

sucksatnetworking
sucksatnetworking

Reputation: 92

with numpy you would get a very similar looking function.

import numpy as np
def Barycentric(p, a, b, c, u, v, w):
    v0 = b - a
    v1 = c - a
    v2 = p - a
    d00 = np.dot(v0, v0)
    d01 = np.dot(v0, v1)
    d11 = np.dot(v1, v1)
    d20 = np.dot(v2, v0)
    d21 = np.dot(v2, v1)
    denom = d00 * d11 - d01 * d01
    v = (d11 * d20 - d01 * d21) / denom
    w = (d00 * d21 - d01 * d20) / denom
    u = 1.0f - v - w

where p, a, b, and c are just numpy arrays of size of the number of dimensions of your coordinate system

Upvotes: 1

Related Questions