-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2dc.py
More file actions
206 lines (177 loc) · 8.48 KB
/
Copy path2dc.py
File metadata and controls
206 lines (177 loc) · 8.48 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# 2D Cubic Bezier Curve
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backend_bases import MouseEvent
import matplotlib.animation as animation
class DraggablePlotExample(object):
def __init__(self):
self._figure, self._axes, self._line, self._bLine = None, None, None, None
self._dragging_point = None
self._xpoints = [10, 30, 80, 100]
self._ypoints = [10, 90, 80, 10]
self.CELLS = 100
self._animator = None
self._xBezier = None
self._yBezier = None
self._animt = True
self._init_plot()
def _init_plot(self):
self._figure = plt.figure("2D Cubic Bezier Curve")
self._ax1 = plt.subplot(111)
self._ax1.set_xlabel("2D Cubic Bezier Curve", fontsize=16)
axes = plt.subplot(1, 1, 1)
axes.set_xlim(0, 100)
axes.set_ylim(0, 100)
axes.grid(which="both")
self._axes = axes
self._figure.canvas.mpl_connect('button_press_event', self._on_click)
self._figure.canvas.mpl_connect('button_release_event', self._on_release)
self._figure.canvas.mpl_connect('motion_notify_event', self._on_motion)
plt.show()
self._update_plot()
# Binomial coefficients
def _Ni(self, n, i):
return np.math.factorial(n) / (np.math.factorial(i) *
np.math.factorial(n - i))
# Bernstein Basis polynomial
def _basisFunction(self, n, i, t):
J = np.array(self._Ni(n, i) * (t ** i) * (1 - t) ** (n - i))
return J
def _update_plot(self):
# Variable reset
nCPTS = np.size(self._xpoints, 0) # Total number of control points, should be 4
n = nCPTS - 1 # Total number of segments, 2 for 2d
i = 0 # Central point counter
t = np.linspace(0, 1, self.CELLS) # Parametrix variable
b = [] # Initialized empty matrix for Bernstein Basis polynomial
self._xBezier = np.zeros((1, self.CELLS))
self._yBezier = np.zeros((1, self.CELLS))
for k in range(0, nCPTS):
b.append(self._basisFunction(n, i, t))
# Bezier curve calculation
self._xBezier = self._basisFunction(n, i,
t) * self._xpoints[k] + self._xBezier
self._yBezier = self._basisFunction(n, i,
t) * self._ypoints[k] + self._yBezier
i += 1
clock = self._ax1.text(-12, 105, "")
eqtX = self._ax1.text(0, 105, "")
eqtY = self._ax1.text(0, 110, "")
if not self._line:
self._bLine, = self._ax1.plot(self._xBezier[0],
self._yBezier[0],
c="orange")
self._line, = self._axes.plot(self._xpoints,
self._ypoints,
linewidth=0.7,
c="blue",
marker="o",
markersize=6)
# Update current plot
else:
self._line.set_data(self._xpoints, self._ypoints)
self._bLine.set_data(self._xBezier[0], self._yBezier[0])
secondaryPoints, = self._ax1.plot([], [],
c="#06bfb6",
marker="o",
linewidth=0.7,
markersize=3)
tertiaryPoints, = self._ax1.plot([], [],
c="green",
marker="o",
linewidth=0.7,
markersize=3)
movingPoint, = self._ax1.plot([], [],
marker="o",
markersize=7,
color="red")
def animate(frame):
movingPointX = self._xBezier[0, frame]
movingPointY = self._yBezier[0, frame]
secondaryPointsX = [0, 0, 0]
secondaryPointsY = [0, 0, 0]
tertiaryPointsX = [0, 0]
tertiaryPointsY = [0, 0]
for i in range(0, nCPTS - 1):
secondaryPointsX[i] = (1 - (frame / 100)) * self._xpoints[i] + (
frame / 100) * self._xpoints[i + 1]
secondaryPointsY[i] = (1 - (frame / 100)) * self._ypoints[i] + (
frame / 100) * self._ypoints[i + 1]
for i in range(0, nCPTS - 2):
tertiaryPointsX[i] = (
(1 - (frame / 100)) ** 2) * self._xpoints[i] + 2 * (
1 - frame / 100) * (frame / 100) * self._xpoints[i + 1] + (
(frame / 100) ** 2) * self._xpoints[i + 2]
tertiaryPointsY[i] = (
(1 - (frame / 100)) ** 2) * self._ypoints[i] + 2 * (
1 - frame / 100) * (frame / 100) * self._ypoints[i + 1] + (
(frame / 100) ** 2) * self._ypoints[i + 2]
secondaryPoints.set_xdata(secondaryPointsX)
secondaryPoints.set_ydata(secondaryPointsY)
tertiaryPoints.set_xdata(tertiaryPointsX)
tertiaryPoints.set_ydata(tertiaryPointsY)
movingPoint.set_xdata([movingPointX])
movingPoint.set_ydata([movingPointY])
clockText = "t=" + str((frame / 100))
clock.set_text(clockText)
equationTextX = "x(t) = (1-t)³(" + str(round(self._xpoints[0], 2)) + ") + 3t(1-t)²(" + str(
round(self._xpoints[1], 2)) + ") + 3t²(1-t)(" + str(round(self._xpoints[2], 2)) + ") + t³(" + str(round(self._xpoints[3], 2)) + ")"
equationTextY = "y(t) = (1-t)³(" + str(round(self._ypoints[0], 2)) + ") + 3t(1-t)²(" + str(
round(self._ypoints[1], 2)) + ") + 3t²(1-t)(" + str(round(self._ypoints[2], 2)) + ") + t³(" + str(round(self._ypoints[3], 2)) + ")"
eqtX.set_text(equationTextX)
eqtY.set_text(equationTextY)
return secondaryPoints, tertiaryPoints, movingPoint, clock, eqtX, eqtY
if self._animt:
self._animator = animation.FuncAnimation(fig=self._figure,
func=animate,
frames=100,
interval=100)
self._animt = False
self._figure.canvas.draw()
def _add_point(self, event):
if isinstance(event, MouseEvent):
g, k = self._find_neighbor_point(event)
i = self._xpoints.index(g)
self._xpoints[i] = event.xdata
self._ypoints[i] = event.ydata
return event.xdata, event.ydata
def _remove_point(self, x, _):
if x in self._xpoints:
i = self._xpoints.index(x)
self._xpoints[i] = 0
self._ypoints[i] = 0
def _find_neighbor_point(self, event):
distance_threshold = 9.0
nearest_point = None
min_distance = math.sqrt(2 * (100 ** 2))
for i in range(4):
distance = math.hypot(event.xdata - self._xpoints[i],
event.ydata - self._ypoints[i])
if distance < min_distance:
min_distance = distance
nearest_point = (self._xpoints[i], self._ypoints[i])
if min_distance < distance_threshold:
return nearest_point
return None
def _on_click(self, event):
# left click
if event.button == 1 and event.inaxes in [self._axes]:
point = self._find_neighbor_point(event)
if point:
self._dragging_point = point
self._update_plot()
def _on_release(self, event):
if event.button == 1 and event.inaxes in [self._axes
] and self._dragging_point:
self._dragging_point = None
self._update_plot()
def _on_motion(self, event):
if not self._dragging_point:
return
if event.xdata is None or event.ydata is None:
return
self._dragging_point = self._add_point(event)
self._update_plot()
if __name__ == "__main__":
plot = DraggablePlotExample()