Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
path_curve_to_quadratic_bezier() function in Python Wand
The quadratic Bézier curve is a mathematical function used to create smooth, curved paths in 2D space. It's defined by three points: two endpoints and one control point that influences the curve's shape without the curve necessarily passing through it.
A quadratic Bézier curve uses the mathematical formula B(t) = (1-t)²P? + 2(1-t)tP? + t²P?, where P? is the start point, P? is the control point, P? is the end point, and t ranges from 0 to 1.
Mathematical Formula
The quadratic Bézier curve is calculated using this parametric equation ?
def quadratic_bezier(p0, p1, p2, t):
x = (1 - t) ** 2 * p0[0] + 2 * (1 - t) * t * p1[0] + t ** 2 * p2[0]
y = (1 - t) ** 2 * p0[1] + 2 * (1 - t) * t * p1[1] + t ** 2 * p2[1]
return x, y
Here p0, p1, and p2 represent the start point, control point, and end point respectively. The parameter t varies from 0 to 1.
Basic Quadratic Bézier Curve
Let's create a simple quadratic Bézier curve with three points ?
import matplotlib.pyplot as plt
import numpy as np
def quad_bezier(p0, p1, p2, num_points=50):
t = np.linspace(0, 1, num_points)
x = (1 - t) ** 2 * p0[0] + 2 * (1 - t) * t * p1[0] + t ** 2 * p2[0]
y = (1 - t) ** 2 * p0[1] + 2 * (1 - t) * t * p1[1] + t ** 2 * p2[1]
return x, y
# Define control points
start_point = (0, 3) # Starting point
control_point = (4, 2) # Control point
end_point = (1, 8) # Ending point
# Generate curve points
x, y = quad_bezier(start_point, control_point, end_point)
# Plot the curve
plt.figure(figsize=(8, 6))
plt.plot(x, y, 'b-', linewidth=2, label='Bézier Curve')
plt.scatter([start_point[0], control_point[0], end_point[0]],
[start_point[1], control_point[1], end_point[1]],
c='red', s=100, zorder=5)
plt.plot([start_point[0], control_point[0], end_point[0]],
[start_point[1], control_point[1], end_point[1]],
'r--', alpha=0.5, label='Control Lines')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Quadratic Bézier Curve')
plt.grid(True, alpha=0.3)
plt.legend()
plt.show()
Multiple Curves Comparison
Let's compare different quadratic Bézier curves by changing the control point position ?
import matplotlib.pyplot as plt
import numpy as np
def quad_bezier(p0, p1, p2, num_points=50):
t = np.linspace(0, 1, num_points)
x = (1 - t) ** 2 * p0[0] + 2 * (1 - t) * t * p1[0] + t ** 2 * p2[0]
y = (1 - t) ** 2 * p0[1] + 2 * (1 - t) * t * p1[1] + t ** 2 * p2[1]
return x, y
# Fixed start and end points
start = (0, 0)
end = (4, 0)
# Different control points
control_points = [(2, 3), (2, -3), (1, 2), (3, 2)]
colors = ['red', 'blue', 'green', 'purple']
plt.figure(figsize=(10, 6))
for i, control in enumerate(control_points):
x, y = quad_bezier(start, control, end)
plt.plot(x, y, color=colors[i], linewidth=2,
label=f'Control: {control}')
# Plot start and end points
plt.scatter([start[0], end[0]], [start[1], end[1]],
c='black', s=100, zorder=5)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Quadratic Bézier Curves with Different Control Points')
plt.grid(True, alpha=0.3)
plt.legend()
plt.axis('equal')
plt.show()
Interactive Curve Parameters
Here's how different parameter values affect the curve smoothness ?
import matplotlib.pyplot as plt
import numpy as np
def quad_bezier(p0, p1, p2, num_points=50):
t = np.linspace(0, 1, num_points)
x = (1 - t) ** 2 * p0[0] + 2 * (1 - t) * t * p1[0] + t ** 2 * p2[0]
y = (1 - t) ** 2 * p0[1] + 2 * (1 - t) * t * p1[1] + t ** 2 * p2[1]
return x, y
# Define points
p0 = (0, 0)
p1 = (2, 4)
p2 = (4, 0)
# Different resolution levels
resolutions = [5, 15, 50, 100]
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
axes = axes.flatten()
for i, res in enumerate(resolutions):
x, y = quad_bezier(p0, p1, p2, res)
axes[i].plot(x, y, 'b-o', linewidth=2, markersize=4)
axes[i].scatter([p0[0], p1[0], p2[0]], [p0[1], p1[1], p2[1]],
c='red', s=100)
axes[i].set_title(f'Resolution: {res} points')
axes[i].grid(True, alpha=0.3)
axes[i].set_xlabel('X')
axes[i].set_ylabel('Y')
plt.tight_layout()
plt.show()
Comparison of Curve Types
| Feature | Linear | Quadratic Bézier | Cubic Bézier |
|---|---|---|---|
| Control Points | 2 | 3 | 4 |
| Curve Complexity | Straight line | One curve | S-shaped curves |
| Mathematical Degree | 1st degree | 2nd degree | 3rd degree |
| Use Cases | Simple connections | Smooth arcs | Complex paths |
Conclusion
Quadratic Bézier curves provide an elegant way to create smooth, curved paths using just three control points. The control point influences the curve's shape without the curve necessarily passing through it, making it ideal for creating natural-looking arcs and transitions in graphics and animations.
---