Search code examples
manim

Distance from a point on axes using manim


I am having trouble to draw a horizontal line on axes. I made a point on the axes and I don't get any idea how can I draw a line on the x or y axes from the point and even don't know how to make a distance between two points. Any idea how to do them?

from manimlib.imports import *

class GraphX(GraphScene):
    CONFIG ={
        'x_min': -4,
        'x_max': 4,
        'y_min': -2,
        'y_max': 2,
        'x_axis_label': '$x$',
        'y_axis_label': '$y$',
        'graph_origin': 0.5 * DOWN + 0 * LEFT,
    }

def show_function_graph(self):
    self.setup_axes(animate = True)
    text = TextMobject('(x,y)')
    text.shift(2*UP+3*RIGHT)
    self.play(Write(text))
    self.wait(3)

def construct(self):
    self.show_function_graph()

Solution

  • graph coords and screen coords are not synced, you have to use coord_to_point and points_to_coords to convert between them

    here's an example

    class GraphX(GraphScene):
        CONFIG ={
            'x_min': -4, 'x_max': 4,
            'y_min': -2, 'y_max': 2,
            'x_axis_label': '$x$', 'y_axis_label': '$y$',
            # 'graph_origin': 0.5 * DOWN + 0 * LEFT,
            'graph_origin': ORIGIN
        }
    
        def draw_graph(self): self.setup_axes(animate = True)
    
        def construct(self):
            self.draw_graph()
    
            # dot = Dot(self.coords_to_point(x=1,y=1))
            dot = Dot(self.coords_to_point(1,1))
            self.play(ShowCreation(dot))
            text = Text('(x,y)')
            text.shift(dot.arc_center+0.5*UR)
            self.play(ShowCreation(text))
            self.wait()
    
            hl = Line( self.coords_to_point(1,1), self.coords_to_point(0,1))
            vl = Line( self.coords_to_point(1,1), self.coords_to_point(1,0))
            self.play(ShowCreation(hl), ShowCreation(vl))
            self.wait(3)