Location>code7788 >text

manim learning by doing - curve class

Popularity:815 ℃/2024-09-04 11:00:15

manimcenterin a roundabout wayIn addition to the arc-like curves described earlier, arbitrary curves can also be drawn.

manimWhat is provided in theCubicBeziermodule, which can draw arbitrary curves using cubic Bezier curves.

For an introduction to Bezier curves, see:/wiki/B%C3%A9zier_curve

This article focuses on modules for Bezier curves and two types of curves with arrows.

  1. CubicBezier: Cubic Bézier curves, which can draw arbitrary curves in the plane.
  2. CurvedArrow: Single Arrow Curve
  3. CurvedDoubleArrow: Double Arrow Curve

1. Main parameters

CurvedArrowcap (a poem)CurvedDoubleArrowThe main parameters of the curve are the beginning and the end of the curve, the

These two modules inherit from theArcBetweenPointsModule.

Parameter name typology clarification
start Point3D beginning
end Point3D finishing line (in a race)

CubicBezierThe parameters of the module are four points. It is recommended to understand the principle of the cubic Bessel curve, and then you will be able to understand the significance of these four parameters.

Parameter name typology clarification
start_anchor Point3D beginning
start_handle Point3D The first control point, which affects the direction of the bend from the start of the curve to the middle part of the curve
end_handle Point3D The second control point, which affects the direction of the bend from the middle part of the curve to the end point
end_anchor Point3D finishing line (in a race)

For example, the following Bezier curve, where.

  • $ P_0 $ Equivalent to parameterstart_anchor
  • $ P_1
    $ is equivalent to the parameterstart_handle
  • $ P_2 $ Equivalent to parameterend_handle
  • $ P_4 $ Equivalent to parameterend_anchor

2. Examples of use

2.1 Curves with arrows

The curves with arrows are given bybeginningstart followingcounterclockwiseDirection of rotation tofinishing line (in a race)

CurvedArrow(
    LEFT / 2 + UP,
    RIGHT / 2 + UP,
    color=BLUE,
)
CurvedArrow(
    LEFT + UP / 2,
    RIGHT + UP / 2,
    color=RED,
)
CurvedDoubleArrow(
    LEFT * 2 + UP / 2,
    RIGHT * 2 + UP / 2,
    color=YELLOW,
)

CurvedDoubleArrow(
    RIGHT * 2 + DOWN * 1.6,
    LEFT * 2 + DOWN * 1.6,
    color=YELLOW,
)
CurvedArrow(
    RIGHT + DOWN * 1.6,
    LEFT + DOWN * 1.6,
    color=RED,
)
CurvedArrow(
    RIGHT / 2 + DOWN * 2.1,
    LEFT / 2 + DOWN * 2.1,
    color=BLUE,
)

2.2. Bessel curve plotting process

Bézier curve through the four points can draw a very smooth curve, the principle of which there are many articles on the network, will not repeat here.

Below is an animation demonstrating the principle of its drawing.

  1. White dots: 4 fixed points for drawing Bezier curves
  2. blue dot: Calculated on the basis of 4 white dots
  3. red dot: Calculated on the basis of 3 blue dots
  4. yellow dots: Calculated on the basis of 2 red dots

blue dotstroll alongwhite pointThe connected lines move.red dotWith the blue dot linkage.yellow dotsLinked with the red dot.

yellow dotsThe trajectory of the motion is the plotted curve.

I've got a solution!CubicBeziermodule, you can draw a Bezier curve directly according to the four points, the effect is the same as above, only the code will be much simplified.

points = [
    LEFT * 2 + DOWN,
    LEFT + UP,
    RIGHT * 1.5 + UP,
    RIGHT * 2 + DOWN,
]

CubicBezier(
    points[0],
    points[1],
    points[2],
    points[3],
    color=YELLOW,
)

2.3 Drawing hearts

Bézier curves can draw very smooth curves, and can theoretically draw a variety of complex curved shapes.

Here's an attempt to use cubic Bézier curves to draw alovingThe pattern.

points = [
    2 * DOWN * 0.5,
    
    
    2 * UP * 0.5,]
]
# Left half
CubicBezier(
    points[0],
    
    points[2],
    points[2], points[3],
    color=RED, )
)

points = [
    2 * DOWN * 0.5,
    
    
    2 * UP * 0.5,, [RIGHT
]
# Right half
CubicBezier(
    points[0],
    
    points[2],
    points[3],
    color=GREEN, )
)

3. Annexes

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

Download at.Full Code (Access code: 6872)