Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Improved the implementation of :meth:`.CoordinateSystem.get_area` to work without Riemann rectangles, added new :meth:`.CoordinateSystem.get_area_with_riemann_rectangles` (#1990)

leleogere522b83cdc798
diff --git a/docs/source/examples.rst b/docs/source/examples.rst
index 2172c522f..9cde03df9 100644
--- a/docs/source/examples.rst
+++ b/docs/source/examples.rst
@@ -366,10 +366,10 @@ Plotting with Manim
line_1 = ax.get_vertical_line(ax.input_to_graph_point(2, curve_1), color=YELLOW)
line_2 = ax.get_vertical_line(ax.i2gp(3, curve_1), color=YELLOW)
- area_1 = ax.get_area(curve_1, x_range=[0.3, 0.6], dx_scaling=40, color=BLUE)
- area_2 = ax.get_area(curve_2, [2, 3], bounded=curve_1, color=GREY, opacity=0.2)
+ riemann_area = ax.get_riemann_rectangles(curve_1, x_range=[0.3, 0.6], dx=0.03, color=BLUE, fill_opacity=0.5)
+ area = ax.get_area(curve_2, [2, 3], bounded_graph=curve_1, color=GREY, opacity=0.5)
- self.add(ax, labels, curve_1, curve_2, line_1, line_2, area_1, area_2)
+ self.add(ax, labels, curve_1, curve_2, line_1, line_2, riemann_area, area)
.. manim:: HeatDiagramPlot
:save_last_frame:
diff --git a/manim/mobject/coordinate_systems.py b/manim/mobject/coordinate_systems.py
index a2ee48231..f27b2694f 100644
--- a/manim/mobject/coordinate_systems.py
+++ b/manim/mobject/coordinate_systems.py
@@ -28,6 +28,7 @@ from ..mobject.geometry import (
DashedLine,
Dot,
Line,
+ Polygon,
Rectangle,
RegularPolygon,
)
@@ -1033,15 +1034,13 @@ class CoordinateSystem:
def get_area(
self,
graph: "ParametricFunction",
- x_range: Optional[Sequence[float]] = None,
+ x_range: Optional[Tuple[float, float]] = None,
color: Union[Color, Iterable[Color]] = [BLUE, GREEN],
opacity: float = 0.3,
- dx_scaling: float = 1,
- bounded: "ParametricFunction" = None,
+ bounded_graph: "ParametricFunction" = None,
**kwargs,
):
- """Returns a :class:`~.VGroup` of Riemann rectangles sufficiently small enough to visually
- approximate the area under the graph passed.
+ """Returns a :class:`~.Polygon` representing the area under the graph passed.
Examples
--------
@@ -1056,7 +1055,6 @@ class CoordinateSystem:
area = ax.get_area(
curve,
x_range=(PI / 2, 3 * PI / 2),
- dx_scaling=10,
color=(GREEN_B, GREEN_D),
opacity=1,
)
@@ -1066,37 +1064,56 @@ class CoordinateSystem:
Parameters
----------
graph
- The graph/curve for which the area needs to be determined.
+ The graph/curve for which the area needs to be gotten.
x_range
The range of the minimum and maximum x-values of the area. ``x_range = [x_min, x_max]``.
color
- The color of the area. Creates a gradient if an iterable of colors is provided.
+ The color of the area. Creates a gradient if a list of colors is provided.
opacity
The opacity of the area.
- bounded
+ bounded_graph
If a secondary :attr:`graph` is specified, encloses the area between the two curves.
- dx_scaling
- The factor by which the :attr:`dx` value is scaled.
kwargs
- Additional arguments to be passed to :meth:`~.CoordinateSystem.get_area`.
+ Additional parameters passed to :class:`~.Polygon`
Returns
-------
- :class:`~.VGroup`
- The :class:`~.VGroup` containing the Riemann Rectangles.
- """
+ :class:`~.Polygon`
+ The :class:`~.Polygon` representing the area.
- dx = kwargs.pop("dx", None) or self.x_range[2] / 500
- return self.get_riemann_rectangles(
- graph,
- x_range=x_range,
- dx=dx * dx_scaling,
- bounded_graph=bounded,
- blend=True,
- color=color,
- show_signed_area=kwargs.pop("show_signed_area", None) or False,
- **kwargs,
- ).set_opacity(opacity=opacity)
+ Raises
+ ------
+ :exc:`ValueError`
+ When x_ranges do not match (either area x_range, graph's x_range or bounded_graph's x_range).
+ """
+ if x_range is None:
+ a = graph.t_min
+ b = graph.t_max
+ else:
+ a, b = x_range
+ if bounded_graph is not None:
+ if bounded_graph.t_min > b:
+ raise ValueError(
+ f"Ranges not matching: {bounded_graph.t_min} < {b}",
+ )
+ if bounded_graph.t_max < a:
+ raise ValueError(
+ f"Ranges not matching: {bounded_graph.t_max} > {a}",
+ )
+ a = max(a, bounded_graph.t_min)
+ b = min(b, bounded_graph.t_max)
+
+ if bounded_graph is None:
+ points = (
+ [self.c2p(a)]
+ + [p for p in graph.points if a <= self.p2c(p)[0] <= b]
+ + [self.c2p(b)]
+ )
+ else:
+ points = [p for p in graph.points if a <= self.p2c(p)[0] <= b] + [
+ p for p in bounded_graph.points if a <= self.p2c(p)[0] <= b
+ ][::-1]
+ return Polygon(*points, **kwargs).set_opacity(opacity).set_color(color)
def angle_of_tangent(
self,
diff --git a/tests/control_data/graphical_units_data/plot/get_area.npz b/tests/control_data/graphical_units_data/plot/get_area.npz
index af2af7523..0b71a4ab9 100644
Binary files a/tests/control_data/graphical_units_data/plot/get_area.npz and b/tests/control_data/graphical_units_data/plot/get_area.npz differ
diff --git a/tests/control_data/graphical_units_data/plot/get_area_with_riemann_rectangles.npz b/tests/control_data/graphical_units_data/plot/get_area_with_riemann_rectangles.npz
new file mode 100644
index 000000000..8fc05274b
Binary files /dev/null and b/tests/control_data/graphical_units_data/plot/get_area_with_riemann_rectangles.npz differ
diff --git a/tests/test_graphical_units/test_axes.py b/tests/test_graphical_units/test_axes.py
index cba17bc07..589037fa6 100644
--- a/tests/test_graphical_units/test_axes.py
+++ b/tests/test_graphical_units/test_axes.py
@@ -177,16 +177,27 @@ def test_t_label(scene):
@frames_comparison
def test_get_area(scene):
ax = Axes().add_coordinates()
- curve = ax.get_graph(lambda x: 2 * np.sin(x), color=DARK_BLUE)
- area = ax.get_area(
- curve,
+ curve1 = ax.get_graph(
+ lambda x: 2 * np.sin(x),
+ x_range=[-5, ax.x_range[1]],
+ color=DARK_BLUE,
+ )
+ curve2 = ax.get_graph(lambda x: (x + 4) ** 2 - 2, x_range=[-5, -2], color=RED)
+ area1 = ax.get_area(
+ curve1,
x_range=(PI / 2, 3 * PI / 2),
- dx_scaling=10,
color=(GREEN_B, GREEN_D),
opacity=1,
)
+ area2 = ax.get_area(
+ curve1,
+ x_range=(-4.5, -2),
+ color=(RED, YELLOW),
+ opacity=0.2,
+ bounded_graph=curve2,
+ )
- scene.add(ax, curve, area)
+ scene.add(ax, curve1, curve2, area1, area2)
@frames_comparison