Last active
March 26, 2020 03:27
-
-
Save csprance/52da802dd5023e4cba8b62895da06fbf to your computer and use it in GitHub Desktop.
Given a set of control points create a bezier curve with n amount of points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def binomial(i, n): | |
"""Binomial coefficient""" | |
return math.factorial(n) / float( | |
math.factorial(i) * math.factorial(n - i)) | |
def bernstein(t, i, n): | |
"""Bernstein polynom""" | |
return binomial(i, n) * (t ** i) * ((1 - t) ** (n - i)) | |
def bezier(t, points): | |
"""Calculate coordinate of a point in the bezier curve""" | |
n = len(points) - 1 | |
x = y = z = 0 | |
for i, pos in enumerate(points): | |
bern = bernstein(t, i, n) | |
x += pos[0] * bern | |
y += pos[1] * bern | |
z += pos[2] * bern | |
return x, y, z | |
def bezier_curve_range(n, points): | |
"""Range of points in a curve bezier""" | |
for i in xrange(n): | |
t = i / float(n - 1) | |
yield bezier(t, points) | |
control_points = [(1, 0, 0), (0, 1, 0), (0, -1, 0)] | |
num_new_pts = 100 | |
bezier_pts = list(bezier_curve_range(num_new_pts, control_points)) | |
geo.createPoints(bezier_pts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment