Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions manim/mobject/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,8 @@ def coordinate_label(
self,
integer_labels: bool = True,
n_dim: int = 2,
color: str = WHITE,
color: Optional[Color] = None,
**kwargs,
):
"""Creates a label based on the coordinates of the vector.

Expand All @@ -1572,24 +1573,32 @@ def coordinate_label(
n_dim
The number of dimensions of the vector.
color
The color of the label.
Sets the color of label, optional.
kwargs
Additional arguments to be passed to :class:`~.Matrix`.

Examples
--------

.. manim VectorCoordinateLabel
.. 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)
vec_1 = Vector([1, 2])
vec_2 = Vector([-3, -2])
label_1 = vec_1.coordinate_label()
label_2 = vec_2.coordinate_label(color=YELLOW)

self.add(plane, vec_1, vec_2, label_1, label_2)

Returns
-------
:class:`~.Matrix`

self.add(plane, vect_1, vect_2, label_1, label_2)
The label.
"""
# avoiding circular imports
from .matrix import Matrix
Expand All @@ -1599,8 +1608,7 @@ def construct(self):
vect = np.round(vect).astype(int)
vect = vect[:n_dim]
vect = vect.reshape((n_dim, 1))

label = Matrix(vect)
label = Matrix(vect, **kwargs)
label.scale(LARGE_BUFF - 0.2)

shift_dir = np.array(self.get_end())
Expand All @@ -1609,7 +1617,8 @@ def construct(self):
else: # Pointing left
shift_dir -= label.get_right() + DEFAULT_MOBJECT_TO_MOBJECT_BUFFER * RIGHT
label.shift(shift_dir)
label.set_color(color)
if color is not None:
label.set_color(color)
return label


Expand Down
84 changes: 45 additions & 39 deletions manim/mobject/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def construct(self):


import itertools as it
from typing import Iterable, Sequence, Type

import numpy as np

Expand Down Expand Up @@ -126,48 +127,53 @@ def construct(self):

def __init__(
self,
matrix,
v_buff=0.8,
h_buff=1.3,
bracket_h_buff=MED_SMALL_BUFF,
bracket_v_buff=MED_SMALL_BUFF,
add_background_rectangles_to_entries=False,
include_background_rectangle=False,
element_to_mobject=MathTex,
element_to_mobject_config={},
element_alignment_corner=DR,
left_bracket="[",
right_bracket="]",
matrix: Iterable,
v_buff: float = 0.8,
h_buff: float = 1.3,
bracket_h_buff: float = MED_SMALL_BUFF,
bracket_v_buff: float = MED_SMALL_BUFF,
add_background_rectangles_to_entries: bool = False,
include_background_rectangle: bool = False,
element_to_mobject: Type[MathTex] = MathTex,
element_to_mobject_config: dict = {},
element_alignment_corner: Sequence[float] = DR,
left_bracket: str = "[",
right_bracket: str = "]",
bracket_config: dict = {},
**kwargs,
):
"""

Parameters
----------
matrix : :class:`typing.Iterable`
A numpy 2d array or list of lists
v_buff : :class:`float`, optional
vertical buffer, by default 0.8
h_buff : :class:`float`, optional
horizontal buffer, by default 1.3
bracket_h_buff : :class:`float`, optional
bracket horizontal buffer, by default MED_SMALL_BUFF
bracket_v_buff : :class:`float`, optional
bracket vertical buffer, by default MED_SMALL_BUFF
add_background_rectangles_to_entries : :class:`bool`, optional
`True` if should add backgraound rectangles to entries, by default False
include_background_rectangle : :class:`bool`, optional
`True` if should include background rectangle, by default False
element_to_mobject : :class:`~.Mobject`, optional
element to mobject, by default MathTex
element_to_mobject_config : Dict[:class:`str`, :class:`~.Mobject`], optional
element to mobject config, by default {}
element_alignment_corner : :class:`np.ndarray`, optional
the element alignment corner, by default DR
left_bracket : :class:`str`, optional
the left bracket type, by default "["
right_bracket : :class:`str`, optional
the right bracket type, by default "]"
matrix
A numpy 2d array or list of lists.
v_buff
Vertical distance between elements, by default 0.8.
h_buff
Horizontal distance between elements, by default 1.3.
bracket_h_buff
Distance of the brackets from the matrix, by default ``MED_SMALL_BUFF``.
bracket_v_buff
Height of the brackets, by default ``MED_SMALL_BUFF``.
add_background_rectangles_to_entries
``True`` if should add backgraound rectangles to entries, by default ``False``.
include_background_rectangle
``True`` if should include background rectangle, by default ``False``.
element_to_mobject
The mobject class used to construct the elements, by default :class:`~.MathTex`.
element_to_mobject_config
Additional arguments to be passed to the constructor in ``element_to_mobject``,
by default ``{}``.
element_alignment_corner
The corner to which elements are aligned, by default ``DR``.
left_bracket
The left bracket type, by default ``"["``.
right_bracket
The right bracket type, by default ``"]"``.
bracket_config
Additional arguments to be passed to :class:`~.MathTex` when constructing
the brackets.

"""

Expand All @@ -187,7 +193,7 @@ def __init__(
self.organize_mob_matrix(mob_matrix)
self.elements = VGroup(*it.chain(*mob_matrix))
self.add(self.elements)
self.add_brackets(self.left_bracket, self.right_bracket)
self.add_brackets(self.left_bracket, self.right_bracket, **bracket_config)
self.center()
self.mob_matrix = mob_matrix
if self.add_background_rectangles_to_entries:
Expand Down Expand Up @@ -217,7 +223,7 @@ def organize_mob_matrix(self, matrix):
)
return self

def add_brackets(self, left="[", right="]"):
def add_brackets(self, left="[", right="]", **kwargs):
"""Used internally. Adds the brackets to the Matrix mobject.

See Latex document for various bracket types.
Expand All @@ -235,7 +241,7 @@ def add_brackets(self, left="[", right="]"):
The current matrix object (self).
"""

bracket_pair = MathTex(left, right)
bracket_pair = MathTex(left, right, **kwargs)
bracket_pair.scale(2)
bracket_pair.stretch_to_fit_height(self.height + 2 * self.bracket_v_buff)
l_bracket, r_bracket = bracket_pair.split()
Expand Down