Skip to content

Commit 9f1f239

Browse files
Boris-Filinpre-commit-ci[bot]JasonGrace2282
authored
Allow :class:.SurroundingRectangle to accept multiple Mobjects (#3964)
* 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 <110117391+JasonGrace2282@users.noreply.github.com> * Remove redundant argument from test_SurroundingRectangle Co-authored-by: Aarush Deshpande <110117391+JasonGrace2282@users.noreply.github.com> * 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 <110117391+JasonGrace2282@users.noreply.github.com> Co-authored-by: JasonGrace2282 <aarush.deshpande@gmail.com>
1 parent 2570a25 commit 9f1f239

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

docs/source/examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Basic Concepts
9090
[[i * 256 / n for i in range(0, n)] for _ in range(0, n)]
9191
)
9292
image = ImageMobject(imageArray).scale(2)
93-
image.background_rectangle = SurroundingRectangle(image, GREEN)
93+
image.background_rectangle = SurroundingRectangle(image, color=GREEN)
9494
self.add(image, image.background_rectangle)
9595

9696
.. manim:: BooleanOperations

manim/animation/indication.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ def __init__(
608608
if shape is Rectangle:
609609
frame = SurroundingRectangle(
610610
mobject,
611-
color,
612-
buff,
611+
color=color,
612+
buff=buff,
613613
stroke_width=stroke_width,
614614
)
615615
elif shape is Circle:

manim/mobject/geometry/shape_matchers.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
88

99
from typing_extensions import Self
1010

11-
from manim import config, logger
12-
from manim.constants import *
11+
from manim import logger
12+
from manim._config import config
13+
from manim.constants import (
14+
DOWN,
15+
LEFT,
16+
RIGHT,
17+
SMALL_BUFF,
18+
UP,
19+
)
1320
from manim.mobject.geometry.line import Line
1421
from manim.mobject.geometry.polygram import RoundedRectangle
1522
from manim.mobject.mobject import Mobject
@@ -43,21 +50,29 @@ def construct(self):
4350

4451
def __init__(
4552
self,
46-
mobject: Mobject,
53+
*mobjects: Mobject,
4754
color: ParsableManimColor = YELLOW,
4855
buff: float = SMALL_BUFF,
4956
corner_radius: float = 0.0,
5057
**kwargs: Any,
5158
) -> None:
59+
from manim.mobject.mobject import Group
60+
61+
if not all(isinstance(mob, Mobject) for mob in mobjects):
62+
raise TypeError(
63+
"Expected all inputs for parameter mobjects to be a Mobjects"
64+
)
65+
66+
group = Group(*mobjects)
5267
super().__init__(
5368
color=color,
54-
width=mobject.width + 2 * buff,
55-
height=mobject.height + 2 * buff,
69+
width=group.width + 2 * buff,
70+
height=group.height + 2 * buff,
5671
corner_radius=corner_radius,
5772
**kwargs,
5873
)
5974
self.buff = buff
60-
self.move_to(mobject)
75+
self.move_to(group)
6176

6277

6378
class BackgroundRectangle(SurroundingRectangle):
@@ -87,7 +102,7 @@ def construct(self):
87102

88103
def __init__(
89104
self,
90-
mobject: Mobject,
105+
*mobjects: Mobject,
91106
color: ParsableManimColor | None = None,
92107
stroke_width: float = 0,
93108
stroke_opacity: float = 0,
@@ -99,7 +114,7 @@ def __init__(
99114
color = config.background_color
100115

101116
super().__init__(
102-
mobject,
117+
*mobjects,
103118
color=color,
104119
stroke_width=stroke_width,
105120
stroke_opacity=stroke_opacity,

tests/module/mobject/geometry/test_unit_geometry.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import numpy as np
66

7-
from manim import BackgroundRectangle, Circle, Sector, Square
7+
from manim import BackgroundRectangle, Circle, Sector, Square, SurroundingRectangle
88

99
logger = logging.getLogger(__name__)
1010

@@ -15,9 +15,18 @@ def test_get_arc_center():
1515
)
1616

1717

18+
def test_SurroundingRectangle():
19+
circle = Circle()
20+
square = Square()
21+
sr = SurroundingRectangle(circle, square)
22+
sr.set_style(fill_opacity=0.42)
23+
assert sr.get_fill_opacity() == 0.42
24+
25+
1826
def test_BackgroundRectangle(manim_caplog):
19-
c = Circle()
20-
bg = BackgroundRectangle(c)
27+
circle = Circle()
28+
square = Square()
29+
bg = BackgroundRectangle(circle, square)
2130
bg.set_style(fill_opacity=0.42)
2231
assert bg.get_fill_opacity() == 0.42
2332
bg.set_style(fill_opacity=1, hello="world")

0 commit comments

Comments
 (0)