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>
This commit is contained in:
Nemo2510 2024-10-30 10:30:32 +11:00 committed by GitHub
commit 9f1f239f10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 14 deletions

View file

@ -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

View file

@ -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:

View file

@ -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,

View file

@ -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")