Skip to content

Commit

Permalink
Allow :class:.SurroundingRectangle to accept multiple Mobjects (#3964)
Browse files Browse the repository at this point in the history
* Implement SurroundingRectangle.multiple and BackgroundRectangle.multiple

* Integrate SurroundingRectangle constructor for multiple objects into __init__

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* SurroundingRectangle now takes multiple Mobjects as positional args

* Add tests for multiple Mobjects in SurroundingRectangle

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix example that was not using keyword args for SurroundingRectangle

* Remove duplicate code from BackgroundRectangle

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add typing to manim argument of SurroundingRecrabgle constructor

Co-authored-by: Aarush Deshpande <[email protected]>

* Remove redundant argument from test_SurroundingRectangle

Co-authored-by: Aarush Deshpande <[email protected]>

* Remove type check from SurroundingRectangle

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix missing line issue

* resolve merge conflict

* Fix Group import

* Move Group import into the body of SurroundingRectangle

* Return type checking to SurroundingRectangle

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* small change to error message

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix missing imports

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Aarush Deshpande <[email protected]>
Co-authored-by: JasonGrace2282 <[email protected]>
  • Loading branch information
4 people authored Oct 29, 2024
1 parent 2570a25 commit 9f1f239
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Basic Concepts
[[i * 256 / n for i in range(0, n)] for _ in range(0, n)]
)
image = ImageMobject(imageArray).scale(2)
image.background_rectangle = SurroundingRectangle(image, GREEN)
image.background_rectangle = SurroundingRectangle(image, color=GREEN)
self.add(image, image.background_rectangle)

.. manim:: BooleanOperations
Expand Down
4 changes: 2 additions & 2 deletions manim/animation/indication.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,8 @@ def __init__(
if shape is Rectangle:
frame = SurroundingRectangle(
mobject,
color,
buff,
color=color,
buff=buff,
stroke_width=stroke_width,
)
elif shape is Circle:
Expand Down
31 changes: 23 additions & 8 deletions manim/mobject/geometry/shape_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@

from typing_extensions import Self

from manim import config, logger
from manim.constants import *
from manim import logger
from manim._config import config
from manim.constants import (
DOWN,
LEFT,
RIGHT,
SMALL_BUFF,
UP,
)
from manim.mobject.geometry.line import Line
from manim.mobject.geometry.polygram import RoundedRectangle
from manim.mobject.mobject import Mobject
Expand Down Expand Up @@ -43,21 +50,29 @@ def construct(self):

def __init__(
self,
mobject: Mobject,
*mobjects: Mobject,
color: ParsableManimColor = YELLOW,
buff: float = SMALL_BUFF,
corner_radius: float = 0.0,
**kwargs: Any,
) -> None:
from manim.mobject.mobject import Group

if not all(isinstance(mob, Mobject) for mob in mobjects):
raise TypeError(
"Expected all inputs for parameter mobjects to be a Mobjects"
)

group = Group(*mobjects)
super().__init__(
color=color,
width=mobject.width + 2 * buff,
height=mobject.height + 2 * buff,
width=group.width + 2 * buff,
height=group.height + 2 * buff,
corner_radius=corner_radius,
**kwargs,
)
self.buff = buff
self.move_to(mobject)
self.move_to(group)


class BackgroundRectangle(SurroundingRectangle):
Expand Down Expand Up @@ -87,7 +102,7 @@ def construct(self):

def __init__(
self,
mobject: Mobject,
*mobjects: Mobject,
color: ParsableManimColor | None = None,
stroke_width: float = 0,
stroke_opacity: float = 0,
Expand All @@ -99,7 +114,7 @@ def __init__(
color = config.background_color

super().__init__(
mobject,
*mobjects,
color=color,
stroke_width=stroke_width,
stroke_opacity=stroke_opacity,
Expand Down
15 changes: 12 additions & 3 deletions tests/module/mobject/geometry/test_unit_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np

from manim import BackgroundRectangle, Circle, Sector, Square
from manim import BackgroundRectangle, Circle, Sector, Square, SurroundingRectangle

logger = logging.getLogger(__name__)

Expand All @@ -15,9 +15,18 @@ def test_get_arc_center():
)


def test_SurroundingRectangle():
circle = Circle()
square = Square()
sr = SurroundingRectangle(circle, square)
sr.set_style(fill_opacity=0.42)
assert sr.get_fill_opacity() == 0.42


def test_BackgroundRectangle(manim_caplog):
c = Circle()
bg = BackgroundRectangle(c)
circle = Circle()
square = Square()
bg = BackgroundRectangle(circle, square)
bg.set_style(fill_opacity=0.42)
assert bg.get_fill_opacity() == 0.42
bg.set_style(fill_opacity=1, hello="world")
Expand Down

0 comments on commit 9f1f239

Please sign in to comment.