diff options
author | Leonard Kugis <leonard@kug.is> | 2020-08-21 22:30:37 +0200 |
---|---|---|
committer | Leonard Kugis <leonard@kug.is> | 2020-08-21 22:30:37 +0200 |
commit | 10df6080647e5904a6c03b497586a554b1985a99 (patch) | |
tree | 44533fc8de9c28c5ef693a4b5ec263ea1db1d12f /AutopyExtended/Curve/CurveBezier.py | |
parent | 1932a860f78830bbff6d9c477409b5e0d5b32b9f (diff) |
Curves
Added Linear and Bezier Curves.
Diffstat (limited to 'AutopyExtended/Curve/CurveBezier.py')
-rw-r--r-- | AutopyExtended/Curve/CurveBezier.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/AutopyExtended/Curve/CurveBezier.py b/AutopyExtended/Curve/CurveBezier.py new file mode 100644 index 0000000..64d9539 --- /dev/null +++ b/AutopyExtended/Curve/CurveBezier.py @@ -0,0 +1,25 @@ +import numpy as np + +from Curve import Curve +from scipy.special import comb + +class CurveBezier(Curve): + def __init__(self, cp, tn): + self.__tn = tn + x = np.array([p[0] for p in cp]) + y = np.array([p[1] for p in cp]) + t = np.linspace(0, 1, tn) + poly = np.array([CurveBezier.bernstein(i, len(cp) - 1, t) for i in range(0, len(cp))]) + self.__x = np.dot(x, poly) + self.__y = 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 (self.__x[t], self.__y[t]) + + @staticmethod + def bernstein(i, n, t): + return comb(n, i) * (t ** (n - 1)) * (1 - t) ** i |