Example #1
0
    def get_riemann_rectangles(
        self,
        graph,
        x_min=None,
        x_max=None,
        dx=0.1,
        input_sample_type="left",
        stroke_width=1,
        stroke_color=BLACK,
        fill_opacity=1,
        start_color=None,
        end_color=None,
        show_signed_area=True,
        width_scale_factor=1.001
    ):
        x_min = x_min if x_min is not None else self.x_min
        x_max = x_max if x_max is not None else self.x_max
        if start_color is None:
            start_color = self.default_riemann_start_color
        if end_color is None:
            end_color = self.default_riemann_end_color
        rectangles = VGroup()
        x_range = np.arange(x_min, x_max, dx)
        colors = color_gradient([start_color, end_color], len(x_range))
        for x, color in zip(x_range, colors):
            if input_sample_type == "left":
                sample_input = x
            elif input_sample_type == "right":
                sample_input = x + dx
            elif input_sample_type == "center":
                sample_input = x + 0.5 * dx
            else:
                raise Exception("Invalid input sample type")
            graph_point = self.input_to_graph_point(sample_input, graph)
            points = VGroup(*list(map(VectorizedPoint, [
                self.coords_to_point(x, 0),
                self.coords_to_point(x + width_scale_factor * dx, 0),
                graph_point
            ])))

            rect = Rectangle()
            rect.replace(points, stretch=True)
            if graph_point[1] < self.graph_origin[1] and show_signed_area:
                fill_color = invert_color(color)
            else:
                fill_color = color
            rect.set_fill(fill_color, opacity=fill_opacity)
            rect.set_stroke(stroke_color, width=stroke_width)
            rectangles.add(rect)
        return rectangles
Example #2
0
    def get_riemann_rectangles(
        self,
        graph,
        x_min=None,
        x_max=None,
        dx=0.1,
        input_sample_type="left",
        stroke_width=1,
        stroke_color=BLACK,
        fill_opacity=1,
        start_color=None,
        end_color=None,
        show_signed_area=True,
        width_scale_factor=1.001
    ):
        x_min = x_min if x_min is not None else self.x_min
        x_max = x_max if x_max is not None else self.x_max
        if start_color is None:
            start_color = self.default_riemann_start_color
        if end_color is None:
            end_color = self.default_riemann_end_color
        rectangles = VGroup()
        x_range = np.arange(x_min, x_max, dx)
        colors = color_gradient([start_color, end_color], len(x_range))
        for x, color in zip(x_range, colors):
            if input_sample_type == "left":
                sample_input = x
            elif input_sample_type == "right":
                sample_input = x + dx
            elif input_sample_type == "center":
                sample_input = x + 0.5 * dx
            else:
                raise Exception("Invalid input sample type")
            graph_point = self.input_to_graph_point(sample_input, graph)
            points = VGroup(*list(map(VectorizedPoint, [
                self.coords_to_point(x, 0),
                self.coords_to_point(x + width_scale_factor * dx, 0),
                graph_point
            ])))

            rect = Rectangle()
            rect.replace(points, stretch=True)
            if graph_point[1] < self.graph_origin[1] and show_signed_area:
                fill_color = invert_color(color)
            else:
                fill_color = color
            rect.set_fill(fill_color, opacity=fill_opacity)
            rect.set_stroke(stroke_color, width=stroke_width)
            rectangles.add(rect)
        return rectangles
