Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added a static method to :class:`~.Circle` to allow it to be defined by three points (#1765)

icedcoffeeee266e54d3c51a
diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.py
index 4a07b387c..7cfd749e1 100644
--- a/manim/mobject/geometry.py
+++ b/manim/mobject/geometry.py
@@ -87,6 +87,7 @@ from ..utils.space_ops import (
compass_directions,
line_intersection,
normalize,
+ perpendicular_bisector,
regular_vertices,
rotate_vector,
)
@@ -583,6 +584,36 @@ class Circle(Arc):
start_angle = angle_of_vector(self.get_points()[0] - self.get_center())
return self.point_from_proportion((angle - start_angle) / TAU)
+ @staticmethod
+ def from_three_points(
+ p1: Sequence[float], p2: Sequence[float], p3: Sequence[float], **kwargs
+ ):
+ """Returns a circle passing through the specified
+ three points.
+
+ Example
+ -------
+
+ .. manim:: CircleFromPointsExample
+ :save_last_frame:
+
+ class CircleFromPointsExample(Scene):
+ def construct(self):
+ circle = Circle.from_three_points(LEFT, LEFT + UP, UP * 2, color=RED)
+ dots = VGroup(
+ Dot(LEFT),
+ Dot(LEFT + UP),
+ Dot(UP * 2),
+ )
+ self.add(NumberPlane(), circle, dots)
+ """
+ center = line_intersection(
+ perpendicular_bisector([p1, p2]),
+ perpendicular_bisector([p2, p3]),
+ )
+ radius = np.linalg.norm(p1 - center)
+ return Circle(radius=radius, **kwargs).shift(center)
+
class Dot(Circle):
"""A circle with a very small radius.
diff --git a/manim/utils/space_ops.py b/manim/utils/space_ops.py
index a675bd435..ddb2cd73b 100644
--- a/manim/utils/space_ops.py
+++ b/manim/utils/space_ops.py
@@ -27,6 +27,7 @@ __all__ = [
"get_winding_number",
"cross2d",
"earclip_triangulation",
+ "perpendicular_bisector",
]
@@ -745,3 +746,32 @@ def earclip_triangulation(verts: np.ndarray, ring_ends: list) -> list:
meta_indices = earcut(verts[indices, :2], [len(indices)])
return [indices[mi] for mi in meta_indices]
+
+
+def perpendicular_bisector(
+ line: Sequence[np.ndarray], norm_vector=OUT
+) -> Sequence[np.ndarray]:
+ """Returns a list of two points that correspond
+ to the ends of the perpendicular bisector of the
+ two points given.
+
+ Parameters
+ ----------
+ line
+ a list of two numpy array points (corresponding
+ to the ends of a line).
+ norm_vector
+ the vector perpendicular to both the line given
+ and the perpendicular bisector.
+
+ Returns
+ -------
+ list
+ A list of two numpy array points that correspond
+ to the ends of the perpendicular bisector
+ """
+ p1 = line[0]
+ p2 = line[1]
+ direction = np.cross(p1 - p2, norm_vector)
+ m = midpoint(p1, p2)
+ return [m + direction, m - direction]
diff --git a/tests/control_data/graphical_units_data/geometry/CirclePointsTest.npz b/tests/control_data/graphical_units_data/geometry/CirclePointsTest.npz
new file mode 100644
index 000000000..477db848f
Binary files /dev/null and b/tests/control_data/graphical_units_data/geometry/CirclePointsTest.npz differ
diff --git a/tests/test_graphical_units/test_geometry.py b/tests/test_graphical_units/test_geometry.py
index c4a17dca9..44a67bc8c 100644
--- a/tests/test_graphical_units/test_geometry.py
+++ b/tests/test_graphical_units/test_geometry.py
@@ -49,6 +49,12 @@ class CircleTest(Scene):
self.add(circle)
+class CirclePointsTest(Scene):
+ def construct(self):
+ circle = Circle.from_three_points(LEFT, LEFT + UP, UP * 2)
+ self.add(circle)
+
+
class DotTest(Scene):
def construct(self):
dot = Dot()