write a python script that visualizes bezier curves
- User
write a python script that visualizes bezier curves
- Assistant
Here is a sample Python script that visualizes Bezier curves using the matplotlib library:
import numpy as np import matplotlib.pyplot as plt
def bezier_curve(control_points, num_points=100): t = np.linspace(0, 1, num_points) curve = np.zeros((num_points, 2)) n = len(control_points) - 1 for i in range(num_points): t_i = t[i] curve[i, :] = (1 - t_i)(n) * control_points[0] +
np.sum([binom(n, j) * (1 - t_i)(n-j) * t_i**j * control_points[j] for j in range(1, n+1)], axis=0) return curvedef binom(n, k): return np.math.factorial(n) // (np.math.factorial(k) * np.math.factorial(n - k))
if name == "main": control_points = np.array([[0, 0], [0.5, 0.5], [1, 1]]) curve = bezier_curve(control_points) plt.plot(control_points[:, 0], control_points[:, 1], 'ro-', markersize=8) plt.plot(curve[:, 0], curve[:, 1], 'b-') plt.show()
This script creates a Bezier curve from a set of control points using the de Casteljau algorithm, which is a recursive method for evaluating Bezier curves. The curve is plotted along with the control points using matplotlib. You can modify the control points to create different shapes of Bezier curves.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.