Skip to content

Two New Classes in geometry module: :class:.LabeledLine and :class:.LabeledArrow #3264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5a007d9
feat: added two new classes LabeledLine and LabeledArrow
andresberejnoi Jun 11, 2023
0e0ca66
test: added tests for new LabeledLine and LabeledArrow for the geomet…
andresberejnoi Jun 11, 2023
0993475
feat: added new class names to '__all__' module attribute
andresberejnoi Jun 16, 2023
ad1309c
Merge remote-tracking branch 'origin/main' into labeled_line_and_arrow
andresberejnoi Jun 20, 2023
de8ecb3
fix: added missing import line for new classes
andresberejnoi Jun 20, 2023
e6bd732
fix: removed import lines causing cyclic import error
andresberejnoi Jun 20, 2023
5f92410
new file containing two new classes
andresberejnoi Jun 25, 2023
fbeead5
modified files to correctly load new classes when manim is imported
andresberejnoi Jun 25, 2023
994f95a
commented out new classes in line.py
andresberejnoi Jun 27, 2023
0553b6c
created control frames for LabeledLine and LabeledArrow
andresberejnoi Jun 27, 2023
a5208e6
removed commented out classes
andresberejnoi Jun 27, 2023
0e470e9
Merge branch 'main' into labeled_line_and_arrow
andresberejnoi Jul 5, 2023
442debc
removed unused import of 'Colors'
andresberejnoi Jul 8, 2023
5b0991d
Merge branch 'main' into labeled_line_and_arrow
andresberejnoi Jul 13, 2023
b8f0eba
Merge branch 'main' into labeled_line_and_arrow
andresberejnoi Jul 18, 2023
1213166
Merge branch 'main' into labeled_line_and_arrow
jsonvillanueva Jul 25, 2023
2806d4a
Update manim/mobject/geometry/labeled_shapes.py
andresberejnoi Jul 27, 2023
54ed3f7
Update manim/mobject/geometry/labeled_shapes.py
andresberejnoi Jul 27, 2023
d7ec30a
Update manim/mobject/geometry/labeled_shapes.py
andresberejnoi Jul 27, 2023
7acdf0f
Update manim/mobject/geometry/labeled_shapes.py
andresberejnoi Jul 27, 2023
1feaa9e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 27, 2023
14effa2
Update __init__.py
andresberejnoi Jul 27, 2023
cd83563
Rename labeled_shapes.py to labeled.py
andresberejnoi Jul 27, 2023
f55eddf
Update __init__.py
andresberejnoi Jul 27, 2023
f264bd7
Update manim/mobject/geometry/labeled.py
andresberejnoi Jul 27, 2023
0b60530
Update labeled.py
andresberejnoi Jul 27, 2023
b7062cf
Update manim/mobject/geometry/labeled.py
andresberejnoi Jul 27, 2023
def8218
Update tests/test_graphical_units/test_geometry.py
andresberejnoi Jul 27, 2023
5b428e8
Revert changes related to WHITE and it's import
jsonvillanueva Jul 27, 2023
4577c59
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 27, 2023
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
1 change: 1 addition & 0 deletions manim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from .mobject.frame import *
from .mobject.geometry.arc import *
from .mobject.geometry.boolean_ops import *
from .mobject.geometry.labeled import *
from .mobject.geometry.line import *
from .mobject.geometry.polygram import *
from .mobject.geometry.shape_matchers import *
Expand Down
1 change: 1 addition & 0 deletions manim/mobject/geometry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

~arc
~boolean_ops
~labeled
~line
~polygram
~shape_matchers
Expand Down
153 changes: 153 additions & 0 deletions manim/mobject/geometry/labeled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
r"""Mobjects that inherit from lines and contain a label along the length."""

from __future__ import annotations

__all__ = ["LabeledLine", "LabeledArrow"]

from manim.constants import *
from manim.mobject.geometry.line import Arrow, Line
from manim.mobject.geometry.shape_matchers import (
BackgroundRectangle,
SurroundingRectangle,
)
from manim.mobject.text.tex_mobject import MathTex, Tex
from manim.mobject.text.text_mobject import Text
from manim.utils.color import WHITE, Color


