Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Implemented :meth:`.Line3D.parallel_to` and :meth:`.Line3D.perpendicular_to` (#2196)

icedcoffeeee239a7b3985c3
diff --git a/manim/mobject/three_dimensions.py b/manim/mobject/three_dimensions.py
index c0c662f5e..c1f52fd5f 100644
--- a/manim/mobject/three_dimensions.py
+++ b/manim/mobject/three_dimensions.py
@@ -32,7 +32,7 @@ from ..mobject.types.vectorized_mobject import VGroup, VMobject
from ..utils.color import *
from ..utils.deprecation import deprecated, deprecated_params
from ..utils.iterables import tuplify
-from ..utils.space_ops import normalize, z_to_vector
+from ..utils.space_ops import normalize, perpendicular_bisector, z_to_vector
class ThreeDVMobject(VMobject, metaclass=ConvertToOpenGL):
@@ -749,6 +749,94 @@ class Line3D(Cylinder):
def get_end(self):
return self.end
+ @classmethod
+ def parallel_to(
+ cls,
+ line: "Line3D",
+ point: Sequence[float] = ORIGIN,
+ length: float = 5,
+ **kwargs
+ ):
+ """Returns a line parallel to another line going through
+ a given point.
+
+ Parameters
+ ----------
+ line
+ The line to be parallel to.
+ point
+ The point to pass through.
+ kwargs
+ Additional parameters to be passed to the class.
+
+ Examples
+ --------
+ .. manim:: ParallelLineExample
+ :save_last_frame:
+
+ class ParallelLineExample(ThreeDScene):
+ def construct(self):
+ self.set_camera_orientation(PI / 3, -PI / 4)
+ ax = ThreeDAxes((-5, 5), (-5, 5), (-5, 5), 10, 10, 10)
+ line1 = Line3D(RIGHT * 2, UP + OUT, color=RED)
+ line2 = Line3D.parallel_to(line1, color=YELLOW)
+ self.add(ax, line1, line2)
+ """
+ point = np.array(point)
+ vect = normalize(line.vect)
+ return cls(
+ point + vect * length / 2,
+ point - vect * length / 2,
+ **kwargs,
+ )
+
+ @classmethod
+ def perpendicular_to(
+ cls,
+ line: "Line3D",
+ point: Sequence[float] = ORIGIN,
+ length: float = 5,
+ **kwargs
+ ):
+ """Returns a line perpendicular to another line going through
+ a given point.
+
+ Parameters
+ ----------
+ line
+ The line to be perpendicular to.
+ point
+ The point to pass through.
+ kwargs
+ Additional parameters to be passed to the class.
+
+ Examples
+ --------
+ .. manim:: PerpLineExample
+ :save_last_frame:
+
+ class PerpLineExample(ThreeDScene):
+ def construct(self):
+ self.set_camera_orientation(PI / 3, -PI / 4)
+ ax = ThreeDAxes((-5, 5), (-5, 5), (-5, 5), 10, 10, 10)
+ line1 = Line3D(RIGHT * 2, UP + OUT, color=RED)
+ line2 = Line3D.perpendicular_to(line1, color=BLUE)
+ self.add(ax, line1, line2)
+ """
+ point = np.array(point)
+
+ norm = np.cross(line.vect, point - line.start)
+ if all(np.linalg.norm(norm) == np.zeros(3)):
+ raise ValueError("Could not find the perpendicular.")
+
+ start, end = perpendicular_bisector([line.start, line.end], norm)
+ vect = normalize(end - start)
+ return cls(
+ point + vect * length / 2,
+ point - vect * length / 2,
+ **kwargs,
+ )
+
class Arrow3D(Line3D):
"""An arrow made out of a cylindrical line and a conical tip.
diff --git a/tests/control_data/graphical_units_data/threed/Line3D.npz b/tests/control_data/graphical_units_data/threed/Line3D.npz
index 7ab914227..173414da5 100644
Binary files a/tests/control_data/graphical_units_data/threed/Line3D.npz and b/tests/control_data/graphical_units_data/threed/Line3D.npz differ
diff --git a/tests/test_graphical_units/test_threed.py b/tests/test_graphical_units/test_threed.py
index 482969647..ccbf27acf 100644
--- a/tests/test_graphical_units/test_threed.py
+++ b/tests/test_graphical_units/test_threed.py
@@ -31,7 +31,10 @@ def test_Cylinder(scene):
@frames_comparison(base_scene=ThreeDScene)
def test_Line3D(scene):
- scene.add(Line3D())
+ line = Line3D()
+ perp_line = Line3D.perpendicular_to(line, UP)
+ parallel_line = Line3D.parallel_to(line, UP)
+ scene.add(line, perp_line, parallel_line)
@frames_comparison(base_scene=ThreeDScene)