Manim
Provides a range of objects designed for 3D space, making it easier to create 3D math animations.
This post begins with the simplest of these point and line related objects, theDot3D
(points in three dimensions).Line3D
(lines in three dimensions) andArrow3D
(three-dimensional arrow).
-
Dot3D
Used to represent points in three-dimensional space, it is the basis for constructing other complex three-dimensional shapes, and it is suitable for labeling key points, positions, vector starting points, etc. -
Line3D
It is used to draw line segments in three-dimensional space and can represent vectors, paths, trajectories, etc. It is suitable for showing the trajectory of an object, the direction of a force, etc. -
Arrow3D
existLine3D
The arrowhead is added to the base of the line segment for explicitly indicating direction. It is suitable for representing vectors, directions of forces, velocities, etc.
1. Main parameters
Dot3D
The main parameters of the
Parameter name | typology | clarification |
---|---|---|
point | Position of the point, contains 3 values x, y, z. | |
radius | float | radius of a point |
color | Color | Dot color |
resolution | tuple[int, int] | Point resolution |
Dot3D
is essentially a sphere, so there isradius
parameter that controls the size of the point;
resolution
The parameter sets the resolution of the point of this sphere, the larger the value, the more rounded the point looks, generally do not need to set this parameter, the default value will be fine.
Line3D
The main parameters of the
Parameter name | typology | clarification |
---|---|---|
start | Coordinates of the starting point of the line segment, containing x, y, and z values. | |
end | The endpoint coordinates of the line segment, containing x, y, and z values. | |
thickness | float | Thickness of the line segment |
color | Color | Color of line segments |
Arrow3D
The main parameters of the
Parameter name | typology | clarification |
---|---|---|
start | Coordinates of the starting point of the arrow, containing x, y, and z values. | |
end | Coordinates of the end point of the arrow, containing x, y, and z values. | |
thickness | float | Thickness of the arrow body |
height | float | Height of arrow tip |
base_radius | float | Bottom radius of arrow tip |
color | Color | Color of arrows |
2. Main approaches
Dot3D
cap (a poem)Arrow3D
There are no special methods, just the usual Manim object scaling, panning and so on.
But.Line3D
There are several methods unique to yourself.
name (of a thing) | clarification |
---|---|
parallel_to | Create a line segment parallel to the given line segment |
perpendicular_to | Creates a line segment perpendicular to the given line segment |
pointify | Get the point that represents the center of the object |
3. Examples of use
Finally, it is better to demonstrate the important parameters and methods of the above objects through examples.
3.1. Position and color of Dot3D
In this example, we'll create aDot3D
Object.
The first point has a default radius and color; while for the second point, we will customize its radius and color.
By adjusting these parameters, we can clearly see the position and color difference between two points in 3D space.
# Use default parameters
dot1 = Dot3D(point=axes.c2p(1, 2, 3))
# Customize the radius and color
dot2 = Dot3D(
radius=0.2, color=RED, # Customize radius and color.
color=RED, )
)
3.2. Length and direction of Line3D
This example will show how to draw a line segment in 3D space using the Line3D object.
First set up a line segment based on the start and end points, and at the same time, adjust the thickness and color of the line segment to make it more eye-catching.
Then, through the functionparallel_to
cap (a poem)perpendicular_to
Plot the parallel (yellow) and perpendicular (red) lines of this segment, respectively.
# Specify the start and end points and the line thickness.
line = Line3D(
start=LEFT + DOWN * 2 + IN, end=RIGHT + UP * 2 + OUT * 2, line = Line3D(
end=RIGHT + UP * 2 + OUT * 2, thickness=0.02, line3D = Line3D(
thickness=0.02, color=BLUE,
color=BLUE, )
)
# Draw parallel lines to line
Line3D.parallel_to(line, color=YELLOW)
# Draw a perpendicular line to line
Line3D.perpendicular_to(line, color=RED)
3.3. Arrow3D Orientation and Style
In this example, we use theArrow3D
object to represent a line segment with an arrow to emphasize direction.
First set the start and end points of the arrows, and to emphasize the arrows, the
Then adjust the thickness (thickness
parameter), the height of the arrow tip (height
parameter) and the bottom radius (base_radius
(Parameters).
# Specify the start and end points and arrow style parameters.
arrow = Arrow3D(
start=[-1, -1, -2], end=[1, 2, 2], # Specify the start and end points and arrow style parameters.
start=[-1, -1, -2], end=[1, 2, 2], thickness=0.05,
thickness=0.05, height=0.2,
base_radius=0.1
base_radius=0.1, color=GREEN,
color=GREEN.
)
3.4. Combined Use of Dot3D, Line3D and Arrow3D
In this synthesized example, we combine the use of theDot3D
、Line3D
cap (a poem)Arrow3D
to create a more complex 3D scene.
First, place aDot3D
object as a starting point.
We will then use theLine3D
object draws a line segment from that point to another location, representing a path or trajectory.
Finally, we'll add a line segment at the end of theArrow3D
Object.
By adjusting the parameters of these objects (e.g., position, color, thickness, etc.), it is possible to create a 3D graphic that is both clear and expressive for a variety of purposes such as presentations, teaching, or scientific research.
# Create a Dot3D object as the starting point
dot = Dot3D(point=[-1, -1, -1], color=BLUE)
# Create a Line3D object to connect the starting point to another point
line = Line3D(
start=[-1, -1, -1], end=[2, 2, 2, 2], color=BLUE
start=[-1, -1, -1], end=[2, 2, 2], thickness=0.05, color=BLUE
thickness=0.05, color=YELLOW,
color=YELLOW, )
)
# Create an Arrow3D object to indicate the direction at the end point
arrow = Arrow3D(
start=[2, 2, 2], end=[1, 2, -1], # Create an Arrow3D object to indicate the direction at the end.
start=[2, 2, 2], end=[1, 2, -1],
start=[2, 2], end=[1, 2, -1], thickness=0.05,
base_radius=0.1
base_radius=0.1, color=RED,
color=RED.
)
4. Annexes
The code in the article is just an extract of the key parts, the complete code is shared on a web disk (dot_line.py
),
Download at.Full Code (Access code: 6872)