-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBezierDerivatives.py
66 lines (56 loc) · 2.05 KB
/
BezierDerivatives.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import numpy as np
import matplotlib.pyplot as plt
from BezierMatrix import MatrixBezier
from Bezier import BernsteinBasis
def BernsteinFirstDerivative(n, i ,t):
return (i - n * t) / (t * (1. - t)) * BernsteinBasis(n, i, t)
def BernsteinSecondDerivative(n, i, t):
numerator = pow(i - n * t, 2) - n * pow(t, 2) - i * (1.0 - 2 * t)
denominator = pow(t, 2) * pow(1 - t, 2)
return numerator / denominator * BernsteinBasis(n, i, t)
def BezierFirstDerivative(Points, T):
countPoints = np.size(Points, 0)
n = countPoints - 1
dimension = np.size(Points, 1)
rVal = []
for t in T:
evaluatedPoint = np.zeros(dimension)
for i in range(countPoints):
evaluatedPoint += Points[i] * BernsteinFirstDerivative(n, i, t)
rVal.append(evaluatedPoint)
return np.array(rVal)
def BezierSecondDerivative(Points, T):
countPoints = np.size(Points, 0)
n = countPoints - 1
dimension = np.size(Points, 1)
rVal = []
for t in T:
evaluatedPoint = np.zeros(dimension)
for i in range(countPoints):
evaluatedPoint += Points[i] * BernsteinSecondDerivative(n, i, t)
rVal.append(evaluatedPoint)
return np.array(rVal)
def BezierTangent(Points, t):
deriv = BezierFirstDerivative(Points, [t])[0]
point = MatrixBezier(Points, [t])[0]
norm = deriv / np.linalg.norm(deriv)
points = []
points.append(point + norm)
points.append(point - norm)
points = np.asarray(points)
return points
if __name__ == "__main__":
Points = np.array([[1., 1.], [2., 3.], [4., 3.], [3., 1.]])
T = np.arange(0, 1, 0.01)
Curve = MatrixBezier(Points, T)
Curve_deriv = BezierFirstDerivative(Points, T)
Curve_deriv2 = BezierSecondDerivative(Points, T)
Tangent = BezierTangent(Points, 0.35)
plt.plot(Curve_deriv[:, 0], Curve_deriv[:, 1])
plt.plot(Curve_deriv2[:, 0], Curve_deriv2[:, 1])
plt.plot(Points[:, 0], Points[:, 1])
plt.plot(Tangent[:, 0], Tangent[:, 1])
plt.plot(Curve[:, 0], Curve[:, 1])
plt.xlim(-15, 15)
plt.ylim(-15, 15)
plt.show()