diff options
Diffstat (limited to 'AutopyExtended/Curve')
-rw-r--r-- | AutopyExtended/Curve/Bezier.py (renamed from AutopyExtended/Curve/CurveBezier.py) | 15 | ||||
-rw-r--r-- | AutopyExtended/Curve/Linear.py (renamed from AutopyExtended/Curve/CurveLinear.py) | 2 | ||||
-rw-r--r-- | AutopyExtended/Curve/__init__.py | 0 | ||||
-rw-r--r-- | AutopyExtended/Curve/test.py | 55 | ||||
-rw-r--r-- | AutopyExtended/Curve/test1.py | 9 |
5 files changed, 7 insertions, 74 deletions
diff --git a/AutopyExtended/Curve/CurveBezier.py b/AutopyExtended/Curve/Bezier.py index 4aedb9d..ab79ec0 100644 --- a/AutopyExtended/Curve/CurveBezier.py +++ b/AutopyExtended/Curve/Bezier.py @@ -1,6 +1,6 @@ import numpy as np -from Curve import Curve +from AutopyExtended.Curve.Curve import Curve from scipy.special import comb class CurveBezier(Curve): @@ -13,14 +13,11 @@ class CurveBezier(Curve): self.__x = np.flip(np.dot(x, poly)) self.__y = np.flip(np.dot(y, poly)) - def point(self, t, t_target=None): - if(t_target == None): - t_target = self.__tn - if(t_target != self.__tn): - raise ValueError("t_target must be equal to tn.") - return np.add((self.__x[int(np.floor(t))], self.__y[int(np.floor(t))]), - np.subtract((self.__x[int(np.ceil(t))], self.__y[int(np.ceil(t))]), - (self.__x[int(np.floor(t))], self.__y[int(np.floor(t))])) * (t - np.floor(t))) + def point(self, t, t_target): + t_scaled = min([t * self.__tn / t_target, self.__tn - 1]) + return np.add((self.__x[int(np.floor(t_scaled))], self.__y[int(np.floor(t_scaled))]), + np.subtract((self.__x[int(np.ceil(t_scaled))], self.__y[int(np.ceil(t_scaled))]), + (self.__x[int(np.floor(t_scaled))], self.__y[int(np.floor(t_scaled))])) * (t_scaled - np.floor(t_scaled))) @staticmethod def bernstein(i, n, t): diff --git a/AutopyExtended/Curve/CurveLinear.py b/AutopyExtended/Curve/Linear.py index bee70cc..f35c095 100644 --- a/AutopyExtended/Curve/CurveLinear.py +++ b/AutopyExtended/Curve/Linear.py @@ -1,5 +1,5 @@ import numpy as np -import Curve +from AutopyExtended.Curve.Curve import Curve class CurveLinear(Curve): def __init__(self, start, end): diff --git a/AutopyExtended/Curve/__init__.py b/AutopyExtended/Curve/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/AutopyExtended/Curve/__init__.py diff --git a/AutopyExtended/Curve/test.py b/AutopyExtended/Curve/test.py deleted file mode 100644 index ab9ee6c..0000000 --- a/AutopyExtended/Curve/test.py +++ /dev/null @@ -1,55 +0,0 @@ -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 deleted file mode 100644 index 05f56d8..0000000 --- a/AutopyExtended/Curve/test1.py +++ /dev/null @@ -1,9 +0,0 @@ -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() |