Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix bug in :meth:`vector_coordinate_label` and move it to :class:`~.geometry.py` (#1407)

hydrobeamac17f757a08f
diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.py
index ff3e9fc68..bb0d2c09e 100644
--- a/manim/mobject/geometry.py
+++ b/manim/mobject/geometry.py
@@ -1293,6 +1293,58 @@ class Vector(Arrow):
direction = np.append(np.array(direction), 0)
Arrow.__init__(self, ORIGIN, direction, buff=buff, **kwargs)
+ def coordinate_label(
+ self, integer_labels: bool = True, n_dim: int = 2, color: str = WHITE
+ ):
+ """Creates a label based on the coordinates of the vector.
+
+ Parameters
+ ----------
+ integer_labels
+ Whether or not to round the coordinates to integers.
+ n_dim
+ The number of dimensions of the vector.
+ color
+ The color of the label.
+
+ Examples
+ --------
+
+ .. manim VectorCoordinateLabel
+ :save_last_frame:
+
+ class VectorCoordinateLabel(Scene):
+ def construct(self):
+ plane = NumberPlane()
+
+ vect_1 = Vector([1, 2])
+ vect_2 = Vector([-3, -2])
+ label_1 = vect1.coordinate_label()
+ label_2 = vect2.coordinate_label(color=YELLOW)
+
+ self.add(plane, vect_1, vect_2, label_1, label_2)
+ """
+ # avoiding circular imports
+ from .matrix import Matrix
+
+ vect = np.array(self.get_end())
+ if integer_labels:
+ vect = np.round(vect).astype(int)
+ vect = vect[:n_dim]
+ vect = vect.reshape((n_dim, 1))
+
+ label = Matrix(vect)
+ label.scale(LARGE_BUFF - 0.2)
+
+ shift_dir = np.array(self.get_end())
+ if shift_dir[0] >= 0: # Pointing right
+ shift_dir -= label.get_left() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * LEFT
+ else: # Pointing left
+ shift_dir -= label.get_right() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * RIGHT
+ label.shift(shift_dir)
+ label.set_color(color)
+ return label
+
class DoubleArrow(Arrow):
"""An arrow with tips on both ends.
diff --git a/manim/mobject/matrix.py b/manim/mobject/matrix.py
index d92cc3486..f7b67dc35 100644
--- a/manim/mobject/matrix.py
+++ b/manim/mobject/matrix.py
@@ -31,7 +31,6 @@ __all__ = [
"MobjectMatrix",
"matrix_to_tex_string",
"matrix_to_mobject",
- "vector_coordinate_label",
"get_det_text",
]
@@ -45,8 +44,6 @@ from ..mobject.svg.tex_mobject import MathTex, Tex
from ..mobject.types.vectorized_mobject import VGroup, VMobject
from ..utils.color import WHITE
-VECTOR_LABEL_SCALE_FACTOR = 0.8
-
def matrix_to_tex_string(matrix):
matrix = np.array(matrix).astype("str")
@@ -63,27 +60,6 @@ def matrix_to_mobject(matrix):
return MathTex(matrix_to_tex_string(matrix))
-def vector_coordinate_label(vector_mob, integer_labels=True, n_dim=2, color=WHITE):
- vect = np.array(vector_mob.get_end())
- if integer_labels:
- vect = np.round(vect).astype(int)
- vect = vect[:n_dim]
- vect = vect.reshape((n_dim, 1))
- label = Matrix(vect, add_background_rectangles_to_entries=True)
- label.scale(VECTOR_LABEL_SCALE_FACTOR)
-
- shift_dir = np.array(vector_mob.get_end())
- if shift_dir[0] >= 0: # Pointing right
- shift_dir -= label.get_left() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * LEFT
- else: # Pointing left
- shift_dir -= label.get_right() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * RIGHT
- label.shift(shift_dir)
- label.set_color(color)
- label.rect = BackgroundRectangle(label)
- label.add_to_back(label.rect)
- return label
-
-
class Matrix(VMobject):
"""A mobject that displays a matrix on the screen."""
diff --git a/manim/scene/vector_space_scene.py b/manim/scene/vector_space_scene.py
index db0d117a3..0135736f7 100644
--- a/manim/scene/vector_space_scene.py
+++ b/manim/scene/vector_space_scene.py
@@ -14,7 +14,7 @@ from ..animation.transform import ApplyFunction, ApplyPointwiseFunction, Transfo
from ..constants import *
from ..mobject.coordinate_systems import Axes, NumberPlane
from ..mobject.geometry import Arrow, Dot, Line, Rectangle, Vector
-from ..mobject.matrix import VECTOR_LABEL_SCALE_FACTOR, Matrix, vector_coordinate_label
+from ..mobject.matrix import Matrix
from ..mobject.mobject import Mobject
from ..mobject.svg.tex_mobject import MathTex, Tex
from ..mobject.types.vectorized_mobject import VGroup, VMobject
@@ -172,7 +172,7 @@ class VectorScene(Scene):
The arrow representing the vector.
**kwargs
- Any valid keyword arguments of :meth:`~.matrix.vector_coordinate_label`:
+ Any valid keyword arguments of :meth:`~.geometry.Vector.coordinate_label`:
integer_labels : :class:`bool`
Whether or not to round the coordinates to integers. Default: ``True``.
@@ -186,7 +186,7 @@ class VectorScene(Scene):
:class:`.Matrix`
The column matrix representing the vector.
"""
- coords = vector_coordinate_label(vector, **kwargs)
+ coords = vector.coordinate_label(**kwargs)
self.play(Write(coords))
return coords
@@ -251,7 +251,7 @@ class VectorScene(Scene):
direction="left",
rotate=False,
color=None,
- label_scale_factor=VECTOR_LABEL_SCALE_FACTOR,
+ label_scale_factor=LARGE_BUFF - 0.2,
):
"""
Returns naming labels for the passed vector.
@@ -433,7 +433,7 @@ class VectorScene(Scene):
else:
arrow = Vector(vector)
show_creation = True
- array = vector_coordinate_label(arrow, integer_labels=integer_labels)
+ array = arrow.coordinate_label(integer_labels=integer_labels)
x_line = Line(ORIGIN, vector[0] * RIGHT)
y_line = Line(x_line.get_end(), arrow.get_end())
x_line.set_color(X_COLOR)