Location>code7788 >text

manim learns by doing - alternating transformations

Popularity:669 ℃/2025-01-20 16:27:49

Today we will introduceManimTwo animation classes for alternating transformations:CyclicReplaceandSwap

Whether it's showing the dynamics of mathematical concepts or presenting the clever interaction of elements in graphic design, both animation classes play an important role.

In their own unique ways, they provide us with rich space for creative expression.

  1. CyclicReplace: Loop to replace the position of a group of objects
  2. Swap: Swap the positions of two specific objects

1. Animation Overview

1.1. CyclicReplace

When you need to replace the position of a group of objects in a loop,CyclicReplaceIs a very useful animation class.

For example, if you have a set of elements arranged in order, and you want to give a visual effect of the elements moving in a circular motion, similar to the element cycle operation of a circular queue, then useCyclicReplaceThis effect can be achieved very well.

It can be used to show cyclic dependencies between elements, or periodic position adjustments, giving people an intuitive feeling of periodic changes.

CyclicReplaceis characterized by combining a group ofMobjectSwap positions according to some circular sequence.

For example, for a set of elements[A, B, C, D], it mayAis replaced byBlocation,B is replaced byC position, and so on, finally theDis replaced byAlocation.

Its main parameters are:

Parameter name type illustrate
mobjects [Mobject] List of mobjects to be transformed
path_arc float mobjects The angle of the arc followed to reach the target position

1.2. Swap

SwapThe animation class is suitable for scenes where the positions of two specific objects need to be exchanged.

When you have two objects and you want to clearly show the interchange of their positions, useSwapAnimation can achieve the effect of directly exchanging positions.

Common application scenarios include swapping two elements in an equation, swapping two data points in a chart, or swapping two elements in a layout to emphasize their equivalence or some association.

It can also be used to compare the situation where two objects before and after are in different positions but have the same functions or attributes, and highlight their interchangeability by exchanging positions.

andCyclicReplacedifferent,SwapMainly operates on two objects.

It will swap the positions of these two objects exactly so that they are in reverse position at the end of the animation.

Its main parameters are:

Parameter name type illustrate
mobjects [Mobject] Mobjects participating in the exchange
path_arc float the angle of the arc that the image follows during the exchange

2. Usage examples

The following examples demonstrate the scenarios in which the above two animation classes can be used.

2.1. Circular movement of elements

This example shows three different shapes (roundsquareandtriangle), which intuitively reflects theCyclicReplaceHow to move a group of objects in a loop.

circle = Circle()
square = Square()
triangle = Triangle()

shapes = VGroup(circle, square, triangle)
(RIGHT)

(shapes)
(CyclicReplace(*shapes))
(CyclicReplace(*shapes))
(CyclicReplace(*shapes))

2.2. Simulate the movement of elements in a circular queue

This example simulates a simple circular queue of numbers1arrive5Arrange in order, passCyclicReplaceThe animation shows them cycling through positions as in a circular queue.

numbers = [Text(str(i)) for i in range(1, 6)]
number_group = VGroup(*numbers).arrange(RIGHT)

(number_group)
(CyclicReplace(*number_group))
(CyclicReplace(*number_group))
(CyclicReplace(*number_group))
(CyclicReplace(*number_group))
(CyclicReplace(*number_group))

2.3. Swap the elements on both sides of the equation

In the context of mathematical equations, first show a simple equationx + 5 = 10, then useSwapSwap the elements in the equation.

eq = MathTex(r"x + 5 = \quad 10")
eq[0][0].set_color(GREEN)
eq[0][2].set_color(BLUE)
eq[0][4:6].set_color(RED)

(eq)
(Swap(eq[0][0], eq[0][2]))
()
(Swap(eq[0][0:3], eq[0][4:6]))

2.4. Symmetrically swap the positions of two figures

This example swaps two different shapes on the left and right (roundandsquare) position, showingSwapUsed for highlighting in graphic layoutsSymmetrical relationshiporposition exchangeeffect.

left_circle = Circle().shift(LEFT)
right_square = Square().shift(RIGHT)

(left_circle, right_square)
(0.5)
(Swap(left_circle, right_square))

3. Accessories

The code in the article is only an interception of key parts, and the complete code is shared on the network disk (),

Download address:Complete code(Access code: 6872)