class LabeledLine(Line):
"""Constructs a line containing a label box somewhere along its length.

Parameters
----------
label : str | Tex | MathTex | Text
Label that will be displayed on the line.
label_position : float | optional
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.
font_size : float | optional
Control font size for the label. This parameter is only used when `label` is of type `str`.
label_color: numpy.ndarray | optional
The color of the label's text. This parameter is only used when `label` is of type `str`.
label_frame : Bool | optional
Add a `SurroundingRectangle` frame to the label box.
frame_fill_color : numpy.ndarray | optional
Background color to fill the label box. If no value is provided, the background color of the canvas will be used.
frame_fill_opacity : float | optional
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.

.. seealso::
:class:`LabeledArrow`

Examples
--------
.. manim:: LabeledLineExample
:save_last_frame:

class LabeledLineExample(Scene):
def construct(self):
line = LabeledLine(
label = '0.5',
label_position = 0.8,
font_size = 20,
label_color = WHITE,
label_frame = True,

start=LEFT+DOWN,
end=RIGHT+UP)


line.set_length(line.get_length() * 2)
self.add(line)
"""

def __init__(
self,
label: str | Tex | MathTex | Text,
label_position: float = 0.5,
font_size: float = DEFAULT_FONT_SIZE,
label_color: Color | str | None = WHITE,
label_frame: bool = True,
frame_fill_color: Color | str | None = None,
frame_fill_opacity: float = 1,
*args,
**kwargs,
) -> None:
if isinstance(label, str):
from manim import MathTex

rendered_label = MathTex(label, color=label_color, font_size=font_size)
else:
rendered_label = label

super().__init__(*args, **kwargs)

# calculating the vector for the label position
line_start, line_end = self.get_start_and_end()
new_vec = (line_end - line_start) * label_position
label_coords = line_start + new_vec

# rendered_label.move_to(self.get_vector() * label_position)
rendered_label.move_to(label_coords)

box = BackgroundRectangle(
rendered_label,
buff=0.05,
color=frame_fill_color,
fill_opacity=frame_fill_opacity,
stroke_width=0.5,
)
self.add(box)

if label_frame:
box_frame = SurroundingRectangle(
rendered_label, buff=0.05, color=label_color, stroke_width=0.5
)

self.add(box_frame)

self.add(rendered_label)


class LabeledArrow(LabeledLine, Arrow):
"""Constructs an arrow containing a label box somewhere along its length.
This class inherits its label properties from `LabeledLine`, so the main parameters controlling it are the same.

Parameters
----------
label : str | Tex | MathTex | Text
Label that will be displayed on the line.
label_position : float | optional
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.
font_size : float | optional
Control font size for the label. This parameter is only used when `label` is of type `str`.
label_color: numpy.ndarray | optional
The color of the label's text. This parameter is only used when `label` is of type `str`.
label_frame : Bool | optional
Add a `SurroundingRectangle` frame to the label box.
frame_fill_color : numpy.ndarray | optional
Background color to fill the label box. If no value is provided, the background color of the canvas will be used.
frame_fill_opacity : float | optional
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.


.. seealso::
:class:`LabeledLine`

Examples
--------
.. manim:: LabeledArrowExample
:save_last_frame:

class LabeledArrowExample(Scene):
def construct(self):
l_arrow = LabeledArrow("0.5", start=LEFT*3, end=RIGHT*3 + UP*2, label_position=0.5)

self.add(l_arrow)
"""

def __init__(
self,
*args,
**kwargs,
) -> None:
super().__init__(*args, **kwargs)
Binary file not shown.
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/test_graphical_units/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,25 @@ def test_CurvedArrowCustomTip(scene):
tip_shape_end=ArrowSquareFilledTip,
)
scene.add(arrow, double_arrow)


@frames_comparison
def test_LabeledLine(scene):
line = LabeledLine(
label="0.5",
label_position=0.8,
font_size=20,
label_color=WHITE,
label_frame=True,
start=LEFT + DOWN,
end=RIGHT + UP,
)
scene.add(line)


@frames_comparison
def test_LabeledArrow(scene):
l_arrow = LabeledArrow(
"0.5", start=LEFT * 3, end=RIGHT * 3 + UP * 2, label_position=0.5, font_size=15
)
scene.add(l_arrow)