From e6284b1acc9fe228a7c9788100266a1a8be6d2e8 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Thu, 9 May 2024 07:35:58 -0400 Subject: [PATCH 1/7] Add shorthand for axes --- manim/mobject/graphing/coordinate_systems.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/manim/mobject/graphing/coordinate_systems.py b/manim/mobject/graphing/coordinate_systems.py index 89e072d4b3..009ff97c6d 100644 --- a/manim/mobject/graphing/coordinate_systems.py +++ b/manim/mobject/graphing/coordinate_systems.py @@ -20,6 +20,7 @@ from manim import config from manim.constants import * +from manim.mobject.mobject import Mobject from manim.mobject.geometry.arc import Circle, Dot from manim.mobject.geometry.line import Arrow, DashedLine, Line from manim.mobject.geometry.polygram import Polygon, Rectangle, RegularPolygon @@ -144,7 +145,7 @@ def __init__( self.y_length = y_length self.num_sampled_graph_points_per_tick = 10 - def coords_to_point(self, *coords: Sequence[ManimFloat]): + def coords_to_point(self, *coords: Point3D): raise NotImplementedError() def point_to_coords(self, point: Point3D): @@ -1779,6 +1780,14 @@ def construct(self): return T_label_group + def __matmul__(self, coord: Point3D | Mobject): + if isinstance(coord, Mobject): + coord = coord.get_center() + return self.coords_to_point(*coord) + + def __rmatmul__(self, point: Point3D): + return self.point_to_coords(point) + class Axes(VGroup, CoordinateSystem, metaclass=ConvertToOpenGL): """Creates a set of axes. @@ -1979,6 +1988,7 @@ def coords_to_point( self, *coords: float | Sequence[float] | Sequence[Sequence[float]] | np.ndarray ) -> np.ndarray: """Accepts coordinates from the axes and returns a point with respect to the scene. + Equivalent to `ax@(coord1)` Parameters ---------- @@ -2007,6 +2017,8 @@ def coords_to_point( >>> ax = Axes() >>> np.around(ax.coords_to_point(1, 0, 0), 2) array([0.86, 0. , 0. ]) + >>> np.around(ax @ (1, 0, 0), 2) + array([0.86, 0. , 0. ]) >>> np.around(ax.coords_to_point([[0, 1], [1, 1], [1, 0]]), 2) array([[0. , 0.75, 0. ], [0.86, 0.75, 0. ], From 2b789f9dd606361ca8c527d236731e674c88dacb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 May 2024 11:43:29 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/mobject/graphing/coordinate_systems.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/graphing/coordinate_systems.py b/manim/mobject/graphing/coordinate_systems.py index 009ff97c6d..d7cd1b5385 100644 --- a/manim/mobject/graphing/coordinate_systems.py +++ b/manim/mobject/graphing/coordinate_systems.py @@ -20,13 +20,13 @@ from manim import config from manim.constants import * -from manim.mobject.mobject import Mobject from manim.mobject.geometry.arc import Circle, Dot from manim.mobject.geometry.line import Arrow, DashedLine, Line from manim.mobject.geometry.polygram import Polygon, Rectangle, RegularPolygon from manim.mobject.graphing.functions import ImplicitFunction, ParametricFunction from manim.mobject.graphing.number_line import NumberLine from manim.mobject.graphing.scale import LinearBase +from manim.mobject.mobject import Mobject from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL from manim.mobject.opengl.opengl_surface import OpenGLSurface from manim.mobject.text.tex_mobject import MathTex From 30ea9221c0d25e14b0aed8e8f530cdc8753f240f Mon Sep 17 00:00:00 2001 From: adeshpande <110117391+JasonGrace2282@users.noreply.github.com> Date: Thu, 9 May 2024 15:35:15 -0400 Subject: [PATCH 3/7] Add spacing Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com> --- manim/mobject/graphing/coordinate_systems.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/graphing/coordinate_systems.py b/manim/mobject/graphing/coordinate_systems.py index d7cd1b5385..58900ca005 100644 --- a/manim/mobject/graphing/coordinate_systems.py +++ b/manim/mobject/graphing/coordinate_systems.py @@ -1988,7 +1988,7 @@ def coords_to_point( self, *coords: float | Sequence[float] | Sequence[Sequence[float]] | np.ndarray ) -> np.ndarray: """Accepts coordinates from the axes and returns a point with respect to the scene. - Equivalent to `ax@(coord1)` + Equivalent to `ax @ (coord1)` Parameters ---------- From fa1b08fd2d2d1130a375c7e4810042c00108ed1c Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Sat, 11 May 2024 21:31:14 -0400 Subject: [PATCH 4/7] Convert CoordinateSystem example, and add to NumberLine --- manim/mobject/graphing/coordinate_systems.py | 8 ++++---- manim/mobject/graphing/number_line.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/manim/mobject/graphing/coordinate_systems.py b/manim/mobject/graphing/coordinate_systems.py index 58900ca005..2c0112ce22 100644 --- a/manim/mobject/graphing/coordinate_systems.py +++ b/manim/mobject/graphing/coordinate_systems.py @@ -96,10 +96,10 @@ def construct(self): ) # Extra lines and labels for point (1,1) - graphs += grid.get_horizontal_line(grid.c2p(1, 1, 0), color=BLUE) - graphs += grid.get_vertical_line(grid.c2p(1, 1, 0), color=BLUE) - graphs += Dot(point=grid.c2p(1, 1, 0), color=YELLOW) - graphs += Tex("(1,1)").scale(0.75).next_to(grid.c2p(1, 1, 0)) + graphs += grid.get_horizontal_line(grid @ (1, 1, 0), color=BLUE) + graphs += grid.get_vertical_line(grid @ (1, 1, 0), color=BLUE) + graphs += Dot(point=grid @ (1, 1, 0), color=YELLOW) + graphs += Tex("(1,1)").scale(0.75).next_to(grid @ (1, 1, 0)) title = Title( # spaces between braces to prevent SyntaxError r"Graphs of $y=x^{ {1}\over{n} }$ and $y=x^n (n=1,2,3,...,20)$", diff --git a/manim/mobject/graphing/number_line.py b/manim/mobject/graphing/number_line.py index 3d70ca530b..d925ad81bc 100644 --- a/manim/mobject/graphing/number_line.py +++ b/manim/mobject/graphing/number_line.py @@ -2,6 +2,7 @@ from __future__ import annotations +from manim.mobject.mobject import Mobject from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVMobject __all__ = ["NumberLine", "UnitInterval"] @@ -11,6 +12,7 @@ if TYPE_CHECKING: from manim.mobject.geometry.tips import ArrowTip + from manim.typing import Point3D import numpy as np @@ -641,6 +643,14 @@ def _decimal_places_from_step(step) -> int: return 0 return len(step.split(".")[-1]) + def __matmul__(self, other: float): + return self.n2p(other) + + def __rmatmul__(self, other: Point3D | Mobject): + if isinstance(other, Mobject): + other = other.get_center() + return self.p2n(other) + class UnitInterval(NumberLine): def __init__( From ea7b32384b1f5a1da9d24d6529c58f33d52e4117 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Fri, 17 May 2024 18:33:24 -0400 Subject: [PATCH 5/7] Add doctest for NumberLine --- manim/mobject/graphing/coordinate_systems.py | 2 +- manim/mobject/graphing/number_line.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/manim/mobject/graphing/coordinate_systems.py b/manim/mobject/graphing/coordinate_systems.py index 44f84c95b9..5a95d3d203 100644 --- a/manim/mobject/graphing/coordinate_systems.py +++ b/manim/mobject/graphing/coordinate_systems.py @@ -561,7 +561,7 @@ def get_horizontal_line(self, point: Sequence[float], **kwargs) -> Line: class GetHorizontalLineExample(Scene): def construct(self): ax = Axes().add_coordinates() - point = ax.c2p(-4, 1.5) + point = ax @ (-4, 1.5) dot = Dot(point) line = ax.get_horizontal_line(point, line_func=Line) diff --git a/manim/mobject/graphing/number_line.py b/manim/mobject/graphing/number_line.py index 5f15ac968d..79dbe85a98 100644 --- a/manim/mobject/graphing/number_line.py +++ b/manim/mobject/graphing/number_line.py @@ -346,6 +346,7 @@ def get_tick_range(self) -> np.ndarray: def number_to_point(self, number: float | np.ndarray) -> np.ndarray: """Accepts a value along the number line and returns a point with respect to the scene. + Equivalent to `NumberLine @ number` Parameters ---------- @@ -366,6 +367,8 @@ def number_to_point(self, number: float | np.ndarray) -> np.ndarray: array([0., 0., 0.]) >>> number_line.number_to_point(1) array([1., 0., 0.]) + >>> number_line @ 1 + array([1., 0., 0.]) >>> number_line.number_to_point([1,2,3]) array([[1., 0., 0.], [2., 0., 0.], From c3fabde2be1b2958d1d6f61c5965756ca4985ce1 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Thu, 23 May 2024 12:55:04 -0400 Subject: [PATCH 6/7] Add test --- .../mobject/graphing/test_coordinate_system.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/module/mobject/graphing/test_coordinate_system.py b/tests/module/mobject/graphing/test_coordinate_system.py index fd86a9bd4d..4aa71f8968 100644 --- a/tests/module/mobject/graphing/test_coordinate_system.py +++ b/tests/module/mobject/graphing/test_coordinate_system.py @@ -7,7 +7,7 @@ from manim import LEFT, ORIGIN, PI, UR, Axes, Circle, ComplexPlane from manim import CoordinateSystem as CS -from manim import NumberPlane, PolarPlane, ThreeDAxes, config, tempconfig +from manim import Dot, NumberPlane, PolarPlane, ThreeDAxes, config, tempconfig def test_initial_config(): @@ -119,7 +119,15 @@ def test_coords_to_point(): # a point with respect to the axes c2p_coord = np.around(ax.coords_to_point(2, 2), decimals=4) - np.testing.assert_array_equal(c2p_coord, (1.7143, 1.5, 0)) + c2p_coord_matmul = np.around(ax @ (2, 2), decimals=4) + + expected = (1.7143, 1.5, 0) + + np.testing.assert_array_equal(c2p_coord, expected) + np.testing.assert_array_equal(c2p_coord_matmul, c2p_coord) + + mob = Dot().move_to((2, 2, 0)) + np.testing.assert_array_equal(np.around(ax @ mob, decimals=4), expected) def test_coords_to_point_vectorized(): From 775aa7ed541fcbec4880b3fb4f19718652bcb393 Mon Sep 17 00:00:00 2001 From: adeshpande <110117391+JasonGrace2282@users.noreply.github.com> Date: Mon, 27 May 2024 13:18:25 -0400 Subject: [PATCH 7/7] Fix typehint for c2p Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com> --- manim/mobject/graphing/coordinate_systems.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/graphing/coordinate_systems.py b/manim/mobject/graphing/coordinate_systems.py index 86e56cfe6f..435d7aced8 100644 --- a/manim/mobject/graphing/coordinate_systems.py +++ b/manim/mobject/graphing/coordinate_systems.py @@ -146,7 +146,7 @@ def __init__( self.y_length = y_length self.num_sampled_graph_points_per_tick = 10 - def coords_to_point(self, *coords: Point3D): + def coords_to_point(self, *coords: ManimFloat): raise NotImplementedError() def point_to_coords(self, point: Point3D):