Location>code7788 >text

manim learning-by-doing - common polygons

Popularity:453 ℃/2024-09-10 15:30:53

polygonalIt is a common geometric structure that has a seemingly infinite variety of shapes, all of which can actually be made from a combination of several commonly used polygons.

This article introducesmanimThere are several drawing options available in theCommon Polygonsof the module.

  1. Triangle: equilateral triangle
  2. Square: Square
  3. Rectangle: Rectangular
  4. RoundedRectangle: rectangle with rounded corners
  5. Star: a regular polygon with no intersecting lines, resembling a star with pointed corners.

1. Main parameters

Of these modules.TriangleAt its simplest, it has no parameters specific to itself.

SquareThere is one parameter:

Parameter name typology clarification
side_length float Length of the sides of the square

RectangleSlightly more complex, it can be evenly chunked to form tables.

Parameter name typology clarification
height float Height of rectangle
width float Width of rectangle
grid_xstep float The width of each column after dividing the rectangle
grid_ystep float The height of each row after dividing the rectangle

RoundedRectangleinherited fromRectangleYou can use theRectangleof all the parameters of the

In addition, it has a parameter specific to itself.

Parameter name typology clarification
corner_radius float list[float]

RoundedRectangleThe curvature of the four corners can be set uniformly or to different curvatures.

StarThe module is so named because it draws shapes that look like little stars.

Parameter name typology clarification
n int How many pointed corners does a star shape have
outer_radius float Radius of the outer circle of the graph
inner_radius float Radius of the tangent circle of the graph
density int The density of the sharp corners of the graph, valid when inner_radius is set.
start_angle float The angle at which the vertex starts

If you do not understand the meaning of these properties, it does not matter, later combined with examples to show the difference between the star-shaped graphic under different parameters.

It will be able to see a little more clearly.

2. Examples of use

2.1 Equilateral triangles and squares

equilateral triangleTriangleSort of the simplest polygon, it has no parameters, the

But it can be done byscalerotateetc. to change its size and angle.

Triangle()

# Zoom in 1.5 times
Triangle().scale(1.5)

# Rotate 180 degrees
Triangle().rotate(PI)

squareSquareIt's also simple, it has only one parameter, setting the length of the sides of the square.

Square(side_length=0.5)
Square(side_length=1)
Square(side_length=2)

The above code is displayed as follows:

2.2. rectangular

rectangularRectangleIn addition to being able to set the widthwidthheightsheight, which can also be chunked.

The so-called chunking is done bygrid_xsteprespond in singinggrid_ystepThe parameter tells the rectangle to be split into smaller rectangles.

The width of each small rectangle iswidth / grid_xstepThe height isheight / grid_ystep

Rectangle(width=2, height=1)
Rectangle(width=1, height=3)

# split2classifier for objects in rows such as words3Rectangle of columns
Rectangle(
    width=3,
    height=2,
    grid_xstep=1,
    grid_ystep=1,
)

2.3 Rounded rectangles

rectangle with rounded cornersRoundedRectanglerespond in singingrectangularRectangleThe difference is that it can set the curvature of the 4 corners.

Rectanglehas the parameter thatRoundedRectanglecan also be used, including the parameters for chunking.

# 4 corners have the same curvature
RoundedRectangle(
    corner_radius=0.4,
)
# Same curvature on the diagonal
RoundedRectangle(
    corner_radius=[0.2, 0.6], ) # Same diagonal curvature RoundedRectangle(
)

# All 4 corners have different curvatures
RoundedRectangle(
    corner_radius=[0.1, 0.6, 0.3, 0.9], ) # The 4 corners have different curvatures.
)

2.4. stars

star polygonStarIt is a special type of concave polygon that is often used as a decorative pattern and design element because of its unique shape and symmetry.

StarThe module can be parameterized by the number of sharp corners and the density of the sharp corners.

Star(n=5)

# The higher the density, the denser the corners look
Star(n=9, density=2)
Star(n=9, density=4)

3. Annexes

The complete code for the article is on a web disk (),

Download at.Full Code (Access code: 6872)