From 16f5890fd3c18a1e4594345b9d8b6b972c96b3ed Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Sat, 10 Jun 2023 09:20:28 -0700 Subject: [PATCH] Add CoordianteSystem.get_area_under_graph This is not perfect, since one could optionally add a different color for negative area. --- manimlib/mobject/coordinate_systems.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/manimlib/mobject/coordinate_systems.py b/manimlib/mobject/coordinate_systems.py index bfa3f2b1..552b4703 100644 --- a/manimlib/mobject/coordinate_systems.py +++ b/manimlib/mobject/coordinate_systems.py @@ -22,6 +22,7 @@ from manimlib.mobject.types.dot_cloud import DotCloud from manimlib.mobject.types.surface import ParametricSurface from manimlib.mobject.types.vectorized_mobject import VGroup from manimlib.mobject.types.vectorized_mobject import VMobject +from manimlib.utils.bezier import inverse_interpolate from manimlib.utils.dict_ops import merge_dicts_recursively from manimlib.utils.simple_functions import binary_search from manimlib.utils.space_ops import angle_of_vector @@ -398,9 +399,24 @@ class CoordinateSystem(ABC): rect.set_fill(negative_color) return result - def get_area_under_graph(self, graph, x_range, fill_color=BLUE, fill_opacity=1): - # TODO - pass + def get_area_under_graph(self, graph, x_range, fill_color=BLUE, fill_opacity=0.5): + if not hasattr(graph, "x_range"): + raise Exception("Argument `graph` must have attribute `x_range`") + + alpha_bounds = [ + inverse_interpolate(*graph.x_range, x) + for x in x_range + ] + sub_graph = graph.copy() + sub_graph.pointwise_become_partial(graph, *alpha_bounds) + sub_graph.add_line_to(self.c2p(x_range[1], 0)) + sub_graph.add_line_to(self.c2p(x_range[0], 0)) + sub_graph.add_line_to(sub_graph.get_start()) + + sub_graph.set_stroke(width=0) + sub_graph.set_fill(fill_color, fill_opacity) + + return sub_graph class Axes(VGroup, CoordinateSystem):