Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix :meth:`~.input_to_graph_point` when passing a `line_graph` (#1994)

leleogere476ac4efb955
diff --git a/manim/mobject/coordinate_systems.py b/manim/mobject/coordinate_systems.py
index 0ab16e65a..5dd071e7f 100644
--- a/manim/mobject/coordinate_systems.py
+++ b/manim/mobject/coordinate_systems.py
@@ -33,7 +33,13 @@ from ..mobject.geometry import (
)
from ..mobject.number_line import NumberLine
from ..mobject.svg.tex_mobject import MathTex
-from ..mobject.types.vectorized_mobject import Mobject, VDict, VectorizedPoint, VGroup
+from ..mobject.types.vectorized_mobject import (
+ Mobject,
+ VDict,
+ VectorizedPoint,
+ VGroup,
+ VMobject,
+)
from ..utils.color import (
BLACK,
BLUE,
@@ -699,7 +705,11 @@ class CoordinateSystem:
graph.underlying_function = function
return graph
- def input_to_graph_point(self, x: float, graph: "ParametricFunction") -> np.ndarray:
+ def input_to_graph_point(
+ self,
+ x: float,
+ graph: Union["ParametricFunction", VMobject],
+ ) -> np.ndarray:
"""Returns the coordinates of the point on a ``graph`` corresponding to an ``x`` value.
Examples
@@ -730,6 +740,11 @@ class CoordinateSystem:
-------
:class:`np.ndarray`
The coordinates of the point on the :attr:`graph` corresponding to the :attr:`x` value.
+
+ Raises
+ ------
+ :exc:`ValueError`
+ When the target x is not in the range of the line graph.
"""
if hasattr(graph, "underlying_function"):
@@ -740,13 +755,15 @@ class CoordinateSystem:
0
],
target=x,
- lower_bound=self.x_range[0],
- upper_bound=self.x_range[1],
+ lower_bound=0,
+ upper_bound=1,
)
if alpha is not None:
return graph.point_from_proportion(alpha)
else:
- return None
+ raise ValueError(
+ f"x={x} not located in the range of the graph ([{self.p2c(graph.get_start())[0]}, {self.p2c(graph.get_end())[0]}])",
+ )
def i2gp(self, x: float, graph: "ParametricFunction") -> np.ndarray:
"""
diff --git a/manim/utils/simple_functions.py b/manim/utils/simple_functions.py
index 50f3025b9..37f11b0e3 100644
--- a/manim/utils/simple_functions.py
+++ b/manim/utils/simple_functions.py
@@ -95,9 +95,9 @@ def binary_search(function, target, lower_bound, upper_bound, tolerance=1e-4):
mh = np.mean([lh, rh])
lx, mx, rx = (function(h) for h in (lh, mh, rh))
if lx == target:
- return lx
+ return lh
if rx == target:
- return rx
+ return rh
if lx <= target and rx >= target:
if mx > target:
diff --git a/tests/test_coordinate_system.py b/tests/test_coordinate_system.py
index 4a94587d5..b2cb60eba 100644
--- a/tests/test_coordinate_system.py
+++ b/tests/test_coordinate_system.py
@@ -107,7 +107,14 @@ def test_coords_to_point():
def test_input_to_graph_point():
ax = Axes()
curve = ax.get_graph(lambda x: np.cos(x))
+ line_graph = ax.get_line_graph([1, 3, 5], [-1, 2, -2], add_vertex_dots=False)[
+ "line_graph"
+ ]
# move a square to PI on the cosine curve.
position = np.around(ax.input_to_graph_point(x=PI, graph=curve), decimals=4)
assert np.array_equal(position, (2.6928, -0.75, 0))
+
+ # test the line_graph implementation
+ position = np.around(ax.input_to_graph_point(x=PI, graph=line_graph), decimals=4)
+ assert np.array_equal(position, (2.6928, 1.2876, 0))