Skip to content

Commit 4de3595

Browse files
authored
Create bezierfunction.py
1 parent 9805047 commit 4de3595

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

bezierfunction.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
def bezier_curve(p0, p1, p2, p3, t):
5+
"""Compute the position on a cubic Bezier curve at parameter value t."""
6+
b = (1-t)**3 * p0 + 3*t*(1-t)**2 * p1 + 3*t**2*(1-t) * p2 + t**3 * p3
7+
return b
8+
9+
# Define the control points
10+
P0 = np.array([0, 0])
11+
P1 = np.array([0.25, 0.5])
12+
P2 = np.array([0.75, 0.5])
13+
P3 = np.array([1, 1])
14+
15+
# Evaluate the curve at different parameter values
16+
t = np.linspace(0, 1, 1000)
17+
curve = np.array([bezier_curve(P0, P1, P2, P3, i) for i in t])
18+
19+
# Add noise to the control points
20+
P1 += np.random.normal(scale=0.05, size=2)
21+
P2 += np.random.normal(scale=0.05, size=2)
22+
23+
# Evaluate the noisy curve
24+
noisy_curve = np.array([bezier_curve(P0, P1, P2, P3, i) for i in t])
25+
26+
# Plot the curves
27+
fig, ax = plt.subplots()
28+
ax.plot(curve[:,0], curve[:,1], label='Smooth')
29+
ax.plot(noisy_curve[:,0], noisy_curve[:,1], label='Noisy')
30+
ax.legend()
31+
plt.show()

0 commit comments

Comments
 (0)