MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / basis_function

Method basis_function

graphics/bezier_curve.py:25–47  ·  view source on GitHub ↗

The basis function determines the weight of each control point at time t. t: time value between 0 and 1 inclusive at which to evaluate the basis of the curve. returns the x, y values of basis function at time t >>> curve = BezierCurve([(1,1), (1,2

(self, t: float)

Source from the content-addressed store, hash-verified

23 self.degree = len(list_of_points) - 1
24
25 def basis_function(self, t: float) -> list[float]:
26 """
27 The basis function determines the weight of each control point at time t.
28 t: time value between 0 and 1 inclusive at which to evaluate the basis of
29 the curve.
30 returns the x, y values of basis function at time t
31
32 >>> curve = BezierCurve([(1,1), (1,2)])
33 >>> [float(x) for x in curve.basis_function(0)]
34 [1.0, 0.0]
35 >>> [float(x) for x in curve.basis_function(1)]
36 [0.0, 1.0]
37 """
38 assert 0 <= t <= 1, "Time t must be between 0 and 1."
39 output_values: list[float] = []
40 for i in range(len(self.list_of_points)):
41 # basis function for each i
42 output_values.append(
43 comb(self.degree, i) * ((1 - t) ** (self.degree - i)) * (t**i)
44 )
45 # the basis must sum up to 1 for it to produce a valid Bezier curve.
46 assert round(sum(output_values), 5) == 1
47 return output_values
48
49 def bezier_curve_function(self, t: float) -> tuple[float, float]:
50 """

Callers 1

bezier_curve_functionMethod · 0.95

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected