You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Explain .Transform vs .ReplacementTransform in quickstart examples (#3500)
* Explained ReplacementTransform vs Transform
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Added section explaining Transform vs ReplacementTransform
* Added a->b->c example
* Clarified explanation
* Fixed Typo
* Fixed missing colon
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Tristan Schulz <[email protected]>
Copy file name to clipboardExpand all lines: docs/source/tutorials/quickstart.rst
+53-6Lines changed: 53 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -268,11 +268,9 @@ and animating those method calls with ``.animate``.
268
268
269
269
self.play(Create(square)) # show the square on screen
270
270
self.play(square.animate.rotate(PI/4)) # rotate the square
271
+
self.play(Transform(square, circle)) # transform the square into a circle
271
272
self.play(
272
-
ReplacementTransform(square, circle)
273
-
) # transform the square into a circle
274
-
self.play(
275
-
circle.animate.set_fill(PINK, opacity=0.5)
273
+
square.animate.set_fill(PINK, opacity=0.5)
276
274
) # color the circle on screen
277
275
278
276
2. Render ``AnimatedSquareToCircle`` by running the following command in the command line:
@@ -293,8 +291,8 @@ The following animation will render:
293
291
294
292
self.play(Create(square)) # show the square on screen
295
293
self.play(square.animate.rotate(PI / 4)) # rotate the square
296
-
self.play(ReplacementTransform(square, circle)) # transform the square into a circle
297
-
self.play(circle.animate.set_fill(PINK, opacity=0.5)) # color the circle on screen
294
+
self.play(Transform(square, circle)) # transform the square into a circle
295
+
self.play(square.animate.set_fill(PINK, opacity=0.5)) # color the circle on screen
298
296
299
297
The first ``self.play`` creates the square. The second animates rotating it 45 degrees.
300
298
The third transforms the square into a circle, and the last colors the circle pink.
@@ -348,6 +346,55 @@ If you find that your own usage of ``.animate`` is causing similar unwanted beha
348
346
using conventional animation methods like the right square, which uses ``Rotate``.
349
347
350
348
349
+
``Transform`` vs ``ReplacementTransform``
350
+
*****************************************
351
+
The difference between ``Transform`` and ``ReplacementTransform`` is that ``Transform(mob1, mob2)`` transforms the points
352
+
(as well as other attributes like color) of ``mob1`` into the points/attributes of ``mob2``.
353
+
354
+
``ReplacementTransform(mob1, mob2)`` on the other hand literally replaces ``mob1`` on the scene with ``mob2``.
355
+
356
+
The use of ``ReplacementTransform`` or ``Transform`` is mostly up to personal preference. They can be used to accomplish the same effect, as shown below.
357
+
358
+
.. code-block:: python
359
+
360
+
classTwoTransforms(Scene):
361
+
deftransform(self):
362
+
a = Circle()
363
+
b = Square()
364
+
c = Triangle()
365
+
self.play(Transform(a, b))
366
+
self.play(Transform(a, c))
367
+
self.play(FadeOut(a))
368
+
369
+
defreplacement_transform(self):
370
+
a = Circle()
371
+
b = Square()
372
+
c = Triangle()
373
+
self.play(ReplacementTransform(a, b))
374
+
self.play(ReplacementTransform(b, c))
375
+
self.play(FadeOut(c))
376
+
377
+
defconstruct(self):
378
+
self.transform()
379
+
self.wait(0.5) # wait for 0.5 seconds
380
+
self.replacement_transform()
381
+
382
+
383
+
However, in some cases it is more beneficial to use ``Transform``, like when you are transforming several mobjects one after the other.
384
+
The code below avoids having to keep a reference to the last mobject that was transformed.
0 commit comments