Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
* [Segment Tree](data_structures/binary_tree/segment_tree.py)
* [Segment Tree Other](data_structures/binary_tree/segment_tree_other.py)
* [Serialize Deserialize Binary Tree](data_structures/binary_tree/serialize_deserialize_binary_tree.py)
* [Splay Tree](data_structures/binary_tree/splay_tree.py)
* [Symmetric Tree](data_structures/binary_tree/symmetric_tree.py)
* [Treap](data_structures/binary_tree/treap.py)
* [Wavelet Tree](data_structures/binary_tree/wavelet_tree.py)
Expand Down
25 changes: 25 additions & 0 deletions graphics/bezier_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Comment thread
cclauss marked this conversation as resolved.
Outdated

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.
Expand Down Expand Up @@ -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))
Loading