Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

addition(bezier.py): adding subdivide_quadratic_bezier and split_quad… (#2841)

Tristan Schulzeef0dd30b5b6
diff --git a/docs/source/_static/bezier_subdivision_example.png b/docs/source/_static/bezier_subdivision_example.png
new file mode 100644
index 000000000..8cdb8f241
Binary files /dev/null and b/docs/source/_static/bezier_subdivision_example.png differ
diff --git a/manim/mobject/opengl/opengl_vectorized_mobject.py b/manim/mobject/opengl/opengl_vectorized_mobject.py
index c866eb2bf..47e25b738 100644
--- a/manim/mobject/opengl/opengl_vectorized_mobject.py
+++ b/manim/mobject/opengl/opengl_vectorized_mobject.py
@@ -690,7 +690,7 @@ class OpenGLVMobject(OpenGLMobject):
nppc = self.n_points_per_curve
remainder = len(points) % nppc
points = points[: len(points) - remainder]
- return [points[i : i + nppc] for i in range(0, len(points), nppc)]
+ return points.reshape((-1, nppc, 3))
def get_bezier_tuples(self):
return self.get_bezier_tuples_from_points(self.points)
diff --git a/manim/utils/bezier.py b/manim/utils/bezier.py
index 4a489318b..689404e05 100644
--- a/manim/utils/bezier.py
+++ b/manim/utils/bezier.py
@@ -22,6 +22,7 @@ __all__ = [
import typing
from functools import reduce
+from typing import Iterable
import numpy as np
from scipy import linalg
@@ -119,6 +120,61 @@ def partial_quadratic_bezier_points(points, a, b):
return [h0, h1, h2]
+def split_quadratic_bezier(points: Iterable[float], t: float) -> np.ndarray:
+ """Split a quadratic Bézier curve at argument ``t`` into two quadratic curves.
+
+ Parameters
+ ----------
+ points
+ The control points of the bezier curve
+ has shape ``[a1, h1, b1]``
+
+ t
+ The ``t``-value at which to split the Bézier curve
+
+ Returns
+ -------
+ The two Bézier curves as a list of tuples,
+ has the shape ``[a1, h1, b1], [a2, h2, b2]``
+ """
+ a1, h1, a2 = points
+ s1 = interpolate(a1, h1, t)
+ s2 = interpolate(h1, a2, t)
+ p = interpolate(s1, s2, t)
+
+ return np.array([a1, s1, p, p, s2, a2])
+
+
+def subdivide_quadratic_bezier(points: Iterable[float], n: int) -> np.ndarray:
+ """Subdivide a quadratic Bézier curve into ``n`` subcurves which have the same shape.
+
+ The points at which the curve is split are located at the
+ arguments :math:`t = i/n` for :math:`i = 1, ..., n-1`.
+
+ Parameters
+ ----------
+ points
+ The control points of the Bézier curve in form ``[a1, h1, b1]``
+
+ n
+ The number of curves to subdivide the Bézier curve into
+
+ Returns
+ -------
+ The new points for the Bézier curve in the form ``[a1, h1, b1, a2, h2, b2, ...]``
+
+ .. image:: /_static/bezier_subdivision_example.png
+
+ """
+ beziers = []
+ current = points
+ for i in range(n, 0, -1):
+ tmp = split_quadratic_bezier(current, 1 / i)
+ beziers.append(tmp[:3])
+ current = tmp[3:]
+ return np.asarray(beziers).reshape(-1, 3)
+
+
# Linear interpolation variants