Search code examples
manim

Polygon set_width and set_height not work


There is right part inverse proportional function (1/x) in the scene, in the begining I draw a rectangle(using polygon), the left down corner is (0, 0, 0), thei right up corner is on the function with x=3.

I override update_mobjects, and update the postion of the right upper corner of rectangle with some point on the function, but not work, the width increase instead of decrease, please help, thanks!

my code is as below:

class Chess(GraphScene):
    CONFIG = {
        "x_min": -5,
        "x_max": 5,
        "y_min": -5,
        "y_max": 6,
        "y_axis_height": 10,
        "graph_origin": ORIGIN,
        "x_labeled_nums": range(-5, 5, 1),
        "y_labeled_nums": range(-5, 6, 1)
    }

    def construct(self):
        self.setup_axes(animate=True)
        if1r = self.get_graph(lambda x: 1 / x, color=YELLOW_D, x_min=1 / self.y_max, x_max=self.x_max)
        self.play(FadeIn(if1r))
        spt1 = self.input_to_graph_point(3, if1r)
        sg = Polygon(np.array([0, 0, 0]), np.array([spt1[0], 0, 0]), spt1, np.array([0, spt1[1], 0]), fill_color=RED_D,
                     fill_opacity=1.0)
        self.add(sg)
        self.sg = sg
        self.sva1 = 3
        self.if1r = if1r
        self.moving3()

    def moving3(self):
        self.always_update_mobjects = True
        self.wait(10)

    def update_mobjects(self, dt):
        if (hasattr(self, "sva1") and self.sva1 > 1):
            self.sva1 = self.sva1 - 0.01
            spt2 = self.input_to_graph_point(self.sva1, self.if1r)
            self.sg.set
            self.sg.set_width(spt2[0], stretch=False, about_point=self.sg.get_corner(DOWN + LEFT))
            self.sg.set_height(spt2[1], stretch=False, about_point=self.sg.get_corner(DOWN+LEFT))

Solution

  • Yes it works, but you forgot to change stretch=True in the last two lines.

    In addition, the line self.sg.set left over.

    If you allow me the comment, I think you have better control in this way: More examples here

    class NewChess(GraphScene):
        CONFIG = {
            "x_min": -5,
            "x_max": 5,
            "y_min": -5,
            "y_max": 6,
            "y_axis_height": 10,
            "graph_origin": ORIGIN,
            "x_labeled_nums": range(-5, 5, 1),
            "y_labeled_nums": range(-5, 6, 1),
            "x_start":3,
            "x_end":1
        }
    
        def get_rectangle_from_point(self,point):
            return Polygon(
                [0, 0, 0],          #(0,0)
                [point[0], 0, 0],   #(x,0)
                point,              #(x,y)
                [0, point[1], 0],   #(0,y)
                fill_color=RED_D,
                fill_opacity=1.0
            )
    
        def construct(self):
            self.setup_axes()
            x_coord_tracker=ValueTracker(self.x_start)
            if1r = self.get_graph(lambda x: 1 / x, color=YELLOW_D, x_min=1 / self.y_max, x_max=self.x_max)
            spt1 = self.input_to_graph_point(x_coord_tracker.get_value(), if1r)
            sg = self.get_rectangle_from_point(spt1)
            self.play(FadeIn(if1r))
            self.play(FadeIn(sg))
    
            def update_rectangle(rectangle):
                new_coord=self.input_to_graph_point(x_coord_tracker.get_value(), if1r)
                rectangle.become(self.get_rectangle_from_point(new_coord))
                return rectangle
    
            sg.add_updater(update_rectangle)
            self.add(sg) # Add object again
    
            self.play(
                x_coord_tracker.set_value,self.x_end,
                rate_func=linear,
                run_time=2
                )
            self.wait()