This article introduces two common objects used to create three-dimensional stereo in Manim:Cube
cap (a poem)Prism
。
Cube
When creating animations, it can be used to demonstrate the concept of cubes in three-dimensional geometry, or to help understand geometric transformations in three-dimensional space through animation effects such as rotation and scaling.
Prism
is rectangular, inherited fromCube
collaboration withCube
Compared to that, it can go further and set different side lengths.
1. Main parameters
Cube
The main parameters are:
Parameter name | typology | clarification |
---|---|---|
side_length | float | length of a side of a cube |
fill_opacity | float | Transparency of cubes |
fill_color | Color | Cube Colors |
stroke_width | float | Set the width of the cube border |
Prism
together withCube
The difference is that it doesn'tside_length
parameter and replacing it with thedimensions
Parameters.
Parameter name | typology | clarification |
---|---|---|
dimensions | tuple[float, float, float] | The length, width and height of a rectangle |
Prism
(used form a nominal expression)dimensions
parameterslength, width and heightWhen defined as the same value, it isCube
。
2. Main approaches
Cube
cap (a poem)Prism
There is no method specific to the
for exampleshift
(panning).rotate
(rotation) andscale
(Zoom), etc.
3. Examples of use
This object is relatively simple to use, the following example demonstrates its basic use and operation.
3.1 Default display
This example shows how theManim
Creates and displays a cube (Cube
) and a rectangle (Prism
)。
# Create a cube
cube = Cube()
# Create a prism
prism = Prism()
3.2 Changing colors
In this example, a red cube and a blue rectangle are first created.
Then, the cube gradually changes to green, while the rectangle gradually changes to yellow.
In real-world animation, dynamic changes in shape properties can be better demonstrated by color transformations.
# Create a cube
cube = Cube()
cube2 = Cube(fill_color=RED)
# Create a prism
prism = Prism()
prism2 = Prism(fill_color=GREEN)
3.3 Movement and rotation
This example begins by showing a cube and a rectangle.
Next, have the cube move to the right while the rectangle moves to the left, then the cube rotates 45 degrees clockwise while the rectangle rotates 45 degrees counterclockwise.
This moving and rotating effect can vividly demonstrate geometric transformations in three-dimensional space.
# Create a cube
cube = Cube(fill_color=RED)
# Create a prism
prism = Prism(fill_color=GREEN)
# Move
(
(RIGHT),
(LEFT), (LEFT), (LEFT), (LEFT), (LEFT)
)
# Rotate
(
(PI / 4), (-PI / 4), (RIGHT), (LEFT), ) # Rotate (
(-PI / 4), )
)
3.4 Combined use
In this example, again, a cube and a rectangle are created first.
The two shapes are then combined into a single whole, and then the whole is moved up and rotated at a small angle.
This combination can be used to show how theManim
Handling and organizing multiple shapes in and how to animate them as a whole.
# Create a cube
cube = Cube(fill_color=RED)
# Create a prism
prism = Prism(fill_color=GREEN)
# Place the cube to the right of the prism
cube.next_to(prism, RIGHT)
# Combine them
vg = VGroup(cube, prism)
# Combine overall movement
((UP))
# Combine overall rotation
((PI / 2, axis=UP))
4. Annexes
The code in the article is just an extract of the key parts, the complete code is shared on a web disk (cube_prism.py
),
Download at.Full Code (Access code: 6872)