Skip to content

Commit 8ba210f

Browse files
andresberejnoijsonvillanuevapre-commit-ci[bot]
authored
Two New Classes in geometry module: :class:.LabeledLine and :class:.LabeledArrow (#3264)
* feat: added two new classes LabeledLine and LabeledArrow * test: added tests for new LabeledLine and LabeledArrow for the geometry module testing suite * feat: added new class names to '__all__' module attribute * fix: added missing import line for new classes * fix: removed import lines causing cyclic import error * new file containing two new classes * modified files to correctly load new classes when manim is imported * commented out new classes in line.py * created control frames for LabeledLine and LabeledArrow * removed commented out classes * removed unused import of 'Colors' * Update manim/mobject/geometry/labeled_shapes.py Co-authored-by: Jason Villanueva <a@jsonvillanueva.com> * Update manim/mobject/geometry/labeled_shapes.py Co-authored-by: Jason Villanueva <a@jsonvillanueva.com> * Update manim/mobject/geometry/labeled_shapes.py Co-authored-by: Jason Villanueva <a@jsonvillanueva.com> * Update manim/mobject/geometry/labeled_shapes.py Co-authored-by: Jason Villanueva <a@jsonvillanueva.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update __init__.py will change name of labeled_shapes.py to labeled.py * Rename labeled_shapes.py to labeled.py * Update __init__.py Changed name of file from labeled_shapes.py to labeled.py and updated __init__.py to import from the correct location. * Update manim/mobject/geometry/labeled.py Removed color import line because of redundancy. Co-authored-by: Jason Villanueva <a@jsonvillanueva.com> * Update labeled.py default label_color parameter is now set to `None` instead of `WHITE`. * Update manim/mobject/geometry/labeled.py Co-authored-by: Jason Villanueva <a@jsonvillanueva.com> * Update tests/test_graphical_units/test_geometry.py Co-authored-by: Jason Villanueva <a@jsonvillanueva.com> * Revert changes related to WHITE and it's import SurroundingRectangle uses label_color as well and using a default of None removes the rectangle. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Jason Villanueva <a@jsonvillanueva.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 56bc8ad commit 8ba210f

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed

manim/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from .mobject.frame import *
4747
from .mobject.geometry.arc import *
4848
from .mobject.geometry.boolean_ops import *
49+
from .mobject.geometry.labeled import *
4950
from .mobject.geometry.line import *
5051
from .mobject.geometry.polygram import *
5152
from .mobject.geometry.shape_matchers import *

manim/mobject/geometry/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
~arc
1010
~boolean_ops
11+
~labeled
1112
~line
1213
~polygram
1314
~shape_matchers

manim/mobject/geometry/labeled.py

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
r"""Mobjects that inherit from lines and contain a label along the length."""
2+
3+
from __future__ import annotations
4+
5+
__all__ = ["LabeledLine", "LabeledArrow"]
6+
7+
from manim.constants import *
8+
from manim.mobject.geometry.line import Arrow, Line
9+
from manim.mobject.geometry.shape_matchers import (
10+
BackgroundRectangle,
11+
SurroundingRectangle,
12+
)
13+
from manim.mobject.text.tex_mobject import MathTex, Tex
14+
from manim.mobject.text.text_mobject import Text
15+
from manim.utils.color import WHITE, Color
16+
17+
18+
class LabeledLine(Line):
19+
"""Constructs a line containing a label box somewhere along its length.
20+
21+
Parameters
22+
----------
23+
label : str | Tex | MathTex | Text
24+
Label that will be displayed on the line.
25+
label_position : float | optional
26+
A ratio in the range [0-1] to indicate the position of the label with respect to the length of the line. Default value is 0.5.
27+
font_size : float | optional
28+
Control font size for the label. This parameter is only used when `label` is of type `str`.
29+
label_color: numpy.ndarray | optional
30+
The color of the label's text. This parameter is only used when `label` is of type `str`.
31+
label_frame : Bool | optional
32+
Add a `SurroundingRectangle` frame to the label box.
33+
frame_fill_color : numpy.ndarray | optional
34+
Background color to fill the label box. If no value is provided, the background color of the canvas will be used.
35+
frame_fill_opacity : float | optional
36+
Determine the opacity of the label box by passing a value in the range [0-1], where 0 indicates complete transparency and 1 means full opacity.
37+
38+
.. seealso::
39+
:class:`LabeledArrow`
40+
41+
Examples
42+
--------
43+
.. manim:: LabeledLineExample
44+
:save_last_frame:
45+
46+
class LabeledLineExample(Scene):
47+
def construct(self):
48+
line = LabeledLine(
49+
label = '0.5',
50+
label_position = 0.8,
51+
font_size = 20,
52+
label_color = WHITE,
53+
label_frame = True,
54+
55+
start=LEFT+DOWN,
56+
end=RIGHT+UP)
57+
58+
59+
line.set_length(line.get_length() * 2)
60+
self.add(line)
61+
"""
62+
63+
def __init__(
64+
self,
65+
label: str | Tex | MathTex | Text,
66+
label_position: float = 0.5,
67+
font_size: float = DEFAULT_FONT_SIZE,
68+
label_color: Color | str | None = WHITE,
69+
label_frame: bool = True,
70+
frame_fill_color: Color | str | None = None,
71+
frame_fill_opacity: float = 1,
72+
*args,
73+
**kwargs,
74+
) -> None:
75+
if isinstance(label, str):
76+
from manim import MathTex
77+
78+
rendered_label = MathTex(label, color=label_color, font_size=font_size)
79+
else:
80+
rendered_label = label
81+
82+
super().__init__(*args, **kwargs)
83+
84+
# calculating the vector for the label position
85+
line_start, line_end = self.get_start_and_end()
86+
new_vec = (line_end - line_start) * label_position
87+
label_coords = line_start + new_vec
88+
89+
# rendered_label.move_to(self.get_vector() * label_position)
90+
rendered_label.move_to(label_coords)
91+
92+
box = BackgroundRectangle(
93+
rendered_label,
94+
buff=0.05,
95+
color=frame_fill_color,
96+
fill_opacity=frame_fill_opacity,
97+
stroke_width=0.5,
98+
)
99+
self.add(box)
100+
101+
if label_frame:
102+
box_frame = SurroundingRectangle(
103+
rendered_label, buff=0.05, color=label_color, stroke_width=0.5
104+
)
105+
106+
self.add(box_frame)
107+
108+
self.add(rendered_label)
109+
110+
111+
class LabeledArrow(LabeledLine, Arrow):
112+
"""Constructs an arrow containing a label box somewhere along its length.
113+
This class inherits its label properties from `LabeledLine`, so the main parameters controlling it are the same.
114+
115+
Parameters
116+
----------
117+
label : str | Tex | MathTex | Text
118+
Label that will be displayed on the line.
119+
label_position : float | optional
120+
A ratio in the range [0-1] to indicate the position of the label with respect to the length of the line. Default value is 0.5.
121+
font_size : float | optional
122+
Control font size for the label. This parameter is only used when `label` is of type `str`.
123+
label_color: numpy.ndarray | optional
124+
The color of the label's text. This parameter is only used when `label` is of type `str`.
125+
label_frame : Bool | optional
126+
Add a `SurroundingRectangle` frame to the label box.
127+
frame_fill_color : numpy.ndarray | optional
128+
Background color to fill the label box. If no value is provided, the background color of the canvas will be used.
129+
frame_fill_opacity : float | optional
130+
Determine the opacity of the label box by passing a value in the range [0-1], where 0 indicates complete transparency and 1 means full opacity.
131+
132+
133+
.. seealso::
134+
:class:`LabeledLine`
135+
136+
Examples
137+
--------
138+
.. manim:: LabeledArrowExample
139+
:save_last_frame:
140+
141+
class LabeledArrowExample(Scene):
142+
def construct(self):
143+
l_arrow = LabeledArrow("0.5", start=LEFT*3, end=RIGHT*3 + UP*2, label_position=0.5)
144+
145+
self.add(l_arrow)
146+
"""
147+
148+
def __init__(
149+
self,
150+
*args,
151+
**kwargs,
152+
) -> None:
153+
super().__init__(*args, **kwargs)
Binary file not shown.
Binary file not shown.

tests/test_graphical_units/test_geometry.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,25 @@ def test_CurvedArrowCustomTip(scene):
247247
tip_shape_end=ArrowSquareFilledTip,
248248
)
249249
scene.add(arrow, double_arrow)
250+
251+
252+
@frames_comparison
253+
def test_LabeledLine(scene):
254+
line = LabeledLine(
255+
label="0.5",
256+
label_position=0.8,
257+
font_size=20,
258+
label_color=WHITE,
259+
label_frame=True,
260+
start=LEFT + DOWN,
261+
end=RIGHT + UP,
262+
)
263+
scene.add(line)
264+
265+
266+
@frames_comparison
267+
def test_LabeledArrow(scene):
268+
l_arrow = LabeledArrow(
269+
"0.5", start=LEFT * 3, end=RIGHT * 3 + UP * 2, label_position=0.5, font_size=15
270+
)
271+
scene.add(l_arrow)

0 commit comments

Comments
 (0)