A community-maintained Python framework for creating mathematical animations.
Added new classes Angle, RightAngle for two intersecting lines (#1085)
Oliver776067584e63
diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.pyindex cd1f6daba..b1b829f40 100644--- a/manim/mobject/geometry.py+++ b/manim/mobject/geometry.py@@ -54,6 +54,8 @@ __all__ = ["Square","RoundedRectangle","Cutout",+ "Angle",+ "RightAngle",]import warnings@@ -1557,3 +1559,243 @@ class Cutout(VMobject):sub_direction = "CW"for mobject in mobjects:self.append_points(mobject.force_direction(sub_direction).get_points())+++class Angle(Arc, Elbow):+ """A circular arc or elbow-type mobject representing an angle of two lines.++ Parameters+ ----------+ line1 : :class:`Line`+ The first line.+ line2 : :class:`Line`+ The second line.+ radius : :class:`float`+ The radius of the :class:`Arc`.+ quadrant : Sequence[:class:`int`]+ A sequence of two :class:`int` numbers determining which of the 4 quadrants should be used.+ The first value indicates whether to anchor the arc on the first line closer to the end point (1)+ or start point (-1), and the second value functions similarly for the end (1) or start (-1) of the second line.+ Possibilities: (1,1), (-1,1), (1,-1), (-1,-1).+ other_angle : :class:`bool`+ Toggles between the two possible angles defined by two points and an arc center. If set to+ False (default), the arc will always go counterclockwise from the point on line1 until+ the point on line2 is reached. If set to True, the angle will go clockwise from line1 to line2.+ dot : :class:`bool`+ Allows for a :class:`Dot` in the arc. Mainly used as an convention to indicate a right angle.+ The dot can be customized in the next three parameters.+ dot_radius : :class:`float`+ The radius of the :class:`Dot`. If not specified otherwise, this radius will be 1/10 of the arc radius.+ dot_distance : :class:`float`+ Relative distance from the center to the arc: 0 puts the dot in the center and 1 on the arc itself.+ dot_color : :class:`~.Colors`+ The color of the :class:`Dot`.+ elbow : :class:`bool`+ Produces an elbow-type mobject indicating a right angle, see :class:`RightAngle` for more information+ and a shorthand.+ **kwargs+ Further keyword arguments that are passed to the constructor of :class:`Arc` or :class:`Elbow`.++ Examples+ --------+ The first example shows some right angles with a dot in the middle while the second example shows+ all 8 possible angles defined by two lines.++ .. manim:: RightArcAngleExample+ :save_last_frame:++ class RightArcAngleExample(Scene):+ def construct(self):+ line1 = Line( LEFT, RIGHT )+ line2 = Line( DOWN, UP )+ rightarcangles = [+ Angle(line1, line2, dot=True),+ Angle(line1, line2, radius=0.4, quadrant=(1,-1), dot=True, other_angle=True),+ Angle(line1, line2, radius=0.5, quadrant=(-1,1), stroke_width=8, dot=True, dot_color=YELLOW, dot_radius=0.04, other_angle=True),+ Angle(line1, line2, radius=0.7, quadrant=(-1,-1), color=RED, dot=True, dot_color=GREEN, dot_radius=0.08),+ ]+ line_list = VGroup( *[VGroup() for k in range(4)] )+ for k in range(4):+ linea = line1.copy()+ lineb = line2.copy()+ line_list[k].add( linea )+ line_list[k].add( lineb )+ line_list[k].add( rightarcangles[k] )+ line_list.arrange_in_grid(buff=1.5)+ self.add(+ line_list+ )++ .. manim:: AngleExample+ :save_last_frame:++ class AngleExample(Scene):+ def construct(self):+ line1 = Line( LEFT + (1/3) * UP, RIGHT + (1/3) * DOWN )+ line2 = Line( DOWN + (1/3) * RIGHT, UP + (1/3) * LEFT )+ angles = [+ Angle(line1, line2),+ Angle(line1, line2, radius=0.4, quadrant=(1,-1), other_angle=True),+ Angle(line1, line2, radius=0.5, quadrant=(-1,1), stroke_width=8, other_angle=True),+ Angle(line1, line2, radius=0.7, quadrant=(-1,-1), color=RED),+ Angle(line1, line2, other_angle=True),+ Angle(line1, line2, radius=0.4, quadrant=(1,-1)),+ Angle(line1, line2, radius=0.5, quadrant=(-1,1), stroke_width=8),+ Angle(line1, line2, radius=0.7, quadrant=(-1,-1), color=RED, other_angle=True),+ ]+ line_list = VGroup( *[VGroup() for k in range(8)] )+ for k in range(8):+ linea = line1.copy()+ lineb = line2.copy()+ line_list[k].add( linea )+ line_list[k].add( lineb )+ line_list[k].add( angles[k] )+ line_list.arrange_in_grid(n_rows=2, n_cols=4, buff=1.5)+ self.add(+ line_list+ )++ """++ def __init__(+ self,+ line1,+ line2,+ radius=None,+ quadrant=(1, 1),+ other_angle=False,+ dot=False,+ dot_radius=None,+ dot_distance=0.55,+ dot_color=WHITE,+ elbow=False,+ **kwargs+ ):+ self.quadrant = quadrant+ self.dot_distance = dot_distance+ self.elbow = elbow+ inter = line_intersection(+ [line1.get_start(), line1.get_end()], [line2.get_start(), line2.get_end()]+ )++ if radius is None:+ if quadrant[0] == 1:+ dist_1 = np.linalg.norm(line1.get_end() - inter)+ else:+ dist_1 = np.linalg.norm(line1.get_start() - inter)+ if quadrant[1] == 1:+ dist_2 = np.linalg.norm(line2.get_end() - inter)+ else:+ dist_2 = np.linalg.norm(line2.get_start() - inter)+ if np.minimum(dist_1, dist_2) < 0.6:+ radius = (2 / 3) * np.minimum(dist_1, dist_2)+ else:+ radius = 0.4+ else:+ self.radius = radius++ anchor_angle_1 = inter + quadrant[0] * radius * line1.get_unit_vector()+ anchor_angle_2 = inter + quadrant[1] * radius * line2.get_unit_vector()++ if elbow:+ anchor_middle = (+ inter+ + quadrant[0] * radius * line1.get_unit_vector()+ + quadrant[1] * radius * line2.get_unit_vector()+ )+ Elbow.__init__(self, **kwargs)+ self.set_points_as_corners([anchor_angle_1, anchor_middle, anchor_angle_2])+ else:+ angle_1 = angle_of_vector(anchor_angle_1 - inter)+ angle_2 = angle_of_vector(anchor_angle_2 - inter)++ if not other_angle:+ start_angle = angle_1+ if angle_2 > angle_1:+ angle_fin = angle_2 - angle_1+ else:+ angle_fin = 2 * np.pi - (angle_1 - angle_2)+ else:+ start_angle = angle_1+ if angle_2 < angle_1:+ angle_fin = -angle_1 + angle_2+ else:+ angle_fin = -2 * np.pi + (angle_2 - angle_1)++ Arc.__init__(+ self,+ radius=radius,+ angle=angle_fin,+ start_angle=start_angle,+ arc_center=inter,+ **kwargs+ )+ if dot:+ if dot_radius is None:+ dot_radius = radius / 10+ else:+ self.dot_radius = dot_radius+ right_dot = Dot(ORIGIN, radius=dot_radius, color=dot_color)+ dot_anchor = (+ inter+ + (self.get_center() - inter)+ / np.linalg.norm(self.get_center() - inter)+ * radius+ * dot_distance+ )+ right_dot.move_to(dot_anchor)+ self.add(right_dot)++ def generate_points(self):+ if self.elbow:+ Elbow.generate_points(self)+ else:+ Arc.generate_points(self)+++class RightAngle(Angle):+ """An elbow-type mobject representing a right angle between two lines.++ Parameters+ ----------+ line1 : :class:`Line`+ The first line.+ line2 : :class:`Line`+ The second line.+ length : :class:`float`+ The length of the arms.+ **kwargs+ Further keyword arguments that are passed to the constructor of :class:`Angle`.++ Examples+ --------++ .. manim:: RightAngleExample+ :save_last_frame:++ class RightAngleExample(Scene):+ def construct(self):+ line1 = Line( LEFT, RIGHT )+ line2 = Line( DOWN, UP )+ rightangles = [+ RightAngle(line1, line2),+ RightAngle(line1, line2, length=0.4, quadrant=(1,-1)),+ RightAngle(line1, line2, length=0.5, quadrant=(-1,1), stroke_width=8),+ RightAngle(line1, line2, length=0.7, quadrant=(-1,-1), color=RED),+ ]+ line_list = VGroup( *[VGroup() for k in range(4)] )+ for k in range(4):+ linea = line1.copy()+ lineb = line2.copy()+ line_list[k].add( linea )+ line_list[k].add( lineb )+ line_list[k].add( rightangles[k] )+ line_list.arrange_in_grid(buff=1.5)+ self.add(+ line_list+ )++ """++ def __init__(self, line1, line2, length=None, **kwargs):+ Angle.__init__(self, line1, line2, radius=length, elbow=True, **kwargs)diff --git a/tests/control_data/graphical_units_data/geometry/AngleTest.npz b/tests/control_data/graphical_units_data/geometry/AngleTest.npznew file mode 100644index 000000000..19fdf31eeBinary files /dev/null and b/tests/control_data/graphical_units_data/geometry/AngleTest.npz differdiff --git a/tests/control_data/graphical_units_data/geometry/RightAngleTest.npz b/tests/control_data/graphical_units_data/geometry/RightAngleTest.npznew file mode 100644index 000000000..241860285Binary files /dev/null and b/tests/control_data/graphical_units_data/geometry/RightAngleTest.npz differdiff --git a/tests/test_graphical_units/test_geometry.py b/tests/test_graphical_units/test_geometry.pyindex 081b9e7a8..bfb509f2f 100644--- a/tests/test_graphical_units/test_geometry.py+++ b/tests/test_graphical_units/test_geometry.py@@ -150,6 +150,22 @@ class ZIndexTest(Scene):self.wait(1)+class AngleTest(Scene):+ def construct(self):+ l1 = Line(ORIGIN, RIGHT)+ l2 = Line(ORIGIN, UP)+ a = Angle(l1, l2)+ self.play(Animation(a))+++class RightAngleTest(Scene):+ def construct(self):+ l1 = Line(ORIGIN, RIGHT)+ l2 = Line(ORIGIN, UP)+ a = RightAngle(l1, l2)+ self.play(Animation(a))++MODULE_NAME = "geometry"