Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

enhancement(vector_field): added fit_to_coordinate_system (#2728)

Tristan Schulz7941e1831900
diff --git a/manim/mobject/vector_field.py b/manim/mobject/vector_field.py
index dd48c039a..16074017f 100644
--- a/manim/mobject/vector_field.py
+++ b/manim/mobject/vector_field.py
@@ -19,6 +19,7 @@ from PIL import Image
from manim.animation.updaters.update import UpdateFromAlphaFunc
from manim.mobject.geometry.line import Vector
+from manim.mobject.graphing.coordinate_systems import CoordinateSystem
from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVMobject
from .. import config
@@ -168,6 +169,22 @@ class VectorField(VGroup):
"""
return lambda p: func(p * scalar)
+ def fit_to_coordinate_system(self, coordinate_system: CoordinateSystem):
+ """Scale the vector field to fit a coordinate system.
+
+ This method is useful when the vector field is defined in a coordinate system
+ different from the one used to display the vector field.
+
+ This method can only be used once because it transforms the origin of each vector.
+
+ Parameters
+ ----------
+ coordinate_system
+ The coordinate system to fit the vector field to.
+
+ """
+ self.apply_function(lambda pos: coordinate_system.coords_to_point(*pos))
+
def nudge(
self,
mob: Mobject,