Skip to content

Commit 36c8fa2

Browse files
nsree0507Suvidhapre-commit-ci[bot]cclauss
authored
Add derivative(t) method to BezierCurve class (#13879)
* Add derivative(t) method to BezierCurve class * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updating DIRECTORY.md * Apply suggestion from @cclauss --------- Co-authored-by: Suvidha <nsuvidhasree05@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 3b5d7a6 commit 36c8fa2

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
* [Segment Tree](data_structures/binary_tree/segment_tree.py)
279279
* [Segment Tree Other](data_structures/binary_tree/segment_tree_other.py)
280280
* [Serialize Deserialize Binary Tree](data_structures/binary_tree/serialize_deserialize_binary_tree.py)
281+
* [Splay Tree](data_structures/binary_tree/splay_tree.py)
281282
* [Symmetric Tree](data_structures/binary_tree/symmetric_tree.py)
282283
* [Treap](data_structures/binary_tree/treap.py)
283284
* [Wavelet Tree](data_structures/binary_tree/wavelet_tree.py)

graphics/bezier_curve.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ 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+
if not 0 <= t <= 1:
82+
raise ValueError("Time t must be between 0 and 1.")
83+
84+
n = self.degree
85+
dx = 0.0
86+
dy = 0.0
87+
for i in range(n):
88+
coeff = comb(n - 1, i) * ((1 - t) ** (n - 1 - i)) * (t**i)
89+
delta_x = self.list_of_points[i + 1][0] - self.list_of_points[i][0]
90+
delta_y = self.list_of_points[i + 1][1] - self.list_of_points[i][1]
91+
dx += coeff * delta_x * n
92+
dy += coeff * delta_y * n
93+
return (dx, dy)
94+
7595
def plot_curve(self, step_size: float = 0.01):
7696
"""
7797
Plots the Bezier curve using matplotlib plotting capabilities.
@@ -112,3 +132,9 @@ def plot_curve(self, step_size: float = 0.01):
112132
BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1
113133
BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2
114134
BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3
135+
136+
# Test derivative method
137+
curve = BezierCurve([(0, 0), (5, 5), (5, 0)])
138+
print("Derivative at t=0.0:", curve.derivative(0.0))
139+
print("Derivative at t=0.5:", curve.derivative(0.5))
140+
print("Derivative at t=1.0:", curve.derivative(1.0))

0 commit comments

Comments
 (0)