aboutsummaryrefslogtreecommitdiff
path: root/AutopyExtended/Curve
diff options
context:
space:
mode:
Diffstat (limited to 'AutopyExtended/Curve')
-rw-r--r--AutopyExtended/Curve/CurveBezier.py4
-rw-r--r--AutopyExtended/Curve/test.py55
-rw-r--r--AutopyExtended/Curve/test1.py9
3 files changed, 66 insertions, 2 deletions
diff --git a/AutopyExtended/Curve/CurveBezier.py b/AutopyExtended/Curve/CurveBezier.py
index 64d9539..421b93d 100644
--- a/AutopyExtended/Curve/CurveBezier.py
+++ b/AutopyExtended/Curve/CurveBezier.py
@@ -19,7 +19,7 @@ class CurveBezier(Curve):
if(t_target != self.__tn):
raise ValueError("t_target must be equal to tn.")
return (self.__x[t], self.__y[t])
-
+
@staticmethod
def bernstein(i, n, t):
- return comb(n, i) * (t ** (n - 1)) * (1 - t) ** i
+ return comb(n, i) * (t ** (n - i)) * (1 - t) ** i
diff --git a/AutopyExtended/Curve/test.py b/AutopyExtended/Curve/test.py
new file mode 100644
index 0000000..ab9ee6c
--- /dev/null
+++ b/AutopyExtended/Curve/test.py
@@ -0,0 +1,55 @@
+import numpy as np
+from scipy.special import comb
+
+def bernstein_poly(i, n, t):
+ """
+ The Bernstein polynomial of n, i as a function of t
+ """
+
+ return comb(n, i) * ( t**(n-i) ) * (1 - t)**i
+
+
+
+def bezier_curve(points, nTimes=1000):
+ """
+ Given a set of control points, return the
+ bezier curve defined by the control points.
+ points should be a list of lists, or list of tuples
+ such as [ [1,1],
+ [2,3],
+ [4,5], ..[Xn, Yn] ]
+ nTimes is the number of time steps, defaults to 1000
+ See http://processingjs.nihongoresources.com/bezierinfo/
+ """
+
+ nPoints = len(points)
+ xPoints = np.array([p[0] for p in points])
+ yPoints = np.array([p[1] for p in points])
+
+ t = np.linspace(0.0, 1.0, nTimes)
+
+ polynomial_array = np.array([ bernstein_poly(i, nPoints-1, t) for i in range(0, nPoints) ])
+
+ xvals = np.dot(xPoints, polynomial_array)
+ yvals = np.dot(yPoints, polynomial_array)
+
+ return xvals, yvals
+
+
+
+if __name__ == "__main__":
+ from matplotlib import pyplot as plt
+
+ nPoints = 4
+ points = np.random.rand(nPoints,2)*200
+ xpoints = [p[0] for p in points]
+ ypoints = [p[1] for p in points]
+
+ xvals, yvals = bezier_curve(points, nTimes=1000)
+ plt.plot(xvals, yvals)
+ plt.plot(xpoints, ypoints, "ro")
+ for nr in range(len(points)):
+ plt.text(points[nr][0], points[nr][1], nr)
+
+
+ plt.show()
diff --git a/AutopyExtended/Curve/test1.py b/AutopyExtended/Curve/test1.py
new file mode 100644
index 0000000..05f56d8
--- /dev/null
+++ b/AutopyExtended/Curve/test1.py
@@ -0,0 +1,9 @@
+import matplotlib.pyplot as plot
+from CurveBezier import CurveBezier
+
+curve = CurveBezier([(0,0),(53,178),(5,87),(140,75),(48,647),(37,49)],1000)
+points = [curve.point(t) for t in range(0,1000)]
+x = [p[0] for p in points]
+y = [p[1] for p in points]
+plot.scatter(x,y)
+plot.show()