Prevent round_corners() failures for collinear vertices - #4906
Merged
behackl merged 1 commit intoAug 1, 2026
Merged
Conversation
Fixes ManimCommunity#3052 When three consecutive vertices of a Polygram are collinear, the turn at a corner is (near) 180 degrees, which makes tan(angle / 2) diverge to infinity. This previously produced either astronomically large coordinates (default round_corners) or an 8.70 PiB MemoryError when anchors were evenly distributed. Clamp cut_off_length to at most half the shorter adjacent edge so the rounded arc endpoints can never be pushed to infinity.
behackl
approved these changes
Aug 1, 2026
behackl
left a comment
Member
There was a problem hiding this comment.
Looks great, thanks for contributing and including the detailed comments!
round_corners() failures for collinear vertices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3052.
Polygram.round_cornerscomputes the distance between a corner vertex and the start of the rounded arc as:When three consecutive vertices are collinear, one corner forms a (near) 180 degree turn, so
tan(angle / 2)diverges to infinity. This previously produced either astronomically large coordinates (defaultround_corners, e.g. ~2.4e15) or an8.70 PiBMemoryErrorwhen anchors were evenly distributed.This PR clamps
cut_off_lengthto at most half the shorter adjacent edge vianp.clip, so the rounded arc endpoints can never be pushed to infinity. The degenerate collinear case now yields a finite, valid mobject.Changes
manim/mobject/geometry/polygram.py: clampcut_off_lengthto[-max_cut_off, max_cut_off]wheremax_cut_off = min(|vect1|, |vect2|) / 2.tests/module/mobject/geometry/test_unit_geometry.py: addtest_round_corners_collinear_points_stays_finitecovering bothround_cornersmodes.Test plan
pytest tests/module/mobject/geometry/test_unit_geometry.py -k round_cornerspasses; full file 13 passed locally.Notes
Earlier attempts #4835 and #4873 were closed; this PR applies the same validated clamp direction with a regression test and a clean commit.