A community-maintained Python framework for creating mathematical animations.
Added support for individualized radius values in :meth:`.Polygram.round_corners` (#3155)
coreyp180795d836143
diff --git a/manim/mobject/geometry/polygram.py b/manim/mobject/geometry/polygram.pyindex f96557525..daaa67534 100644--- a/manim/mobject/geometry/polygram.py+++ b/manim/mobject/geometry/polygram.py@@ -15,6 +15,7 @@ __all__ = ["Cutout",]+from math import ceilfrom typing import Iterable, Sequenceimport numpy as np@@ -133,18 +134,52 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):return np.array(vertex_groups)- def round_corners(self, radius: float = 0.5):+ def round_corners(+ self,+ radius: float | list[float] = 0.5,+ evenly_distribute_anchors: bool = False,+ components_per_rounded_corner: int = 2,+ ):"""Rounds off the corners of the :class:`Polygram`.Parameters----------radiusThe curvature of the corners of the :class:`Polygram`.+ evenly_distribute_anchors+ Break long line segments into proportionally-sized segments.+ components_per_rounded_corner+ The number of points used to represent the rounded corner curve... seealso:::class:`.~RoundedRectangle`+ .. note::+ If `radius` is supplied as a single value, then the same radius+ will be applied to all corners. If `radius` is a list, then the+ individual values will be applied sequentially, with the first+ corner receiving `radius[0]`, the second corner receiving+ `radius[1]`, etc. The radius list will be repeated as necessary.++ The `components_per_rounded_corner` value is provided so that the+ fidelity of the rounded corner may be fine-tuned as needed. 2 is+ an appropriate value for most shapes, however a larger value may be+ need if the rounded corner is particularly large. 2 is the minimum+ number allowed, representing the start and end of the curve. 3 will+ result in a start, middle, and end point, meaning 2 curves will be+ generated.++ The option to `evenly_distribute_anchors` is provided so that the+ line segments (the part part of each line remaining after rounding+ off the corners) can be subdivided to a density similar to that of+ the average density of the rounded corners. This may be desirable+ in situations in which an even distribution of curves is desired+ for use in later transformation animations. Be aware, though, that+ enabling this option can result in an an object containing+ significantly more points than the original, especially when the+ rounded corner curves are small.+Examples--------.. manim:: PolygramRoundCorners@@ -169,7 +204,17 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):for vertices in self.get_vertex_groups():arcs = []- for v1, v2, v3 in adjacent_n_tuples(vertices, 3):++ # Repeat the radius list as necessary in order to provide a radius+ # for each vertex.+ if isinstance(radius, (int, float)):+ radius_list = [radius] * len(vertices)+ else:+ radius_list = radius * ceil(len(vertices) / len(radius))++ for currentRadius, (v1, v2, v3) in zip(+ radius_list, adjacent_n_tuples(vertices, 3)+ ):vect1 = v2 - v1vect2 = v3 - v2unit_vect1 = normalize(vect1)@@ -177,10 +222,10 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):angle = angle_between_vectors(vect1, vect2)# Negative radius gives concave curves- angle *= np.sign(radius)+ angle *= np.sign(currentRadius)# Distance between vertex and start of the arc- cut_off_length = radius * np.tan(angle / 2)+ cut_off_length = currentRadius * np.tan(angle / 2)# Determines counterclockwise vs. clockwisesign = np.sign(np.cross(vect1, vect2)[2])@@ -189,9 +234,24 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):v2 - unit_vect1 * cut_off_length,v2 + unit_vect2 * cut_off_length,angle=sign * angle,+ num_components=components_per_rounded_corner,)arcs.append(arc)+ if evenly_distribute_anchors:+ # Determine the average length of each curve+ nonZeroLengthArcs = [arc for arc in arcs if len(arc.points) > 4]+ if len(nonZeroLengthArcs):+ totalArcLength = sum(+ [arc.get_arc_length() for arc in nonZeroLengthArcs]+ )+ totalCurveCount = (+ sum([len(arc.points) for arc in nonZeroLengthArcs]) / 4+ )+ averageLengthPerCurve = totalArcLength / totalCurveCount+ else:+ averageLengthPerCurve = 1+# To ensure that we loop through starting with lastarcs = [arcs[-1], *arcs[:-1]]from manim.mobject.geometry.line import Line@@ -201,10 +261,11 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):line = Line(arc1.get_end(), arc2.get_start())- # Make sure anchors are evenly distributed- len_ratio = line.get_length() / arc1.get_arc_length()-- line.insert_n_curves(int(arc1.get_num_curves() * len_ratio))+ # Make sure anchors are evenly distributed, if necessary+ if evenly_distribute_anchors:+ line.insert_n_curves(+ ceil(line.get_length() / averageLengthPerCurve)+ )new_points.extend(line.points)@@ -633,7 +694,7 @@ class RoundedRectangle(Rectangle):self.add(rect_group)"""- def __init__(self, corner_radius: float = 0.5, **kwargs):+ def __init__(self, corner_radius: float | list[float] = 0.5, **kwargs):super().__init__(**kwargs)self.corner_radius = corner_radiusself.round_corners(self.corner_radius)diff --git a/tests/test_graphical_units/control_data/geometry/RoundedRectangle.npz b/tests/test_graphical_units/control_data/geometry/RoundedRectangle.npzindex f77b88221..98083a27c 100644Binary files a/tests/test_graphical_units/control_data/geometry/RoundedRectangle.npz and b/tests/test_graphical_units/control_data/geometry/RoundedRectangle.npz differ