0
# Mathematical Objects
1
2
ManimGL's mathematical objects (Mobjects) are the building blocks of animations. They represent visual elements that can be positioned, styled, and animated. The library provides a comprehensive collection of geometric shapes, mathematical constructs, and specialized objects for mathematical visualization.
3
4
## Capabilities
5
6
### Base Mobject Classes
7
8
Foundation classes that provide core functionality for all mathematical objects.
9
10
```python { .api }
11
class Mobject:
12
def __init__(self, **kwargs):
13
"""
14
Base class for all mathematical objects.
15
16
Parameters:
17
- color: Color specification
18
- name: str, object identifier
19
"""
20
21
def shift(self, vector):
22
"""
23
Move object by specified vector.
24
25
Parameters:
26
- vector: np.array or list, displacement vector
27
28
Returns:
29
Mobject (self)
30
"""
31
32
def move_to(self, point_or_mobject):
33
"""
34
Move object to specified position.
35
36
Parameters:
37
- point_or_mobject: Position or another mobject to align with
38
39
Returns:
40
Mobject (self)
41
"""
42
43
def scale(self, factor, **kwargs):
44
"""
45
Scale object by specified factor.
46
47
Parameters:
48
- factor: float, scaling factor
49
- about_point: np.array, point to scale around (default: center)
50
- about_edge: np.array, edge to scale around
51
52
Returns:
53
Mobject (self)
54
"""
55
56
def rotate(self, angle, axis=OUT, **kwargs):
57
"""
58
Rotate object around specified axis.
59
60
Parameters:
61
- angle: float, rotation angle in radians
62
- axis: np.array, rotation axis (default: OUT for 2D rotation)
63
- about_point: np.array, point to rotate around
64
65
Returns:
66
Mobject (self)
67
"""
68
69
def flip(self, axis=RIGHT):
70
"""
71
Flip object across specified axis.
72
73
Parameters:
74
- axis: np.array, axis to flip across
75
76
Returns:
77
Mobject (self)
78
"""
79
80
def set_color(self, color):
81
"""
82
Set object color.
83
84
Parameters:
85
- color: Color specification
86
87
Returns:
88
Mobject (self)
89
"""
90
91
def copy(self):
92
"""
93
Create a copy of the object.
94
95
Returns:
96
Mobject (copy of self)
97
"""
98
99
class Group(Mobject):
100
def __init__(self, *mobjects, **kwargs):
101
"""
102
Container for grouping multiple mobjects.
103
104
Parameters:
105
- mobjects: Mobject instances to group
106
"""
107
108
def add(self, *mobjects):
109
"""
110
Add mobjects to the group.
111
112
Parameters:
113
- mobjects: Mobject instances to add
114
115
Returns:
116
Group (self)
117
"""
118
119
class Point(Mobject):
120
def __init__(self, location=ORIGIN, **kwargs):
121
"""
122
Single point in space.
123
124
Parameters:
125
- location: np.array, point coordinates
126
"""
127
```
128
129
### Vectorized Mobjects
130
131
Advanced mobjects with vectorized graphics capabilities for smooth curves and complex shapes.
132
133
```python { .api }
134
class VMobject(Mobject):
135
def __init__(self, **kwargs):
136
"""
137
Base vectorized mobject with bezier curve support.
138
139
Parameters:
140
- fill_color: Interior color
141
- fill_opacity: float, interior opacity (0-1)
142
- stroke_color: Border color
143
- stroke_width: float, border width
144
- stroke_opacity: float, border opacity (0-1)
145
"""
146
147
def set_points(self, points):
148
"""
149
Set the bezier curve points.
150
151
Parameters:
152
- points: np.array, array of control points
153
154
Returns:
155
VMobject (self)
156
"""
157
158
def set_fill(self, color=None, opacity=None):
159
"""
160
Set fill properties.
161
162
Parameters:
163
- color: Fill color
164
- opacity: float, fill opacity (0-1)
165
166
Returns:
167
VMobject (self)
168
"""
169
170
def set_stroke(self, color=None, width=None, opacity=None):
171
"""
172
Set stroke properties.
173
174
Parameters:
175
- color: Stroke color
176
- width: float, stroke width
177
- opacity: float, stroke opacity (0-1)
178
179
Returns:
180
VMobject (self)
181
"""
182
183
class VGroup(VMobject):
184
def __init__(self, *vmobjects, **kwargs):
185
"""
186
Group of vectorized mobjects.
187
188
Parameters:
189
- vmobjects: VMobject instances to group
190
"""
191
192
class VectorizedPoint(VMobject):
193
def __init__(self, location=ORIGIN, **kwargs):
194
"""
195
Vectorized point representation.
196
197
Parameters:
198
- location: np.array, point coordinates
199
"""
200
201
class DashedVMobject(VMobject):
202
def __init__(self, vmobject, **kwargs):
203
"""
204
Dashed version of any VMobject.
205
206
Parameters:
207
- vmobject: VMobject to make dashed
208
- num_dashes: int, number of dash segments
209
- dash_length: float, length of each dash
210
"""
211
```
212
213
### Basic Geometric Shapes
214
215
Fundamental geometric shapes including lines, circles, polygons, and curves.
216
217
```python { .api }
218
class Line(VMobject):
219
def __init__(self, start=LEFT, end=RIGHT, **kwargs):
220
"""
221
Straight line segment.
222
223
Parameters:
224
- start: np.array, starting point
225
- end: np.array, ending point
226
- path_arc: float, arc curvature (0 for straight line)
227
"""
228
229
def get_start(self):
230
"""Get line start point."""
231
232
def get_end(self):
233
"""Get line end point."""
234
235
def get_center(self):
236
"""Get line center point."""
237
238
def get_length(self):
239
"""Get line length."""
240
241
class DashedLine(Line):
242
def __init__(self, *args, **kwargs):
243
"""
244
Dashed line segment.
245
246
Parameters:
247
- dash_length: float, length of dashes
248
- dash_spacing: float, spacing between dashes
249
"""
250
251
class Arrow(Line):
252
def __init__(self, *args, **kwargs):
253
"""
254
Line with arrowhead.
255
256
Parameters:
257
- buff: float, buffer space from endpoints
258
- max_tip_length_to_length_ratio: float, tip size ratio
259
- max_stroke_width_to_length_ratio: float, stroke ratio
260
"""
261
262
class Vector(Arrow):
263
def __init__(self, direction=RIGHT, **kwargs):
264
"""
265
Vector arrow from origin.
266
267
Parameters:
268
- direction: np.array, vector direction and magnitude
269
"""
270
271
class DoubleArrow(Arrow):
272
def __init__(self, *args, **kwargs):
273
"""Line with arrowheads on both ends."""
274
275
class Arc(VMobject):
276
def __init__(self, start_angle=0, angle=TAU/4, **kwargs):
277
"""
278
Circular arc.
279
280
Parameters:
281
- start_angle: float, starting angle in radians
282
- angle: float, arc angle in radians
283
- radius: float, arc radius
284
- arc_center: np.array, center point
285
"""
286
287
class Circle(Arc):
288
def __init__(self, radius=None, **kwargs):
289
"""
290
Complete circle.
291
292
Parameters:
293
- radius: float, circle radius (default: 1.0)
294
- color: Circle color
295
"""
296
297
def get_radius(self):
298
"""Get circle radius."""
299
300
class Dot(Circle):
301
def __init__(self, point=ORIGIN, **kwargs):
302
"""
303
Point represented as small filled circle.
304
305
Parameters:
306
- point: np.array, dot location
307
- radius: float, dot size (default: 0.08)
308
- color: Dot color
309
"""
310
311
class SmallDot(Dot):
312
def __init__(self, point=ORIGIN, **kwargs):
313
"""Smaller dot with radius 0.04."""
314
315
class Ellipse(Circle):
316
def __init__(self, width=2, height=1, **kwargs):
317
"""
318
Elliptical shape.
319
320
Parameters:
321
- width: float, ellipse width
322
- height: float, ellipse height
323
"""
324
325
class Sector(Arc):
326
def __init__(self, **kwargs):
327
"""
328
Circular sector (pie slice).
329
330
Parameters:
331
- start_angle: float, sector start angle
332
- angle: float, sector angle
333
- outer_radius: float, sector radius
334
- inner_radius: float, inner radius (for annular sector)
335
"""
336
337
class Annulus(Circle):
338
def __init__(self, inner_radius=1, outer_radius=2, **kwargs):
339
"""
340
Ring shape (annulus).
341
342
Parameters:
343
- inner_radius: float, inner circle radius
344
- outer_radius: float, outer circle radius
345
"""
346
```
347
348
### Polygons and Complex Shapes
349
350
Multi-sided shapes and complex geometric constructs.
351
352
```python { .api }
353
class Polygon(VMobject):
354
def __init__(self, *vertices, **kwargs):
355
"""
356
Multi-sided polygon.
357
358
Parameters:
359
- vertices: np.array points defining polygon vertices
360
"""
361
362
def get_vertices(self):
363
"""Get polygon vertices."""
364
365
class RegularPolygon(Polygon):
366
def __init__(self, n=6, **kwargs):
367
"""
368
Regular polygon with n sides.
369
370
Parameters:
371
- n: int, number of sides
372
- radius: float, circumscribed radius
373
- start_angle: float, rotation angle
374
"""
375
376
class Triangle(RegularPolygon):
377
def __init__(self, **kwargs):
378
"""Equilateral triangle."""
379
380
class Rectangle(Polygon):
381
def __init__(self, width=4.0, height=2.0, **kwargs):
382
"""
383
Rectangle shape.
384
385
Parameters:
386
- width: float, rectangle width
387
- height: float, rectangle height
388
"""
389
390
def get_width(self):
391
"""Get rectangle width."""
392
393
def get_height(self):
394
"""Get rectangle height."""
395
396
class Square(Rectangle):
397
def __init__(self, side_length=2.0, **kwargs):
398
"""
399
Square shape.
400
401
Parameters:
402
- side_length: float, side length
403
"""
404
405
class RoundedRectangle(Rectangle):
406
def __init__(self, **kwargs):
407
"""
408
Rectangle with rounded corners.
409
410
Parameters:
411
- corner_radius: float, corner rounding radius
412
"""
413
```
414
415
### Special Geometric Objects
416
417
Specialized geometric constructs for mathematical demonstrations.
418
419
```python { .api }
420
class TangentLine(Line):
421
def __init__(self, vmob, alpha, length=1, **kwargs):
422
"""
423
Tangent line to a curve.
424
425
Parameters:
426
- vmob: VMobject, curve to find tangent to
427
- alpha: float, position along curve (0-1)
428
- length: float, tangent line length
429
"""
430
431
class Elbow(VMobject):
432
def __init__(self, width=0.2, angle=0, **kwargs):
433
"""
434
Right angle indicator.
435
436
Parameters:
437
- width: float, elbow size
438
- angle: float, elbow orientation
439
"""
440
441
class CubicBezier(VMobject):
442
def __init__(self, a0, h0, h1, a1, **kwargs):
443
"""
444
Cubic Bezier curve.
445
446
Parameters:
447
- a0: np.array, start point
448
- h0: np.array, first control point
449
- h1: np.array, second control point
450
- a1: np.array, end point
451
"""
452
453
class Polygon3D(VMobject):
454
def __init__(self, *vertices, **kwargs):
455
"""
456
3D polygon.
457
458
Parameters:
459
- vertices: 3D points defining polygon vertices
460
"""
461
462
class Sphere(Surface):
463
def __init__(self, radius=1, **kwargs):
464
"""
465
3D sphere.
466
467
Parameters:
468
- radius: float, sphere radius
469
- resolution: tuple, surface resolution (u, v)
470
"""
471
```
472
473
## Usage Examples
474
475
### Basic Shapes
476
477
```python
478
from manimgl import *
479
480
class ShapeExample(Scene):
481
def construct(self):
482
# Create basic shapes
483
circle = Circle(radius=1, color=BLUE)
484
square = Square(side_length=2, color=RED)
485
triangle = Triangle(color=GREEN)
486
487
# Position shapes
488
circle.shift(LEFT * 3)
489
square.shift(ORIGIN)
490
triangle.shift(RIGHT * 3)
491
492
# Add to scene
493
self.add(circle, square, triangle)
494
self.wait()
495
```
496
497
### Custom Polygon
498
499
```python
500
class PolygonExample(Scene):
501
def construct(self):
502
# Create custom polygon
503
vertices = [
504
np.array([0, 2, 0]),
505
np.array([-1, 0, 0]),
506
np.array([1, 0, 0]),
507
]
508
custom_shape = Polygon(*vertices, color=YELLOW)
509
510
self.add(custom_shape)
511
self.wait()
512
```
513
514
### Grouped Objects
515
516
```python
517
class GroupExample(Scene):
518
def construct(self):
519
# Create and group objects
520
circle = Circle(color=BLUE)
521
text = Text("Circle").next_to(circle, DOWN)
522
group = Group(circle, text)
523
524
# Transform the entire group
525
self.add(group)
526
self.play(group.animate.scale(2).shift(RIGHT))
527
self.wait()
528
```