forked from 3b1b/manim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinate_systems.py
368 lines (312 loc) · 11.4 KB
/
coordinate_systems.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import numpy as np
import numbers
from manimlib.constants import *
from manimlib.mobject.functions import ParametricCurve
from manimlib.mobject.geometry import Arrow
from manimlib.mobject.geometry import Line
from manimlib.mobject.number_line import NumberLine
from manimlib.mobject.svg.tex_mobject import TexMobject
from manimlib.mobject.types.vectorized_mobject import VGroup
from manimlib.utils.config_ops import merge_dicts_recursively
from manimlib.utils.simple_functions import binary_search
from manimlib.utils.space_ops import angle_of_vector
# TODO: There should be much more code reuse between Axes, NumberPlane and GraphScene
class CoordinateSystem():
"""
Abstract class for Axes and NumberPlane
"""
CONFIG = {
"dimension": 2,
"x_range": [-8, 8, 1],
"y_range": [-4, 4, 1],
"width": None,
"height": None,
}
def coords_to_point(self, *coords):
raise Exception("Not implemented")
def point_to_coords(self, point):
raise Exception("Not implemented")
def c2p(self, *coords):
"""Abbreviation for coords_to_point"""
return self.coords_to_point(*coords)
def p2c(self, point):
"""Abbreviation for point_to_coords"""
return self.point_to_coords(point)
def get_axes(self):
raise Exception("Not implemented")
def get_axis(self, index):
return self.get_axes()[index]
def get_x_axis(self):
return self.get_axis(0)
def get_y_axis(self):
return self.get_axis(1)
def get_z_axis(self):
return self.get_axis(2)
def get_x_axis_label(self, label_tex, edge=RIGHT, direction=DL, **kwargs):
return self.get_axis_label(
label_tex, self.get_x_axis(),
edge, direction, **kwargs
)
def get_y_axis_label(self, label_tex, edge=UP, direction=DR, **kwargs):
return self.get_axis_label(
label_tex, self.get_y_axis(),
edge, direction, **kwargs
)
def get_axis_label(self, label_tex, axis, edge, direction, buff=MED_SMALL_BUFF):
label = TexMobject(label_tex)
label.next_to(
axis.get_edge_center(edge), direction,
buff=buff
)
label.shift_onto_screen(buff=MED_SMALL_BUFF)
return label
def get_axis_labels(self, x_label_tex="x", y_label_tex="y"):
self.axis_labels = VGroup(
self.get_x_axis_label(x_label_tex),
self.get_y_axis_label(y_label_tex),
)
return self.axis_labels
def get_graph(self, function, x_range=None, **kwargs):
if x_range is None:
x_range = self.x_range
graph = ParametricCurve(
lambda t: self.coords_to_point(t, function(t)),
t_range=x_range,
**kwargs
)
graph.underlying_function = function
return graph
def get_parametric_curve(self, function, **kwargs):
dim = self.dimension
graph = ParametricCurve(
lambda t: self.coords_to_point(*function(t)[:dim]),
**kwargs
)
graph.underlying_function = function
return graph
def input_to_graph_point(self, x, graph):
if hasattr(graph, "underlying_function"):
return self.coords_to_point(x, graph.underlying_function(x))
else:
alpha = binary_search(
function=lambda a: self.point_to_coords(
graph.point_from_proportion(a)
)[0],
target=x,
lower_bound=self.x_range[0],
upper_bound=self.x_range[1],
)
if alpha is not None:
return graph.point_from_proportion(alpha)
else:
return None
class Axes(VGroup, CoordinateSystem):
CONFIG = {
"axis_config": {
"include_tip": True,
},
"x_axis_config": {},
"y_axis_config": {
"line_to_number_direction": LEFT,
},
}
def __init__(self, x_range=None, y_range=None, **kwargs):
VGroup.__init__(self, **kwargs)
self.x_axis = self.create_axis(
x_range or self.x_range,
self.x_axis_config,
self.width,
)
self.y_axis = self.create_axis(
y_range or self.y_range,
self.y_axis_config,
self.height
)
self.y_axis.rotate(90 * DEGREES, about_point=ORIGIN)
# Add as a separate group in case various other
# mobjects are added to self, as for example in
# NumberPlane below
self.axes = VGroup(self.x_axis, self.y_axis)
self.add(*self.axes)
self.center()
def create_axis(self, range_terms, axis_config, length):
new_config = merge_dicts_recursively(self.axis_config, axis_config)
new_config["width"] = length
axis = NumberLine(range_terms, **new_config)
axis.shift(-axis.n2p(0))
return axis
def coords_to_point(self, *coords):
origin = self.x_axis.number_to_point(0)
result = np.array(origin)
for axis, coord in zip(self.get_axes(), coords):
result += (axis.number_to_point(coord) - origin)
return result
def point_to_coords(self, point):
return tuple([
axis.point_to_number(point)
for axis in self.get_axes()
])
def get_axes(self):
return self.axes
def add_coordinate_labels(self, x_values=None, y_values=None):
axes = self.get_axes()
self.coordinate_labels = VGroup()
for axis, values in zip(axes, [x_values, y_values]):
numbers = axis.add_numbers(values, excluding=[0])
self.coordinate_labels.add(numbers)
return self.coordinate_labels
class ThreeDAxes(Axes):
CONFIG = {
"dimension": 3,
"x_range": (-6, 6, 1),
"y_range": (-5, 5, 1),
"z_range": (-4, 4, 1),
"z_axis_config": {},
"z_normal": DOWN,
"depth": None,
"num_axis_pieces": 20,
"gloss": 0.5,
}
def __init__(self, x_range=None, y_range=None, z_range=None, **kwargs):
Axes.__init__(self, x_range, y_range, **kwargs)
z_axis = self.create_axis(
z_range or self.z_range,
self.z_axis_config,
self.depth,
)
z_axis.rotate(-PI / 2, UP, about_point=ORIGIN)
z_axis.rotate(
angle_of_vector(self.z_normal), OUT,
about_point=ORIGIN
)
z_axis.shift(self.x_axis.n2p(0))
self.axes.add(z_axis)
self.add(z_axis)
self.z_axis = z_axis
for axis in self.axes:
axis.insert_n_curves(self.num_axis_pieces - 1)
class NumberPlane(Axes):
CONFIG = {
"axis_config": {
"stroke_color": WHITE,
"stroke_width": 2,
"include_ticks": False,
"include_tip": False,
"line_to_number_buff": SMALL_BUFF,
"line_to_number_direction": DL,
"decimal_number_config": {
"height": 0.2,
}
},
"y_axis_config": {
"line_to_number_direction": DL,
},
"background_line_style": {
"stroke_color": BLUE_D,
"stroke_width": 2,
"stroke_opacity": 1,
},
# Defaults to a faded version of line_config
"faded_line_style": None,
"faded_line_ratio": 1,
"make_smooth_after_applying_functions": True,
}
def __init__(self, x_range=None, y_range=None, **kwargs):
super().__init__(x_range, y_range, **kwargs)
self.init_background_lines()
def init_background_lines(self):
if self.faded_line_style is None:
style = dict(self.background_line_style)
# For anything numerical, like stroke_width
# and stroke_opacity, chop it in half
for key in style:
if isinstance(style[key], numbers.Number):
style[key] *= 0.5
self.faded_line_style = style
self.background_lines, self.faded_lines = self.get_lines()
self.background_lines.set_style(**self.background_line_style)
self.faded_lines.set_style(**self.faded_line_style)
self.add_to_back(
self.faded_lines,
self.background_lines,
)
def get_lines(self):
x_axis = self.get_x_axis()
y_axis = self.get_y_axis()
x_lines1, x_lines2 = self.get_lines_parallel_to_axis(x_axis, y_axis)
y_lines1, y_lines2 = self.get_lines_parallel_to_axis(y_axis, x_axis)
lines1 = VGroup(*x_lines1, *y_lines1)
lines2 = VGroup(*x_lines2, *y_lines2)
return lines1, lines2
def get_lines_parallel_to_axis(self, axis1, axis2):
freq = axis1.x_step
ratio = self.faded_line_ratio
line = Line(axis1.get_start(), axis1.get_end())
dense_freq = (1 + ratio)
step = (1 / dense_freq) * freq
lines1 = VGroup()
lines2 = VGroup()
inputs = np.arange(axis2.x_min, axis2.x_max + step, step)
for i, x in enumerate(inputs):
new_line = line.copy()
new_line.shift(axis2.n2p(x) - axis2.n2p(0))
if i % (1 + ratio) == 0:
lines1.add(new_line)
else:
lines2.add(new_line)
return lines1, lines2
def get_x_unit_size(self):
return self.get_x_axis().get_unit_size()
def get_y_unit_size(self):
return self.get_x_axis().get_unit_size()
def get_axes(self):
return self.axes
def get_vector(self, coords, **kwargs):
kwargs["buff"] = 0
return Arrow(self.c2p(0, 0), self.c2p(*coords), **kwargs)
def prepare_for_nonlinear_transform(self, num_inserted_curves=50):
for mob in self.family_members_with_points():
num_curves = mob.get_num_curves()
if num_inserted_curves > num_curves:
mob.insert_n_curves(num_inserted_curves - num_curves)
return self
class ComplexPlane(NumberPlane):
CONFIG = {
"color": BLUE,
"line_frequency": 1,
}
def number_to_point(self, number):
number = complex(number)
return self.coords_to_point(number.real, number.imag)
def n2p(self, number):
return self.number_to_point(number)
def point_to_number(self, point):
x, y = self.point_to_coords(point)
return complex(x, y)
def p2n(self, point):
return self.point_to_number(point)
def get_default_coordinate_values(self):
x_numbers = self.get_x_axis().get_tick_range()[1:]
y_numbers = self.get_y_axis().get_tick_range()[1:]
y_numbers = [complex(0, y) for y in y_numbers if y != 0]
return [*x_numbers, *y_numbers]
def add_coordinate_labels(self, numbers=None, **kwargs):
if numbers is None:
numbers = self.get_default_coordinate_values()
self.coordinate_labels = VGroup()
for number in numbers:
z = complex(number)
if abs(z.imag) > abs(z.real):
axis = self.get_y_axis()
value = z.imag
kwargs = merge_dicts_recursively(
kwargs,
{"number_config": {"unit": "i"}},
)
else:
axis = self.get_x_axis()
value = z.real
number_mob = axis.get_number_mobject(value, **kwargs)
self.coordinate_labels.add(number_mob)
self.add(self.coordinate_labels)
return self