Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Allow :class:`.SurroundingRectangle` to accept multiple Mobjects (#3964)

Nemo25109f1f239f10dc
diff --git a/docs/source/examples.rst b/docs/source/examples.rst
index 6d39027bf..3213c3516 100644
--- a/docs/source/examples.rst
+++ b/docs/source/examples.rst
@@ -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
diff --git a/manim/animation/indication.py b/manim/animation/indication.py
index 8060737fa..f931491b3 100644
--- a/manim/animation/indication.py
+++ b/manim/animation/indication.py
@@ -608,8 +608,8 @@ class Circumscribe(Succession):
if shape is Rectangle:
frame = SurroundingRectangle(
mobject,
- color,
- buff,
+ color=color,
+ buff=buff,
stroke_width=stroke_width,
)
elif shape is Circle:
diff --git a/manim/mobject/geometry/shape_matchers.py b/manim/mobject/geometry/shape_matchers.py
index b546dfb4f..86afb58db 100644
--- a/manim/mobject/geometry/shape_matchers.py
+++ b/manim/mobject/geometry/shape_matchers.py
@@ -8,8 +8,15 @@ from typing import Any
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
@@ -43,21 +50,29 @@ class SurroundingRectangle(RoundedRectangle):
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):
@@ -87,7 +102,7 @@ class BackgroundRectangle(SurroundingRectangle):
def __init__(
self,
- mobject: Mobject,
+ *mobjects: Mobject,
color: ParsableManimColor | None = None,
stroke_width: float = 0,
stroke_opacity: float = 0,
@@ -99,7 +114,7 @@ class BackgroundRectangle(SurroundingRectangle):
color = config.background_color
super().__init__(
- mobject,
+ *mobjects,
color=color,
stroke_width=stroke_width,
stroke_opacity=stroke_opacity,
diff --git a/tests/module/mobject/geometry/test_unit_geometry.py b/tests/module/mobject/geometry/test_unit_geometry.py
index 53ee79aff..4f5403a14 100644
--- a/tests/module/mobject/geometry/test_unit_geometry.py
+++ b/tests/module/mobject/geometry/test_unit_geometry.py
@@ -4,7 +4,7 @@ import logging
import numpy as np
-from manim import BackgroundRectangle, Circle, Sector, Square
+from manim import BackgroundRectangle, Circle, Sector, Square, SurroundingRectangle
logger = logging.getLogger(__name__)
@@ -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")