-
Notifications
You must be signed in to change notification settings - Fork 0
/
Composite Bezier curves
271 lines (205 loc) · 9.68 KB
/
Composite Bezier curves
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from manim import *
import numpy as np
class BezierCurve(Scene):
def construct(self):
p0 = np.array([-5, 1, 0])
p1 = np.array([-4.5, -1.5, 0])
p2 = np.array([-1, -2, 0])
p3 = np.array([-1, 0, 0])
p4 = np.array([0.5, 0, 0])
p5 = np.array([0.5, 2, 0])
p6 = np.array([5, 0, 0])
p7 = np.array([4, -2, 0])
#Desenam punctele
P0 = Dot(p0)
P1 = Dot(p1)
P2 = Dot(p2)
P3 = Dot(p3)
P4 = Dot(p4)
P5 = Dot(p5)
P6 = Dot(p6)
P7 = Dot(p7)
puncte_control = VGroup(P0, P1, P2, P3)
puncte_control2 = VGroup(P4, P5, P6, P7)
P0_label = MathTex("b_{10}", font_size=25).next_to(P0, RIGHT)
P1_label = MathTex("b_{11}", font_size=25).next_to(P1, RIGHT)
P2_label = MathTex("b_{12}", font_size=25).next_to(P2, DOWN)
P3_label = MathTex("b_{13}", font_size=25).next_to(P3, RIGHT)
P4_label = MathTex("b_{20}", font_size=25).next_to(P4, UP)
P5_label = MathTex("b_{21}", font_size=25).next_to(P5, RIGHT)
P6_label = MathTex("b_{22}", font_size=25).next_to(P6, RIGHT)
P7_label = MathTex("b_{23}", font_size=25).next_to(P7, RIGHT)
control_labels = VGroup(P0_label,P1_label, P2_label, P3_label)
control_labels2 = VGroup(P4_label, P5_label, P6_label, P7_label)
#Deseneaza liniile
line0 = Line(P0, P1, stroke_width=2)
line1 = Line(P1, P2, stroke_width=2)
line2 = Line(P2, P3, stroke_width=2)
line3 = Line(P3, P4, stroke_width=2)
line4 = Line(P4, P5, stroke_width=2)
line5 = Line(P5, P6, stroke_width=2)
lines = VGroup(line0, line1, line2)
lines2 = VGroup( line3,line4,line5)
# Define binomial and Bernstein polynomials
def combinari(n, i):
return np.math.factorial(n) // (np.math.factorial(i) * np.math.factorial(n-i))
def bernstein(i, t):
return combinari(3, i) * t**i * (1-t)**(3-i)
# Generate points along curve using Bernstein polynomials
num_points = 100
t_values = np.linspace(0, 1, num_points)
curve_points = np.zeros((num_points, 3))
curve_points2 = np.zeros((num_points, 3))
for i, t in enumerate(t_values):
curve_points[i] = bernstein(0, t)*p0 + bernstein(1, t)*p1 + bernstein(2, t)*p2 + bernstein(3, t)*p3
curve_points2[i] = bernstein(0, t)*p4 + bernstein(1, t)*p5 + bernstein(2, t)*p6 + bernstein(3, t)*p7
# Create curve
curve = VMobject()
curve.set_points_smoothly(curve_points)
curve.set_color(RED)
curve2 = VMobject()
curve2.set_points_smoothly(curve_points2)
curve2.set_color(ORANGE)
# Add curve and control points to scene
self.add(curve, curve2, puncte_control,puncte_control2, control_labels, control_labels2, lines, lines2)
###################################################################################################
# Define the updater functions
def update_lines(obj):
line0.put_start_and_end_on(P0.get_center(), P1.get_center())
line1.put_start_and_end_on(P1.get_center(), P2.get_center())
line2.put_start_and_end_on(P2.get_center(), P3.get_center())
def update_curve(obj):
new_points = np.zeros((num_points, 3))
for i, t in enumerate(t_values):
new_points[i] = (
bernstein(0, t) * P0.get_center()
+ bernstein(1, t) * P1.get_center()
+ bernstein(2, t) * P2.get_center()
+ bernstein(3, t) * P3.get_center()
)
obj.set_points_smoothly(new_points)
def update_labels(obj):
P0_label.next_to(P0.get_center(), RIGHT)
P1_label.next_to(P1.get_center(), LEFT)
P2_label.next_to(P2.get_center(), DOWN)
P3_label.next_to(P3.get_center(), LEFT)
# Add the updaters to the objects
lines.add_updater(update_lines)
curve.add_updater(update_curve)
control_labels.add_updater(update_labels)
########################################################################################
# Define the updater functions
def update_lines(obj):
line3.put_start_and_end_on(P4.get_center(), P5.get_center())
line4.put_start_and_end_on(P5.get_center(), P6.get_center())
line5.put_start_and_end_on(P6.get_center(), P7.get_center())
def update_curve(obj):
new_points = np.zeros((num_points, 3))
for i, t in enumerate(t_values):
new_points[i] = (
bernstein(0, t) * P4.get_center()
+ bernstein(1, t) * P5.get_center()
+ bernstein(2, t) * P6.get_center()
+ bernstein(3, t) * P7.get_center()
)
obj.set_points_smoothly(new_points)
def update_labels(obj):
P4_label.next_to(P4.get_center(), RIGHT)
P5_label.next_to(P5.get_center(), UP)
P6_label.next_to(P6.get_center(), UP)
P7_label.next_to(P7.get_center(), RIGHT)
# Add the updaters to the objects
lines2.add_updater(update_lines)
curve2.add_updater(update_curve)
control_labels2.add_updater(update_labels)
################################################################################
text = MathTex(r'\text{Racordarea a dou\u a curbe Be\' zier}')
text.to_edge(UP)
self.play(Write(text))
self.wait(4)
self.play(FadeOut(text))
text = MathTex(r'\text{Racordare de clasa }C^0, b_{13} = b_{20} ')
text.to_edge(UP)
self.play(Write(text))
self.wait(1)
self.play(P4.animate.move_to(P3),run_time=2)
p4 = p3
self.wait(2)
self.play(FadeOut(text))
text = MathTex(r'\text{Tangentele celor dou\u a curbe}')
text.to_edge(UP)
self.play(Write(text))
self.wait(1)
############################################
# Define the tangent line
tangent_line = Line(stroke_width=6, color = BLUE)
tangent_length = 2.5
# Define the ValueTracker for t
t = ValueTracker(0)
# Define the updater function for the tangent line
def update_tangent_line(obj):
t_val = t.get_value()
tangent_vector = (
3 * (1 - t_val) ** 2 * (p1 - p0)
+ 6 * (1 - t_val) * t_val * (p2 - p1)
+ 3 * t_val ** 2 * (p3 - p2)
)
tangent_vector /= np.linalg.norm(tangent_vector) # Normalize the tangent vector
tangent_vector *= tangent_length / 2 # Scale the tangent vector to half of the desired length
curve_point = curve.point_from_proportion(t_val)
tangent_line.put_start_and_end_on(curve_point - tangent_vector, curve_point + tangent_vector)
# Add the updater to the tangent line
tangent_line.add_updater(update_tangent_line)
# Add the tangent line to the scene
self.add(tangent_line)
# Animate the movement of t from 0 to 1
self.play(t.animate.set_value(1), run_time=3)
################################################################################
# Define the tangent line
tangent_line2 = Line(stroke_width=6, color = GREEN)
tangent_length = 2.5
# Define the ValueTracker for t
t2 = ValueTracker(1)
# Define the updater function for the tangent line
def update_tangent_line(obj):
t_val = t2.get_value()
tangent_vector = (
3 * (1 - t_val) ** 2 * (p5 - p4)
+ 6 * (1 - t_val) * t_val * (p6 - p5)
+ 3 * t_val ** 2 * (p7 - p6)
)
tangent_vector /= np.linalg.norm(tangent_vector) # Normalize the tangent vector
tangent_vector *= tangent_length / 2 # Scale the tangent vector to half of the desired length
curve_points2 = curve2.point_from_proportion(t_val)
tangent_line2.put_start_and_end_on(curve_points2 - tangent_vector, curve_points2 + tangent_vector)
# Add the updater to the tangent line
tangent_line2.add_updater(update_tangent_line)
def update_tangent_line(obj):
t_val = t2.get_value()
curve_point = curve2.point_from_proportion(t_val)
tangent_vector = curve2.derivative_function()(t_val)
tangent_length = 2 # Adjust the length of the tangent line
tangent_start = curve_point - tangent_length * tangent_vector
tangent_end = curve_point + tangent_length * tangent_vector
tangent_line2.put_start_and_end_on(tangent_start, tangent_end)
# Add the tangent line to the scene
self.add(tangent_line2)
# Animate the movement of t from 0 to 1
self.play(t2.animate.set_value(0), run_time=3)
self.play(FadeOut(text))
text = MathTex(r'\text{Racordare de clasa }C^1, b_{21} = b_{13} -b_{12}')
text.to_edge(UP)
self.play(Write(text))
self.wait(1)
self.play(P5.animate.move_to(np.array([-1, 2, 0])),run_time=2)
p5 = np.array([-1, 2, 0])
self.wait(2)
self.play(t2.animate.set_value(0), run_time=2)
self.play(FadeOut(text))
text = MathTex(r'\text{Racordare de clasa }C^2, b_{22} = b_{11} -4b_{12}+4b_{13}')
text.to_edge(UP)
self.play(Write(text))
self.wait(1)
self.play(P6.animate.move_to(np.array([-4.5, 6.5, 0])),run_time=2)
p6 = np.array([-4.5, 6.5, 0])
self.wait(2)