Example #3
0
 def add_shadow_mobjects(self,
                         shadow=2,
                         mobject=None,
                         color=WHITE,
                         opacity=0.9,
                         shift=([0.03, -0.01, 0]),
                         **kwargs):
     from manimlib.mobject.types.vectorized_mobject import VGroup
     group = VGroup()
     if mobject == None:
         mobject = self
     if shadow == 2:
         if isinstance(mobject, (Mobject)):
             mobject_2 = mobject.copy()
             mobject = mobject.copy()
         if isinstance(color, (list, tuple)) and len(color) == 2:
             color_2 = color[1]
             color = color[0]
         else:
             color_2 = invert_color(color)
         if isinstance(opacity, (list, tuple)) and len(opacity) == 2:
             opacity_2 = opacity[1]
             opacity = opacity[0]
         else:
             opacity_2 = opacity
         if isinstance(shift, (tuple)) and len(shift) == 2:
             shift_2 = shift[1]
             shift = shift[0]
         else:
             shift_2 = [each * -1 for each in shift]
         group.add(
             mobject_2.set_color(color_2).set_opacity(opacity_2).shift(
                 shift_2))  #
     elif shadow == 1:
         mobject = mobject.copy()
     else:
         return self
     group.add(mobject.set_color(color).set_opacity(opacity).shift(shift))
     self.remove(self.submobjects)
     return self.add_to_back(group, **kwargs).push_self_into_submobjects()
    def get_riemann_rectangles(self,
                               graph,
                               x_min=None,
                               x_max=None,
                               dx=0.1,
                               input_sample_type="left",
                               stroke_width=1,
                               stroke_color=BLACK,
                               fill_opacity=1,
                               start_color=None,
                               end_color=None,
                               show_signed_area=True,
                               width_scale_factor=1.001):
        """
        This method returns the VGroup() of the Riemann Rectangles for
        a particular curve.

        Parameters
        ----------
        graph (ParametricFunction)
            The graph whose area needs to be approximated
            by the Riemann Rectangles.

        x_min Union[int,float]
            The lower bound from which to start adding rectangles

        x_max Union[int,float]
            The upper bound where the rectangles stop.

        dx Union[int,float]
            The smallest change in x-values that is 
            considered significant.

        input_sample_type str
            Can be any of "left", "right" or "center

        stroke_width : Union[int, float]
            The stroke_width of the border of the rectangles.

        stroke_color : str
            The string of hex colour of the rectangle's border.

        fill_opacity Union[int, float]
            The opacity of the rectangles.

        start_color : str,
            The hex starting colour for the rectangles,
            this will, if end_color is a different colour,
            make a nice gradient.

        end_color : str,
            The hex ending colour for the rectangles,
            this will, if start_color is a different colour,
            make a nice gradient.

        show_signed_area : bool (True)
            Whether or not to indicate -ve area if curve dips below
            x-axis.

        width_scale_factor : Union[int, float]
            How much the width of the rectangles are scaled by when transforming.

        Returns
        -------
        VGroup
            A VGroup containing the Riemann Rectangles.

        """
        x_min = x_min if x_min is not None else self.x_min
        x_max = x_max if x_max is not None else self.x_max
        if start_color is None:
            start_color = self.default_riemann_start_color
        if end_color is None:
            end_color = self.default_riemann_end_color
        rectangles = VGroup()
        x_range = np.arange(x_min, x_max, dx)
        colors = color_gradient([start_color, end_color], len(x_range))
        for x, color in zip(x_range, colors):
            if input_sample_type == "left":
                sample_input = x
            elif input_sample_type == "right":
                sample_input = x + dx
            elif input_sample_type == "center":
                sample_input = x + 0.5 * dx
            else:
                raise Exception("Invalid input sample type")
            graph_point = self.input_to_graph_point(sample_input, graph)
            points = VGroup(*list(
                map(VectorizedPoint, [
                    self.coords_to_point(x, 0),
                    self.coords_to_point(x + width_scale_factor *
                                         dx, 0), graph_point
                ])))

            rect = Rectangle()
            rect.replace(points, stretch=True)
            if graph_point[1] < self.graph_origin[1] and show_signed_area:
                fill_color = invert_color(color)
            else:
                fill_color = color
            rect.set_fill(fill_color, opacity=fill_opacity)
            rect.set_stroke(stroke_color, width=stroke_width)
            rectangles.add(rect)
        return rectangles