@@ -72,6 +72,25 @@ def bezier_curve_function(self, t: float) -> tuple[float, float]:
7272 y += basis_function [i ] * self .list_of_points [i ][1 ]
7373 return (x , y )
7474
75+ def derivative (self , t : float ) -> tuple [float , float ]:
76+ """
77+ Computes the derivative (tangent vector) of the Bezier curve at time t.
78+ t: parameter between 0 and 1
79+ Returns the (dx, dy) vector representing the direction of the curve at t.
80+ """
81+ assert 0 <= t <= 1 , "Time t must be between 0 and 1."
82+
83+ n = self .degree
84+ dx = 0.0
85+ dy = 0.0
86+ for i in range (n ):
87+ coeff = comb (n - 1 , i ) * ((1 - t ) ** (n - 1 - i )) * (t ** i )
88+ delta_x = self .list_of_points [i + 1 ][0 ] - self .list_of_points [i ][0 ]
89+ delta_y = self .list_of_points [i + 1 ][1 ] - self .list_of_points [i ][1 ]
90+ dx += coeff * delta_x * n
91+ dy += coeff * delta_y * n
92+ return (dx , dy )
93+
7594 def plot_curve (self , step_size : float = 0.01 ):
7695 """
7796 Plots the Bezier curve using matplotlib plotting capabilities.
@@ -112,3 +131,9 @@ def plot_curve(self, step_size: float = 0.01):
112131 BezierCurve ([(1 , 2 ), (3 , 5 )]).plot_curve () # degree 1
113132 BezierCurve ([(0 , 0 ), (5 , 5 ), (5 , 0 )]).plot_curve () # degree 2
114133 BezierCurve ([(0 , 0 ), (5 , 5 ), (5 , 0 ), (2.5 , - 2.5 )]).plot_curve () # degree 3
134+
135+ # Test derivative method
136+ curve = BezierCurve ([(0 , 0 ), (5 , 5 ), (5 , 0 )])
137+ print ("Derivative at t=0.0:" , curve .derivative (0.0 ))
138+ print ("Derivative at t=0.5:" , curve .derivative (0.5 ))
139+ print ("Derivative at t=1.0:" , curve .derivative (1.0 ))
0 commit comments