Location>code7788 >text

manim learn-by-doing - circle class

Popularity:356 ℃/2024-08-15 17:49:16

existmanimThe Circle class is a fundamental and powerful module in the rich library of graphics.
Whether it's a simple circle drawing or a complex circle transformation, it can be realized with concise code.

manimThere are 3 main modules associated with the Middle Circle class:

  1. Circle: Standard round
  2. Annulus: Circular shape
  3. Ellipseellipsoid

Among them.Annuluscap (a poem)Ellipseinherited fromCircle

Round Seriesbe in (some state, position, or condition)manimhit the nail on the headMobjectUnder.

1. Main parameters

unit of Chinese currency (Yuan)CircleThe object has few properties, mainly:

Parameter name typology clarification
radius float radius
color str color
stroke_width float Bezel Thickness
fill_opacity float transparency

traffic circleAnnulusThe main properties of the object are:

Parameter name typology clarification
inner_radius float Inner circle radius
outer_radius float outer ring radius
color str color
mark_paths_closed bool TODO

Annulusrenderableinner_radiuscap (a poem)outer_radiusThe part between, that is, a ring.

ellipsesEllipseThe main properties of the object are:

Parameter name typology clarification
width float ellipse width
height float ellipse height

ellipsesWhen the width and height are set to the same, it is the standardunit of Chinese currency (Yuan)

2. Main approaches

unit of Chinese currency (Yuan)CircleObjects have 3 common methods:

name (of a thing) clarification
from_three_points Draw a circle based on 3 points
point_at_angle Returns the position of a point on a circle
surround Modifies a circle so that it surrounds the given object

See the following for the usage of the above methodusage exampleChapters.

traffic circleAnnulus(math.) and ellipseEllipseThere is no common method.

3. Examples of use

3.1. size and color

For roundCircleFor example, through the radiusradiusto adjust the size;
For ringsAnnulusFor example, through the inner radiusinner_radiusand outer radiusouter_radiusto adjust the size and thickness of the ring;
For ellipsesEllipseFor example, bywidthcap (a poem)heightto adjust the size and flatness of the ellipse.

All three shapes are coloredcolorProperties.

# orbicular
Circle(radius=0.1, color=RED)
Circle(radius=0.3, color=BLUE)
Circle(radius=0.5, color=YELLOW)

# traffic circle
Annulus(
    inner_radius=0.1,
    outer_radius=0.2,
    color=RED,
)
Annulus(
    inner_radius=0.4,
    outer_radius=0.5,
    color=BLUE,
)
Annulus(
    inner_radius=0.4,
    outer_radius=0.8,
    color=YELLOW,
)

# ellipses
Ellipse(
    width=0.3,
    height=0.1,
    color=RED,
)
Ellipse(
    width=0.8,
    height=0.4,
    color=BLUE,
)
Ellipse(
    width=1.8,
    height=1,
    color=YELLOW,
)

The effect is as follows:

3.2 Drawing a circle from three points

In addition to setting the radius by the aboveradiusThe way to draw a circle can also be based on an arbitrarythree-pointto generate a circle.
utilizationfrom_three_pointsMethods.

d1 = Dot(RIGHT, color=RED)
d2 = Dot(UL, color=BLUE)
d3 = Dot(DR, color=GREEN)

Circle.from_three_points(
    d1.get_center(),
    d2.get_center(),
    d3.get_center(),
    color=YELLOW,
)

3.3. Getting a point on a circle

By means of methodspoint_at_angle, we can get a point on the circle based on the angle.
manimTaking the rightmost point of the whole circle (at the same height as the center of the circle) as an angle of\(0^\circ\)The point of the
Then gradually increase the angle counterclockwise.

c = Circle(radius=2, color=YELLOW)

# Point at 60 degrees
p1 = c.point_at_angle(PI / 6)

# Point at 180 degrees
p2 = c.point_at_angle(PI)

# Point at 270 degrees
p3 = c.point_at_angle(3 * PI / 2)

3.4 Surrounding other shapes with circles

And finally, there's asurroundmethod, which has the primary effect of wrapping a circle around another figure.
surroundThe method has abuffer_factor parameters.
(coll.) fail (a student)buffer_factor >= 1when the circle wraps around the outside of the graph; when thebuffer_factor < 1When the circle is inside the graph.

# Star graphics
star = Star()
# buffer_factor=1
# So surround the outside of the star
Circle().surround(star, buffer_factor=1)

# Crosshair graphics
vg = VGroup(
    Line(UP / 2, DOWN / 2), Line(LEFT / 2, RIGHT / 2), VG = VGroup()
    Line(LEFT / 2, RIGHT / 2), )
)
# Default buffer_factor=1.2
# So the surround is outside the crosshairs
Circle().surround(vg)

# Triangle graphics
t = Triangle()
# buffer_factor<1
# So the circle is inside the triangle
Circle().surround(t, buffer_factor=0.3)

4. Annexes

The complete code for the article is on a web disk (),
Download at.Full Code (Access code: 6872)