Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-072

Barycentric Coordinates 2D

Problem

Compute the barycentric coordinates [u, v, w] of point p with respect to triangle abc, so that p = u*a + v*b + w*c and u + v + w = 1.

Use v0 = b - a, v1 = c - a, v2 = p - a and the standard dot-product formulas. Return None if the triangle is degenerate.

Hint

Compute v and w from dot products, then u = 1 - v - w.

Test cases

case 1
in: [[0.5,0.5],[0,0],[1,0],[0,1]]
expected: [0,0.5,0.5]
case 2
in: [[0,0],[0,0],[1,0],[0,1]]
expected: [1,0,0]
case 3
in: [[1,1],[0,0],[1,0],[0,1]]
expected: [-1,1,1]
case 4
in: [[1,1],[0,0],[1,1],[2,2]]
expected: null
Starter codePython
def barycentric_2d(p, a, b, c):
    # Your code here
    pass

4 test cases · run your code against them in the editor