-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
jsonvillanueva
merged 30 commits into
ManimCommunity:main
from
andresberejnoi:labeled_line_and_arrow
Jul 31, 2023
Merged
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 0e0ca66
test: added tests for new LabeledLine and LabeledArrow for the geomet…
andresberejnoi 0993475
feat: added new class names to '__all__' module attribute
andresberejnoi ad1309c
Merge remote-tracking branch 'origin/main' into labeled_line_and_arrow
andresberejnoi de8ecb3
fix: added missing import line for new classes
andresberejnoi e6bd732
fix: removed import lines causing cyclic import error
andresberejnoi 5f92410
new file containing two new classes
andresberejnoi fbeead5
modified files to correctly load new classes when manim is imported
andresberejnoi 994f95a
commented out new classes in line.py
andresberejnoi 0553b6c
created control frames for LabeledLine and LabeledArrow
andresberejnoi a5208e6
removed commented out classes
andresberejnoi 0e470e9
Merge branch 'main' into labeled_line_and_arrow
andresberejnoi 442debc
removed unused import of 'Colors'
andresberejnoi 5b0991d
Merge branch 'main' into labeled_line_and_arrow
andresberejnoi b8f0eba
Merge branch 'main' into labeled_line_and_arrow
andresberejnoi 1213166
Merge branch 'main' into labeled_line_and_arrow
jsonvillanueva 2806d4a
Update manim/mobject/geometry/labeled_shapes.py
andresberejnoi 54ed3f7
Update manim/mobject/geometry/labeled_shapes.py
andresberejnoi d7ec30a
Update manim/mobject/geometry/labeled_shapes.py
andresberejnoi 7acdf0f
Update manim/mobject/geometry/labeled_shapes.py
andresberejnoi 1feaa9e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 14effa2
Update __init__.py
andresberejnoi cd83563
Rename labeled_shapes.py to labeled.py
andresberejnoi f55eddf
Update __init__.py
andresberejnoi f264bd7
Update manim/mobject/geometry/labeled.py
andresberejnoi 0b60530
Update labeled.py
andresberejnoi b7062cf
Update manim/mobject/geometry/labeled.py
andresberejnoi def8218
Update tests/test_graphical_units/test_geometry.py
andresberejnoi 5b428e8
Revert changes related to WHITE and it's import
jsonvillanueva 4577c59
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
~arc | ||
~boolean_ops | ||
~labeled | ||
~line | ||
~polygram | ||
~shape_matchers | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
andresberejnoi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.