Skip to content

Commit c4dc0ea

Browse files
nubDotDevbehackl
andauthored
Allow passing a tuple to buff in SurroundingRectangle to specify buffer in x and y direction independently (#4390)
* Give SurroundingRectangle 2D buffer * add test for SurroundingRectangle buff parameter --------- Co-authored-by: Benjamin Hackl <[email protected]>
1 parent e822de0 commit c4dc0ea

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

manim/mobject/geometry/shape_matchers.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(
5353
self,
5454
*mobjects: Mobject,
5555
color: ParsableManimColor = YELLOW,
56-
buff: float = SMALL_BUFF,
56+
buff: float | tuple[float, float] = SMALL_BUFF,
5757
corner_radius: float = 0.0,
5858
**kwargs: Any,
5959
) -> None:
@@ -64,11 +64,17 @@ def __init__(
6464
"Expected all inputs for parameter mobjects to be a Mobjects"
6565
)
6666

67+
if isinstance(buff, tuple):
68+
buff_x = buff[0]
69+
buff_y = buff[1]
70+
else:
71+
buff_x = buff_y = buff
72+
6773
group = Group(*mobjects)
6874
super().__init__(
6975
color=color,
70-
width=group.width + 2 * buff,
71-
height=group.height + 2 * buff,
76+
width=group.width + 2 * buff_x,
77+
height=group.height + 2 * buff_y,
7278
corner_radius=corner_radius,
7379
**kwargs,
7480
)
@@ -108,7 +114,7 @@ def __init__(
108114
stroke_width: float = 0,
109115
stroke_opacity: float = 0,
110116
fill_opacity: float = 0.75,
111-
buff: float = 0,
117+
buff: float | tuple[float, float] = 0,
112118
**kwargs: Any,
113119
) -> None:
114120
if color is None:

tests/module/mobject/geometry/test_unit_geometry.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ def test_SurroundingRectangle():
116116
assert sr.get_fill_opacity() == 0.42
117117

118118

119+
def test_SurroundingRectangle_buff():
120+
sq = Square()
121+
rect1 = SurroundingRectangle(sq, buff=1)
122+
assert rect1.width == sq.width + 2
123+
assert rect1.height == sq.height + 2
124+
125+
rect2 = SurroundingRectangle(sq, buff=(1, 2))
126+
assert rect2.width == sq.width + 2
127+
assert rect2.height == sq.height + 4
128+
129+
119130
def test_BackgroundRectangle(manim_caplog):
120131
circle = Circle()
121132
square = Square()

0 commit comments

Comments
 (0)