Skip to content

Instantly share code, notes, and snippets.

@tikoyes94
Created December 1, 2021 00:05
Show Gist options
  • Save tikoyes94/02541e60f444359d9a914778983405d9 to your computer and use it in GitHub Desktop.
Save tikoyes94/02541e60f444359d9a914778983405d9 to your computer and use it in GitHub Desktop.
Bezier curve implementation via recursive method in python
# This is an implementation of bezier curves via recursive method.
import numpy as np
def bezier(t, points):
"""
Get the point at `t` for Bezier Curve with `points` control points.
@param t curve parameter. 0 <= t <= 1
@param points control points of Bezier Curve. Type is numpy array
@returns the value at `t` of Bezier Curve.
"""
if len(points) == 1:
return points[0]
return (1 - t) * bezier(t, points[:-1]) + t * bezier(t, points[1:])
# Usage
# Control points for Bezier Curve
pts = np.array([[3, 0], [1, 5], [5, 6], [2.5, 2.5], [1, 1]])
# get points for each i from Bezier curve.
values = [bezier(i, pts) for i in np.linspace(0, 1, 30)]
# visualize curve
from matplotlib import pyplot as plt
x, y = zip(*values)
f = plt.figure()
plt.gca().set_aspect('equal', adjustable='box')
plt.plot(x, y)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment