Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix ``Angle`` crash on parallel/collinear lines (#1930) (#4907)

Nefelibata33d8733e6512
diff --git a/manim/mobject/geometry/line.py b/manim/mobject/geometry/line.py
index 2cd7aff80..516ef8f23 100644
--- a/manim/mobject/geometry/line.py
+++ b/manim/mobject/geometry/line.py
@@ -993,10 +993,27 @@ class Angle(VMobject, metaclass=ConvertToOpenGL):
self.quadrant = quadrant
self.dot_distance = dot_distance
self.elbow = elbow
- inter = line_intersection(
- [line1.get_start(), line1.get_end()],
- [line2.get_start(), line2.get_end()],
- )
+ try:
+ inter = line_intersection(
+ [line1.get_start(), line1.get_end()],
+ [line2.get_start(), line2.get_end()],
+ )
+ except ValueError:
+ lines_are_in_xy_plane = all(
+ point[2] == 0
+ for line in (line1, line2)
+ for point in (line.get_start(), line.get_end())
+ )
+ directions_are_parallel = (
+ np.cross(line1.get_vector(), line2.get_vector())[2] == 0
+ )
+ if not (lines_are_in_xy_plane and directions_are_parallel):
+ raise
+ # When the two lines are parallel or collinear there is no
+ # unique intersection point. Rather than raising, Angle
+ # becomes an empty Mobject (see issue #1930).
+ self.angle_value = 0.0
+ return
if radius is None:
if quadrant[0] == 1:
diff --git a/tests/module/mobject/geometry/test_unit_geometry.py b/tests/module/mobject/geometry/test_unit_geometry.py
index 1f0f5f527..82e4a176c 100644
--- a/tests/module/mobject/geometry/test_unit_geometry.py
+++ b/tests/module/mobject/geometry/test_unit_geometry.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import logging
import numpy as np
+import pytest
from manim import (
DEGREES,
@@ -12,6 +13,7 @@ from manim import (
ORIGIN,
RIGHT,
UP,
+ Angle,
BackgroundRectangle,
Circle,
Line,
@@ -266,6 +268,34 @@ def test_Circle_point_at_angle():
np.testing.assert_array_almost_equal(p_reflected_0, reflected_start, decimal=5)
+def test_angle_parallel_lines_returns_empty():
+ # Regression test for https://github.com/ManimCommunity/manim/issues/1930
+ # When two lines are parallel or collinear, line_intersection raises
+ # ValueError. Angle should gracefully become an empty Mobject instead
+ # of propagating the error.
+ # Collinear lines (infinitely many intersections)
+ l1 = Line(LEFT, RIGHT)
+ l2 = Line(LEFT * 0.5, RIGHT * 0.5)
+ angle = Angle(l1, l2)
+ assert not angle.has_points()
+ assert angle.get_value() == 0
+
+ # Parallel but non-collinear lines (no intersection)
+ l3 = Line(UP, UP + RIGHT)
+ l4 = Line(DOWN, DOWN + RIGHT)
+ angle2 = Angle(l3, l4)
+ assert not angle2.has_points()
+ assert angle2.get_value() == 0
+
+
+def test_angle_non_xy_lines_raise():
+ l1 = Line([0, 0, 1], [1, 0, 1])
+ l2 = Line([0, 0, 1], [0, 1, 1])
+
+ with pytest.raises(ValueError, match="xy-plane"):
+ Angle(l1, l2)
+
+
def test_round_corners_collinear_points_stays_finite():
# Regression test for https://github.com/ManimCommunity/manim/issues/3052
# Collinear vertices create a (near) 180 degree turn at a corner, which