From 7f22f476a22da4abb11b10e87e2c57f1536b2ace Mon Sep 17 00:00:00 2001 From: Suvidha Date: Fri, 7 Nov 2025 12:17:52 +0530 Subject: [PATCH 1/2] Add derivative(t) method to BezierCurve class --- graphics/bezier_curve.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/graphics/bezier_curve.py b/graphics/bezier_curve.py index 6c7dcd4f06e7..33bfabf0a4a5 100644 --- a/graphics/bezier_curve.py +++ b/graphics/bezier_curve.py @@ -72,6 +72,25 @@ def bezier_curve_function(self, t: float) -> tuple[float, float]: y += basis_function[i] * self.list_of_points[i][1] return (x, y) + def derivative(self, t: float) -> tuple[float, float]: + """ + Computes the derivative (tangent vector) of the Bezier curve at time t. + t: parameter between 0 and 1 + Returns the (dx, dy) vector representing the direction of the curve at t. + """ + assert 0 <= t <= 1, "Time t must be between 0 and 1." + + n = self.degree + dx = 0.0 + dy = 0.0 + for i in range(n): + coeff = comb(n - 1, i) * ((1 - t) ** (n - 1 - i)) * (t ** i) + delta_x = self.list_of_points[i + 1][0] - self.list_of_points[i][0] + delta_y = self.list_of_points[i + 1][1] - self.list_of_points[i][1] + dx += coeff * delta_x * n + dy += coeff * delta_y * n + return (dx, dy) + def plot_curve(self, step_size: float = 0.01): """ Plots the Bezier curve using matplotlib plotting capabilities. @@ -112,3 +131,9 @@ def plot_curve(self, step_size: float = 0.01): BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1 BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2 BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3 + + # Test derivative method + curve = BezierCurve([(0, 0), (5, 5), (5, 0)]) + print("Derivative at t=0.0:", curve.derivative(0.0)) + print("Derivative at t=0.5:", curve.derivative(0.5)) + print("Derivative at t=1.0:", curve.derivative(1.0)) From 66d62fc24e62e06c46f6b85e3a3e0674c9a24063 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 7 Nov 2025 06:59:30 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphics/bezier_curve.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/graphics/bezier_curve.py b/graphics/bezier_curve.py index 33bfabf0a4a5..99cd55f35176 100644 --- a/graphics/bezier_curve.py +++ b/graphics/bezier_curve.py @@ -84,13 +84,13 @@ def derivative(self, t: float) -> tuple[float, float]: dx = 0.0 dy = 0.0 for i in range(n): - coeff = comb(n - 1, i) * ((1 - t) ** (n - 1 - i)) * (t ** i) + coeff = comb(n - 1, i) * ((1 - t) ** (n - 1 - i)) * (t**i) delta_x = self.list_of_points[i + 1][0] - self.list_of_points[i][0] delta_y = self.list_of_points[i + 1][1] - self.list_of_points[i][1] dx += coeff * delta_x * n dy += coeff * delta_y * n return (dx, dy) - + def plot_curve(self, step_size: float = 0.01): """ Plots the Bezier curve using matplotlib plotting capabilities. @@ -131,7 +131,7 @@ def plot_curve(self, step_size: float = 0.01): BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1 BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2 BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3 - + # Test derivative method curve = BezierCurve([(0, 0), (5, 5), (5, 0)]) print("Derivative at t=0.0:", curve.derivative(0.0))