Location>code7788 >text

manim learning by doing - cubes and prisms

Popularity:381 ℃/2024-11-12 11:49:41

This article introduces two common objects used to create three-dimensional stereo in Manim:Cubecap (a poem)Prism

CubeWhen 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.

Prismis rectangular, inherited fromCubecollaboration withCubeCompared to that, it can go further and set different side lengths.

1. Main parameters

CubeThe 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

Prismtogether withCubeThe difference is that it doesn'tside_lengthparameter and replacing it with thedimensionsParameters.

Parameter name typology clarification
dimensions tuple[float, float, float] The length, width and height of a rectangle

Prism(used form a nominal expression)dimensionsparameterslength, width and heightWhen defined as the same value, it isCube

2. Main approaches

Cubecap (a poem)PrismThere 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 theManimCreates 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 theManimHandling 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)