BBarySlate
Workflows

Coding workflows · Exact triangle barycentric coordinates

03
Built around your work

A concrete starting point

Compute exact barycentric weights from three integer vertices and an integer point with coordinate magnitudes at most 10000. Test a vertex and an outside point, rejecting collinear vertices and verifying that the weights reconstruct the point.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Exact triangle barycentric coordinates

A concrete starting point
from fractions import Fraction

def barycentric(vertices, point):
    vertices, point = tuple(tuple(v) for v in vertices), tuple(point)
    if len(vertices) != 3 or any(len(v) != 2 for v in vertices) or len(point) != 2:
        raise ValueError("triangle.shape")
    if any(type(c) is not int or abs(c) > 10000 for v in vertices + (point,) for c in v):
        raise ValueError("coordinate.range")
    (x0, y0), (x1, y1), (x2, y2) = vertices
    x, y = point
    den = (y1-y2)*(x0-x2) + (x2-x1)*(y0-y2)
    if den == 0:
        raise ValueError("triangle.degenerate")
    a = Fraction((y1-y2)*(x-x2) + (x2-x1)*(y-y2), den)
    b = Fraction((y2-y0)*(x-x2) + (x0-x2)*(y-y2), den)
    return a, b, 1-a-b

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