Skip to content

Commit ac17f75

Browse files
hydrobeambehackl
andauthored
Fix bug in :meth:vector_coordinate_label and move it to :class:~.geometry.py (#1407)
* remove vector_coordinate_label, move to geometry.py, fix bug * black * documentation and example * apply suggestions from code review Co-authored-by: Benjamin Hackl <[email protected]>
1 parent b4b68a9 commit ac17f75

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

manim/mobject/geometry.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,58 @@ def __init__(self, direction=RIGHT, buff=0, **kwargs):
12931293
direction = np.append(np.array(direction), 0)
12941294
Arrow.__init__(self, ORIGIN, direction, buff=buff, **kwargs)
12951295

1296+
def coordinate_label(
1297+
self, integer_labels: bool = True, n_dim: int = 2, color: str = WHITE
1298+
):
1299+
"""Creates a label based on the coordinates of the vector.
1300+
1301+
Parameters
1302+
----------
1303+
integer_labels
1304+
Whether or not to round the coordinates to integers.
1305+
n_dim
1306+
The number of dimensions of the vector.
1307+
color
1308+
The color of the label.
1309+
1310+
Examples
1311+
--------
1312+
1313+
.. manim VectorCoordinateLabel
1314+
:save_last_frame:
1315+
1316+
class VectorCoordinateLabel(Scene):
1317+
def construct(self):
1318+
plane = NumberPlane()
1319+
1320+
vect_1 = Vector([1, 2])
1321+
vect_2 = Vector([-3, -2])
1322+
label_1 = vect1.coordinate_label()
1323+
label_2 = vect2.coordinate_label(color=YELLOW)
1324+
1325+
self.add(plane, vect_1, vect_2, label_1, label_2)
1326+
"""
1327+
# avoiding circular imports
1328+
from .matrix import Matrix
1329+
1330+
vect = np.array(self.get_end())
1331+
if integer_labels:
1332+
vect = np.round(vect).astype(int)
1333+
vect = vect[:n_dim]
1334+
vect = vect.reshape((n_dim, 1))
1335+
1336+
label = Matrix(vect)
1337+
label.scale(LARGE_BUFF - 0.2)
1338+
1339+
shift_dir = np.array(self.get_end())
1340+
if shift_dir[0] >= 0: # Pointing right
1341+
shift_dir -= label.get_left() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * LEFT
1342+
else: # Pointing left
1343+
shift_dir -= label.get_right() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * RIGHT
1344+
label.shift(shift_dir)
1345+
label.set_color(color)
1346+
return label
1347+
12961348

12971349
class DoubleArrow(Arrow):
12981350
"""An arrow with tips on both ends.

manim/mobject/matrix.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def construct(self):
3131
"MobjectMatrix",
3232
"matrix_to_tex_string",
3333
"matrix_to_mobject",
34-
"vector_coordinate_label",
3534
"get_det_text",
3635
]
3736

@@ -45,8 +44,6 @@ def construct(self):
4544
from ..mobject.types.vectorized_mobject import VGroup, VMobject
4645
from ..utils.color import WHITE
4746

48-
VECTOR_LABEL_SCALE_FACTOR = 0.8
49-
5047

5148
def matrix_to_tex_string(matrix):
5249
matrix = np.array(matrix).astype("str")
@@ -63,27 +60,6 @@ def matrix_to_mobject(matrix):
6360
return MathTex(matrix_to_tex_string(matrix))
6461

6562

66-
def vector_coordinate_label(vector_mob, integer_labels=True, n_dim=2, color=WHITE):
67-
vect = np.array(vector_mob.get_end())
68-
if integer_labels:
69-
vect = np.round(vect).astype(int)
70-
vect = vect[:n_dim]
71-
vect = vect.reshape((n_dim, 1))
72-
label = Matrix(vect, add_background_rectangles_to_entries=True)
73-
label.scale(VECTOR_LABEL_SCALE_FACTOR)
74-
75-
shift_dir = np.array(vector_mob.get_end())
76-
if shift_dir[0] >= 0: # Pointing right
77-
shift_dir -= label.get_left() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * LEFT
78-
else: # Pointing left
79-
shift_dir -= label.get_right() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * RIGHT
80-
label.shift(shift_dir)
81-
label.set_color(color)
82-
label.rect = BackgroundRectangle(label)
83-
label.add_to_back(label.rect)
84-
return label
85-
86-
8763
class Matrix(VMobject):
8864
"""A mobject that displays a matrix on the screen."""
8965

manim/scene/vector_space_scene.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ..constants import *
1515
from ..mobject.coordinate_systems import Axes, NumberPlane
1616
from ..mobject.geometry import Arrow, Dot, Line, Rectangle, Vector
17-
from ..mobject.matrix import VECTOR_LABEL_SCALE_FACTOR, Matrix, vector_coordinate_label
17+
from ..mobject.matrix import Matrix
1818
from ..mobject.mobject import Mobject
1919
from ..mobject.svg.tex_mobject import MathTex, Tex
2020
from ..mobject.types.vectorized_mobject import VGroup, VMobject
@@ -172,7 +172,7 @@ def write_vector_coordinates(self, vector, **kwargs):
172172
The arrow representing the vector.
173173
174174
**kwargs
175-
Any valid keyword arguments of :meth:`~.matrix.vector_coordinate_label`:
175+
Any valid keyword arguments of :meth:`~.geometry.Vector.coordinate_label`:
176176
177177
integer_labels : :class:`bool`
178178
Whether or not to round the coordinates to integers. Default: ``True``.
@@ -186,7 +186,7 @@ def write_vector_coordinates(self, vector, **kwargs):
186186
:class:`.Matrix`
187187
The column matrix representing the vector.
188188
"""
189-
coords = vector_coordinate_label(vector, **kwargs)
189+
coords = vector.coordinate_label(**kwargs)
190190
self.play(Write(coords))
191191
return coords
192192

@@ -251,7 +251,7 @@ def get_vector_label(
251251
direction="left",
252252
rotate=False,
253253
color=None,
254-
label_scale_factor=VECTOR_LABEL_SCALE_FACTOR,
254+
label_scale_factor=LARGE_BUFF - 0.2,
255255
):
256256
"""
257257
Returns naming labels for the passed vector.
@@ -433,7 +433,7 @@ def vector_to_coords(self, vector, integer_labels=True, clean_up=True):
433433
else:
434434
arrow = Vector(vector)
435435
show_creation = True
436-
array = vector_coordinate_label(arrow, integer_labels=integer_labels)
436+
array = arrow.coordinate_label(integer_labels=integer_labels)
437437
x_line = Line(ORIGIN, vector[0] * RIGHT)
438438
y_line = Line(x_line.get_end(), arrow.get_end())
439439
x_line.set_color(X_COLOR)

0 commit comments

Comments
 (0)