Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Raise appropriate errors in :meth:`~.VMobject.point_from_proportion` (#1302)

friedkeenan3156b9f20aa3
diff --git a/manim/mobject/types/opengl_vectorized_mobject.py b/manim/mobject/types/opengl_vectorized_mobject.py
index 90cf465ee..9af1e23ca 100644
--- a/manim/mobject/types/opengl_vectorized_mobject.py
+++ b/manim/mobject/types/opengl_vectorized_mobject.py
@@ -624,7 +624,31 @@ class OpenGLVMobject(OpenGLMobject):
for n in range(num_curves):
yield self.get_nth_curve_function_with_length(n, **kwargs)
- def point_from_proportion(self, alpha):
+ def point_from_proportion(self, alpha: float) -> np.ndarray:
+ """Gets the point at a proportion along the path of the :class:`OpenGLVMobject`.
+
+ Parameters
+ ----------
+ alpha
+ The proportion along the the path of the :class:`OpenGLVMobject`.
+
+ Returns
+ -------
+ :class:`numpy.ndarray`
+ The point on the :class:`OpenGLVMobject`.
+
+ Raises
+ ------
+ :exc:`ValueError`
+ If ``alpha`` is not between 0 and 1.
+ :exc:`Exception`
+ If the :class:`OpenGLVMobject` has no points.
+ """
+
+ if alpha < 0 or alpha > 1:
+ raise ValueError(f"Alpha {alpha} not between 0 and 1.")
+
+ self.throw_error_if_no_points()
if alpha == 1:
return self.get_points()[-1]
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.py
index 80f9e33b1..f8c573273 100644
--- a/manim/mobject/types/vectorized_mobject.py
+++ b/manim/mobject/types/vectorized_mobject.py
@@ -977,20 +977,30 @@ class VMobject(Mobject):
yield self.get_nth_curve_function_with_length(n, **kwargs)
def point_from_proportion(self, alpha: float) -> np.ndarray:
- """Get the bezier curve evaluated at a position P,
- where P is the point corresponding to the proportion defined by the given alpha.
+ """Gets the point at a proportion along the path of the :class:`VMobject`.
Parameters
----------
- alpha : float
- Proportion.
+ alpha
+ The proportion along the the path of the :class:`VMobject`.
Returns
-------
- np.ndarray
- Point evaluated.
+ :class:`numpy.ndarray`
+ The point on the :class:`VMobject`.
+
+ Raises
+ ------
+ :exc:`ValueError`
+ If ``alpha`` is not between 0 and 1.
+ :exc:`Exception`
+ If the :class:`VMobject` has no points.
"""
+ if alpha < 0 or alpha > 1:
+ raise ValueError(f"Alpha {alpha} not between 0 and 1.")
+
+ self.throw_error_if_no_points()
if alpha == 1:
return self.get_points()[-1]
diff --git a/tests/test_vectorized_mobject.py b/tests/test_vectorized_mobject.py
index 57d8de38a..712f64a33 100644
--- a/tests/test_vectorized_mobject.py
+++ b/tests/test_vectorized_mobject.py
@@ -20,6 +20,13 @@ def test_vmobject_point_from_propotion():
# would be at length 3, which lands in the first, long line.
assert np.all(obj.point_from_proportion(0.5) == np.array([3, 0, 0]))
+ with pytest.raises(ValueError, match="between 0 and 1"):
+ obj.point_from_proportion(2)
+
+ obj.clear_points()
+ with pytest.raises(Exception, match="with no points"):
+ obj.point_from_proportion(0)
+
def test_vgroup_init():
"""Test the VGroup instantiation."""