Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Allow passing a tuple to `buff` in `SurroundingRectangle` to specify buffer in x and y direction independently (#4390)

nubDotDevc4dc0ea86da5
diff --git a/manim/mobject/geometry/shape_matchers.py b/manim/mobject/geometry/shape_matchers.py
index 892f91259..dd696881f 100644
--- a/manim/mobject/geometry/shape_matchers.py
+++ b/manim/mobject/geometry/shape_matchers.py
@@ -53,7 +53,7 @@ class SurroundingRectangle(RoundedRectangle):
self,
*mobjects: Mobject,
color: ParsableManimColor = YELLOW,
- buff: float = SMALL_BUFF,
+ buff: float | tuple[float, float] = SMALL_BUFF,
corner_radius: float = 0.0,
**kwargs: Any,
) -> None:
@@ -64,11 +64,17 @@ class SurroundingRectangle(RoundedRectangle):
"Expected all inputs for parameter mobjects to be a Mobjects"
)
+ if isinstance(buff, tuple):
+ buff_x = buff[0]
+ buff_y = buff[1]
+ else:
+ buff_x = buff_y = buff
+
group = Group(*mobjects)
super().__init__(
color=color,
- width=group.width + 2 * buff,
- height=group.height + 2 * buff,
+ width=group.width + 2 * buff_x,
+ height=group.height + 2 * buff_y,
corner_radius=corner_radius,
**kwargs,
)
@@ -108,7 +114,7 @@ class BackgroundRectangle(SurroundingRectangle):
stroke_width: float = 0,
stroke_opacity: float = 0,
fill_opacity: float = 0.75,
- buff: float = 0,
+ buff: float | tuple[float, float] = 0,
**kwargs: Any,
) -> None:
if color is None:
diff --git a/tests/module/mobject/geometry/test_unit_geometry.py b/tests/module/mobject/geometry/test_unit_geometry.py
index 210af0fdf..a7ff5e2c4 100644
--- a/tests/module/mobject/geometry/test_unit_geometry.py
+++ b/tests/module/mobject/geometry/test_unit_geometry.py
@@ -116,6 +116,17 @@ def test_SurroundingRectangle():
assert sr.get_fill_opacity() == 0.42
+def test_SurroundingRectangle_buff():
+ sq = Square()
+ rect1 = SurroundingRectangle(sq, buff=1)
+ assert rect1.width == sq.width + 2
+ assert rect1.height == sq.height + 2
+
+ rect2 = SurroundingRectangle(sq, buff=(1, 2))
+ assert rect2.width == sq.width + 2
+ assert rect2.height == sq.height + 4
+
+
def test_BackgroundRectangle(manim_caplog):
circle = Circle()
square = Square()