Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix :class:`~.NumberPlane` bug with positive/negative `x_range`/`y_range` values. (#1744)

ccn463975455de5
diff --git a/manim/mobject/coordinate_systems.py b/manim/mobject/coordinate_systems.py
index de7747512..0af554310 100644
--- a/manim/mobject/coordinate_systems.py
+++ b/manim/mobject/coordinate_systems.py
@@ -1470,7 +1470,7 @@ class NumberPlane(Axes):
y_length: Optional[float] = None,
background_line_style: Optional[dict] = None,
faded_line_style: Optional[dict] = None,
- faded_line_ratio: Optional[float] = 1,
+ faded_line_ratio: int = 1,
make_smooth_after_applying_functions=True,
**kwargs,
):
@@ -1537,6 +1537,7 @@ class NumberPlane(Axes):
self.faded_line_style = style
self.background_lines, self.faded_lines = self.get_lines()
+
self.background_lines.set_style(
**self.background_line_style,
)
@@ -1565,22 +1566,29 @@ class NumberPlane(Axes):
self.x_axis.x_step,
self.faded_line_ratio,
)
+
y_lines1, y_lines2 = self.get_lines_parallel_to_axis(
y_axis,
x_axis,
self.y_axis.x_step,
self.faded_line_ratio,
)
+
+ # TODO this was added so that we can run tests on NumberPlane
+ # In the future these attributes will be tacked onto self.background_lines
+ self.x_lines = x_lines1
+ self.y_lines = y_lines1
lines1 = VGroup(*x_lines1, *y_lines1)
lines2 = VGroup(*x_lines2, *y_lines2)
+
return lines1, lines2
def get_lines_parallel_to_axis(
self,
- axis_parallel_to: Line,
- axis_perpendicular_to: Line,
+ axis_parallel_to: NumberLine,
+ axis_perpendicular_to: NumberLine,
freq: float,
- ratio_faded_lines: float,
+ ratio_faded_lines: int,
) -> Tuple[VGroup, VGroup]:
"""Generate a set of lines parallel to an axis.
@@ -1611,10 +1619,28 @@ class NumberPlane(Axes):
lines1 = VGroup()
lines2 = VGroup()
unit_vector_axis_perp_to = axis_perpendicular_to.get_unit_vector()
+
+ # min/max used in case range does not include 0. i.e. if (2,6):
+ # the range becomes (0,4), not (0,6), to produce the correct number of lines
ranges = (
- np.arange(0, axis_perpendicular_to.x_max, step),
- np.arange(0, axis_perpendicular_to.x_min, -step),
+ np.arange(
+ 0,
+ min(
+ axis_perpendicular_to.x_max - axis_perpendicular_to.x_min,
+ axis_perpendicular_to.x_max,
+ ),
+ step,
+ ),
+ np.arange(
+ 0,
+ max(
+ axis_perpendicular_to.x_min - axis_perpendicular_to.x_max,
+ axis_perpendicular_to.x_min,
+ ),
+ -step,
+ ),
)
+
for inputs in ranges:
for k, x in enumerate(inputs):
new_line = line.copy()
diff --git a/tests/test_coordinate_system.py b/tests/test_coordinate_system.py
index a4153a6bc..5c678fc50 100644
--- a/tests/test_coordinate_system.py
+++ b/tests/test_coordinate_system.py
@@ -1,3 +1,5 @@
+import math
+
import numpy as np
import pytest
@@ -41,3 +43,45 @@ def test_abstract_base_class():
"""Check that CoordinateSystem has some abstract methods."""
with pytest.raises(Exception):
CS().get_axes()
+
+
+def test_NumberPlane():
+ """Test that NumberPlane generates the correct number of lines when its ranges do not cross 0."""
+ pos_x_range = (0, 7)
+ neg_x_range = (-7, 0)
+
+ pos_y_range = (2, 6)
+ neg_y_range = (-6, -2)
+
+ x_vals = [0, 1.5, 2, 2.8, 4, 6.25]
+ y_vals = [2, 5, 4.25, 6, 4.5, 2.75]
+
+ testing_data = [
+ (pos_x_range, pos_y_range, x_vals, y_vals),
+ (pos_x_range, neg_y_range, x_vals, [-v for v in y_vals]),
+ (neg_x_range, pos_y_range, [-v for v in x_vals], y_vals),
+ (neg_x_range, neg_y_range, [-v for v in x_vals], [-v for v in y_vals]),
+ ]
+
+ for test_data in testing_data:
+
+ x_range, y_range, x_vals, y_vals = test_data
+
+ x_start, x_end = x_range
+ y_start, y_end = y_range
+
+ plane = NumberPlane(
+ x_range=x_range,
+ y_range=y_range,
+ # x_length = 7,
+ axis_config={"include_numbers": True},
+ )
+
+ # normally these values would be need to be added by one to pass since there's an
+ # overlapping pair of lines at the origin, but since these planes do not cross 0,
+ # this is not needed.
+ num_y_lines = math.ceil(x_end - x_start)
+ num_x_lines = math.floor(y_end - y_start)
+
+ assert len(plane.y_lines) == num_y_lines
+ assert len(plane.x_lines) == num_x_lines