aboutsummaryrefslogtreecommitdiff
path: root/AutopyExtended/Curve/CurveBezier.py
blob: 4aedb9d6438e0c130d08333b5c0b1ad4697f7eb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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.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)))

	@staticmethod
	def bernstein(i, n, t):
		return comb(n, i) * (t ** (n - i)) * (1 - t) ** i