Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Implemented :class:`~.ImplicitFunction` and :meth:`.CoordinateSystem.get_implicit_curve` for plotting implicit curves (#2094)

icedcoffeeeeb8d76cfa7a8f
diff --git a/manim/mobject/coordinate_systems.py b/manim/mobject/coordinate_systems.py
index f27b2694f..c660d8d6b 100644
--- a/manim/mobject/coordinate_systems.py
+++ b/manim/mobject/coordinate_systems.py
@@ -21,7 +21,7 @@ from manim.mobject.opengl_compatibility import ConvertToOpenGL
from .. import config
from ..constants import *
-from ..mobject.functions import ParametricFunction
+from ..mobject.functions import ImplicitFunction, ParametricFunction
from ..mobject.geometry import (
Arrow,
Circle,
@@ -200,7 +200,7 @@ class CoordinateSystem:
return self.get_x_axis().get_unit_size()
def get_y_unit_size(self):
- return self.get_x_axis().get_unit_size()
+ return self.get_y_axis().get_unit_size()
def get_x_axis_label(
self,
@@ -698,6 +698,54 @@ class CoordinateSystem:
graph.underlying_function = function
return graph
+ def get_implicit_curve(
+ self,
+ func: Callable,
+ min_depth: int = 5,
+ max_quads: int = 1500,
+ **kwargs,
+ ) -> ImplicitFunction:
+ """Creates the curves of an implicit function.
+
+ Parameters
+ ----------
+ func
+ The function to graph, in the form of f(x, y) = 0.
+ min_depth
+ The minimum depth of the function to calculate.
+ max_quads
+ The maximum number of quads to use.
+ kwargs
+ Additional parameters to pass into :class:`ImplicitFunction`
+
+ Examples
+ --------
+ .. manim:: ImplicitExample
+ :save_last_frame:
+
+ class ImplicitExample(Scene):
+ def construct(self):
+ ax = Axes()
+ a = ax.get_implicit_curve(
+ lambda x, y: y * (x - y) ** 2 - 4 * x - 8, color=BLUE
+ )
+ self.add(ax, a)
+ """
+ graph = ImplicitFunction(
+ func=func,
+ x_range=self.x_range[:2],
+ y_range=self.y_range[:2],
+ min_depth=min_depth,
+ max_quads=max_quads,
+ **kwargs,
+ )
+ (
+ graph.stretch(self.get_x_unit_size(), 0, about_point=ORIGIN)
+ .stretch(self.get_y_unit_size(), 1, about_point=ORIGIN)
+ .shift(self.get_origin())
+ )
+ return graph
+
def get_parametric_curve(self, function, **kwargs):
dim = self.dimension
graph = ParametricFunction(
diff --git a/manim/mobject/functions.py b/manim/mobject/functions.py
index 5c0e3c908..204183b13 100644
--- a/manim/mobject/functions.py
+++ b/manim/mobject/functions.py
@@ -1,9 +1,12 @@
"""Mobjects representing function graphs."""
-__all__ = ["ParametricFunction", "FunctionGraph"]
+__all__ = ["ParametricFunction", "FunctionGraph", "ImplicitFunction"]
+from typing import Callable, Optional, Sequence
+
import numpy as np
+from isosurfaces import plot_isoline
from .. import config
from ..constants import *
@@ -147,3 +150,96 @@ class FunctionGraph(ParametricFunction):
def get_point_from_function(self, x):
return self.parametric_function(x)
+
+
+class ImplicitFunction(VMobject, metaclass=ConvertToOpenGL):
+ def __init__(
+ self,
+ func: Callable[[float, float], float],
+ x_range: Optional[Sequence[float]] = None,
+ y_range: Optional[Sequence[float]] = None,
+ min_depth: int = 5,
+ max_quads: int = 1500,
+ use_smoothing: bool = True,
+ **kwargs
+ ):
+ """An implicit function.
+
+ Parameters
+ ----------
+ func
+ The implicit function in the form ``f(x, y) = 0``.
+ x_range
+ The x min and max of the function.
+ y_range
+ The y min and max of the function.
+ min_depth
+ The minimum depth of the function to calculate.
+ max_quads
+ The maximum number of quads to use.
+ use_smoothing
+ Whether or not to smoothen the curves.
+ kwargs
+ Additional parameters to pass into :class:`VMobject`
+
+
+ .. note::
+ A small ``min_depth`` :math:`d` means that some small details might
+ be ignored if they don't cross an edge of one of the
+ :math:`4^d` uniform quads.
+
+ The value of ``max_quads`` strongly corresponds to the
+ quality of the curve, but a higher number of quads
+ may take longer to render.
+
+ Examples
+ --------
+ .. manim:: ImplicitFunctionExample
+ :save_last_frame:
+
+ class ImplicitFunctionExample(Scene):
+ def construct(self):
+ graph = ImplicitFunction(
+ lambda x, y: x * y ** 2 - x ** 2 * y - 2,
+ color=YELLOW
+ )
+ self.add(NumberPlane(), graph)
+ """
+ self.function = func
+ self.min_depth = min_depth
+ self.max_quads = max_quads
+ self.use_smoothing = use_smoothing
+ self.x_range = x_range or [
+ -config.frame_width / 2,
+ config.frame_width / 2,
+ ]
+ self.y_range = y_range or [
+ -config.frame_height / 2,
+ config.frame_height / 2,
+ ]
+
+ super().__init__(**kwargs)
+
+ def generate_points(self):
+ p_min, p_max = (
+ np.array([self.x_range[0], self.y_range[0]]),
+ np.array([self.x_range[1], self.y_range[1]]),
+ )
+ curves = plot_isoline(
+ fn=lambda u: self.function(u[0], u[1]),
+ pmin=p_min,
+ pmax=p_max,
+ min_depth=self.min_depth,
+ max_quads=self.max_quads,
+ ) # returns a list of lists of 2D points
+ curves = [
+ np.pad(curve, [(0, 0), (0, 1)]) for curve in curves if curve != []
+ ] # add z coord as 0
+ for curve in curves:
+ self.start_new_path(curve[0])
+ self.add_points_as_corners(curve[1:])
+ if self.use_smoothing:
+ self.make_smooth()
+ return self
+
+ init_points = generate_points
diff --git a/poetry.lock b/poetry.lock
index d39a8f9dd..7cb3809d5 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -610,7 +610,7 @@ protobuf = ">=3.5.0.post1,<4.0dev"
[[package]]
name = "identify"
-version = "2.2.15"
+version = "2.3.0"
description = "File identification library for Python"
category = "dev"
optional = false
@@ -737,6 +737,17 @@ requirements_deprecated_finder = ["pipreqs", "pip-api"]
colors = ["colorama (>=0.4.3,<0.5.0)"]
plugins = ["setuptools"]
+[[package]]
+name = "isosurfaces"
+version = "0.1.0"
+description = "Construct isolines/isosurfaces over a 2D/3D scalar field defined by a function (not a uniform grid)"
+category = "main"
+optional = false
+python-versions = "*"
+
+[package.dependencies]
+numpy = "*"
+
[[package]]
name = "jedi"
version = "0.18.0"
@@ -2087,14 +2098,14 @@ testing = ["coverage (>=4)", "coverage-enable-subprocess (>=1)", "flaky (>=3)",
[[package]]
name = "watchdog"
-version = "2.1.5"
+version = "2.1.6"
description = "Filesystem events monitoring"
category = "main"
optional = false
python-versions = ">=3.6"
[package.extras]
-watchmedo = ["PyYAML (>=3.10)", "argh (>=0.24.1)"]
+watchmedo = ["PyYAML (>=3.10)"]
[[package]]
name = "wcwidth"
@@ -2152,7 +2163,7 @@ webgl_renderer = ["grpcio", "grpcio-tools"]
[metadata]
lock-version = "1.1"
python-versions = "^3.7"
-content-hash = "2f159ae91113bf5f81a74f02a471abe291199a708169db5dd235fa8ae883f749"
+content-hash = "9edc33b7502b40d0b2bcd06d48e40ec743d468d45d4f38178bd009a4aa596d10"
[metadata.files]
alabaster = [
@@ -2639,8 +2650,8 @@ grpcio-tools = [
{file = "grpcio_tools-1.33.2-cp39-cp39-win_amd64.whl", hash = "sha256:b68ce929acb8c392b83c49257e18f03d73ca3af408bfd5f7e78fe351c384c008"},
]
identify = [
- {file = "identify-2.2.15-py2.py3-none-any.whl", hash = "sha256:de83a84d774921669774a2000bf87ebba46b4d1c04775f4a5d37deff0cf39f73"},
- {file = "identify-2.2.15.tar.gz", hash = "sha256:528a88021749035d5a39fe2ba67c0642b8341aaf71889da0e1ed669a429b87f0"},
+ {file = "identify-2.3.0-py2.py3-none-any.whl", hash = "sha256:d1e82c83d063571bb88087676f81261a4eae913c492dafde184067c584bc7c05"},
+ {file = "identify-2.3.0.tar.gz", hash = "sha256:fd08c97f23ceee72784081f1ce5125c8f53a02d3f2716dde79a6ab8f1039fea5"},
]
idna = [
{file = "idna-3.2-py3-none-any.whl", hash = "sha256:14475042e284991034cb48e06f6851428fb14c4dc953acd9be9a5e95c7b6dd7a"},
@@ -2674,6 +2685,10 @@ isort = [
{file = "isort-5.9.3-py3-none-any.whl", hash = "sha256:e17d6e2b81095c9db0a03a8025a957f334d6ea30b26f9ec70805411e5c7c81f2"},
{file = "isort-5.9.3.tar.gz", hash = "sha256:9c2ea1e62d871267b78307fe511c0838ba0da28698c5732d54e2790bf3ba9899"},
]
+isosurfaces = [
+ {file = "isosurfaces-0.1.0-py3-none-any.whl", hash = "sha256:a3421f7e7115f72f8f1af538ac4723e5570b1aaa0ddfc6a86520d2d781f3e91f"},
+ {file = "isosurfaces-0.1.0.tar.gz", hash = "sha256:fa1b44e5e59d2f429add49289ab89e36f8dcda49b7badd99e0beea273be331f4"},
+]
jedi = [
{file = "jedi-0.18.0-py2.py3-none-any.whl", hash = "sha256:18456d83f65f400ab0c2d3319e48520420ef43b23a086fdc05dff34132f0fb93"},
{file = "jedi-0.18.0.tar.gz", hash = "sha256:92550a404bad8afed881a137ec9a461fed49eca661414be45059329614ed0707"},
@@ -3683,29 +3698,29 @@ virtualenv = [
{file = "virtualenv-20.8.1.tar.gz", hash = "sha256:bcc17f0b3a29670dd777d6f0755a4c04f28815395bca279cdcb213b97199a6b8"},
]
watchdog = [
- {file = "watchdog-2.1.5-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:5f57ce4f7e498278fb2a091f39359930144a0f2f90ea8cbf4523c4e25de34028"},
- {file = "watchdog-2.1.5-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:8b74d0d92a69a7ab5f101f9fe74e44ba017be269efa824337366ccbb4effde85"},
- {file = "watchdog-2.1.5-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:59767f476cd1f48531bf378f0300565d879688c82da8369ca8c52f633299523c"},
- {file = "watchdog-2.1.5-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:814d396859c95598f7576d15bc257c3bd3ba61fa4bc1db7dfc18f09070ded7da"},
- {file = "watchdog-2.1.5-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:28777dbed3bbd95f9c70f461443990a36c07dbf49ae7cd69932cdd1b8fb2850c"},
- {file = "watchdog-2.1.5-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:5cf78f794c9d7bc64a626ef4f71aff88f57a7ae288e0b359a9c6ea711a41395f"},
- {file = "watchdog-2.1.5-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:43bf728eb7830559f329864ab5da2302c15b2efbac24ad84ccc09949ba753c40"},
- {file = "watchdog-2.1.5-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:a7053d4d22dc95c5e0c90aeeae1e4ed5269d2f04001798eec43a654a03008d22"},
- {file = "watchdog-2.1.5-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:6f3ad1d973fe8fc8fe64ba38f6a934b74346342fa98ef08ad5da361a05d46044"},
- {file = "watchdog-2.1.5-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:41d44ef21a77a32b55ce9bf59b75777063751f688de51098859b7c7f6466589a"},
- {file = "watchdog-2.1.5-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:ed4ca4351cd2bb0d863ee737a2011ca44d8d8be19b43509bd4507f8a449b376b"},
- {file = "watchdog-2.1.5-pp37-pypy37_pp73-macosx_10_9_x86_64.whl", hash = "sha256:8874d5ad6b7f43b18935d9b0183e29727a623a216693d6938d07dfd411ba462f"},
- {file = "watchdog-2.1.5-py3-none-manylinux2014_aarch64.whl", hash = "sha256:50a7f81f99d238f72185f481b493f9de80096e046935b60ea78e1276f3d76960"},
- {file = "watchdog-2.1.5-py3-none-manylinux2014_armv7l.whl", hash = "sha256:e40e33a4889382824846b4baa05634e1365b47c6fa40071dc2d06b4d7c715fc1"},
- {file = "watchdog-2.1.5-py3-none-manylinux2014_i686.whl", hash = "sha256:78b1514067ff4089f4dac930b043a142997a5b98553120919005e97fbaba6546"},
- {file = "watchdog-2.1.5-py3-none-manylinux2014_ppc64.whl", hash = "sha256:58ae842300cbfe5e62fb068c83901abe76e4f413234b7bec5446e4275eb1f9cb"},
- {file = "watchdog-2.1.5-py3-none-manylinux2014_ppc64le.whl", hash = "sha256:b0cc7d8b7d60da6c313779d85903ce39a63d89d866014b085f720a083d5f3e9a"},
- {file = "watchdog-2.1.5-py3-none-manylinux2014_s390x.whl", hash = "sha256:e60d3bb7166b7cb830b86938d1eb0e6cfe23dfd634cce05c128f8f9967895193"},
- {file = "watchdog-2.1.5-py3-none-manylinux2014_x86_64.whl", hash = "sha256:51af09ae937ada0e9a10cc16988ec03c649754a91526170b6839b89fc56d6acb"},
- {file = "watchdog-2.1.5-py3-none-win32.whl", hash = "sha256:9391003635aa783957b9b11175d9802d3272ed67e69ef2e3394c0b6d9d24fa9a"},
- {file = "watchdog-2.1.5-py3-none-win_amd64.whl", hash = "sha256:eab14adfc417c2c983fbcb2c73ef3f28ba6990d1fff45d1180bf7e38bda0d98d"},
- {file = "watchdog-2.1.5-py3-none-win_ia64.whl", hash = "sha256:a2888a788893c4ef7e562861ec5433875b7915f930a5a7ed3d32c048158f1be5"},
- {file = "watchdog-2.1.5.tar.gz", hash = "sha256:5563b005907613430ef3d4aaac9c78600dd5704e84764cb6deda4b3d72807f09"},
+ {file = "watchdog-2.1.6-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:9693f35162dc6208d10b10ddf0458cc09ad70c30ba689d9206e02cd836ce28a3"},
+ {file = "watchdog-2.1.6-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:aba5c812f8ee8a3ff3be51887ca2d55fb8e268439ed44110d3846e4229eb0e8b"},
+ {file = "watchdog-2.1.6-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:4ae38bf8ba6f39d5b83f78661273216e7db5b00f08be7592062cb1fc8b8ba542"},
+ {file = "watchdog-2.1.6-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:ad6f1796e37db2223d2a3f302f586f74c72c630b48a9872c1e7ae8e92e0ab669"},
+ {file = "watchdog-2.1.6-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:922a69fa533cb0c793b483becaaa0845f655151e7256ec73630a1b2e9ebcb660"},
+ {file = "watchdog-2.1.6-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:b2fcf9402fde2672545b139694284dc3b665fd1be660d73eca6805197ef776a3"},
+ {file = "watchdog-2.1.6-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:3386b367e950a11b0568062b70cc026c6f645428a698d33d39e013aaeda4cc04"},
+ {file = "watchdog-2.1.6-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:8f1c00aa35f504197561060ca4c21d3cc079ba29cf6dd2fe61024c70160c990b"},
+ {file = "watchdog-2.1.6-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:b52b88021b9541a60531142b0a451baca08d28b74a723d0c99b13c8c8d48d604"},
+ {file = "watchdog-2.1.6-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:8047da932432aa32c515ec1447ea79ce578d0559362ca3605f8e9568f844e3c6"},
+ {file = "watchdog-2.1.6-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:e92c2d33858c8f560671b448205a268096e17870dcf60a9bb3ac7bfbafb7f5f9"},
+ {file = "watchdog-2.1.6-pp37-pypy37_pp73-macosx_10_9_x86_64.whl", hash = "sha256:b7d336912853d7b77f9b2c24eeed6a5065d0a0cc0d3b6a5a45ad6d1d05fb8cd8"},
+ {file = "watchdog-2.1.6-py3-none-manylinux2014_aarch64.whl", hash = "sha256:cca7741c0fcc765568350cb139e92b7f9f3c9a08c4f32591d18ab0a6ac9e71b6"},
+ {file = "watchdog-2.1.6-py3-none-manylinux2014_armv7l.whl", hash = "sha256:25fb5240b195d17de949588628fdf93032ebf163524ef08933db0ea1f99bd685"},
+ {file = "watchdog-2.1.6-py3-none-manylinux2014_i686.whl", hash = "sha256:be9be735f827820a06340dff2ddea1fb7234561fa5e6300a62fe7f54d40546a0"},
+ {file = "watchdog-2.1.6-py3-none-manylinux2014_ppc64.whl", hash = "sha256:d0d19fb2441947b58fbf91336638c2b9f4cc98e05e1045404d7a4cb7cddc7a65"},
+ {file = "watchdog-2.1.6-py3-none-manylinux2014_ppc64le.whl", hash = "sha256:3becdb380d8916c873ad512f1701f8a92ce79ec6978ffde92919fd18d41da7fb"},
+ {file = "watchdog-2.1.6-py3-none-manylinux2014_s390x.whl", hash = "sha256:ae67501c95606072aafa865b6ed47343ac6484472a2f95490ba151f6347acfc2"},
+ {file = "watchdog-2.1.6-py3-none-manylinux2014_x86_64.whl", hash = "sha256:e0f30db709c939cabf64a6dc5babb276e6d823fd84464ab916f9b9ba5623ca15"},
+ {file = "watchdog-2.1.6-py3-none-win32.whl", hash = "sha256:e02794ac791662a5eafc6ffeaf9bcc149035a0e48eb0a9d40a8feb4622605a3d"},
+ {file = "watchdog-2.1.6-py3-none-win_amd64.whl", hash = "sha256:bd9ba4f332cf57b2c1f698be0728c020399ef3040577cde2939f2e045b39c1e5"},
+ {file = "watchdog-2.1.6-py3-none-win_ia64.whl", hash = "sha256:a0f1c7edf116a12f7245be06120b1852275f9506a7d90227648b250755a03923"},
+ {file = "watchdog-2.1.6.tar.gz", hash = "sha256:a36e75df6c767cbf46f61a91c70b3ba71811dfa0aca4a324d9407a06a8b7a2e7"},
]
wcwidth = [
{file = "wcwidth-0.2.5-py2.py3-none-any.whl", hash = "sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784"},
diff --git a/pyproject.toml b/pyproject.toml
index 2e9925c3a..ed1ed22b6 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -54,6 +54,7 @@ requests = "*"
dearpygui = { version = "^0.8", optional = true }
screeninfo = "^0.6.7"
skia-pathops = "^0.7.0"
+isosurfaces = "0.1.0"
[tool.poetry.extras]
webgl_renderer = ["grpcio","grpcio-tools"]
diff --git a/tests/control_data/graphical_units_data/coordinate_system/implicit_graph.npz b/tests/control_data/graphical_units_data/coordinate_system/implicit_graph.npz
new file mode 100644
index 000000000..6d37ffbd5
Binary files /dev/null and b/tests/control_data/graphical_units_data/coordinate_system/implicit_graph.npz differ
diff --git a/tests/control_data/graphical_units_data/functions/ImplicitFunction.npz b/tests/control_data/graphical_units_data/functions/ImplicitFunction.npz
new file mode 100644
index 000000000..3ec761219
Binary files /dev/null and b/tests/control_data/graphical_units_data/functions/ImplicitFunction.npz differ
diff --git a/tests/test_graphical_units/test_coordinate_systems.py b/tests/test_graphical_units/test_coordinate_systems.py
index 3a78dc2b9..754516370 100644
--- a/tests/test_graphical_units/test_coordinate_systems.py
+++ b/tests/test_graphical_units/test_coordinate_systems.py
@@ -33,3 +33,10 @@ def test_line_graph(scene):
)
scene.add(plane, first_line, second_line)
+
+
+@frames_comparison
+def test_implicit_graph(scene):
+ ax = Axes()
+ graph = ax.get_implicit_curve(lambda x, y: x ** 2 + y ** 2 - 4)
+ scene.add(ax, graph)
diff --git a/tests/test_graphical_units/test_functions.py b/tests/test_graphical_units/test_functions.py
index 63599d2a5..731c649dd 100644
--- a/tests/test_graphical_units/test_functions.py
+++ b/tests/test_graphical_units/test_functions.py
@@ -8,3 +8,9 @@ __module_test__ = "functions"
def test_FunctionGraph(scene):
graph = FunctionGraph(lambda x: 2 * np.cos(0.5 * x), x_range=[-PI, PI], color=BLUE)
scene.add(graph)
+
+
+@frames_comparison
+def test_ImplicitFunction(scene):
+ graph = ImplicitFunction(lambda x, y: x ** 2 + y ** 2 - 9)
+ scene.add(graph)