Open the context
Choose the project folder and name the files or behavior involved.
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.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
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-bThe 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.