Search code examples
manim

How to apply transform on Cartesian plane (NumberPlane()) to Polar plane?


In this video Grant performs a non linear transform. The transform from cartesian to polar is also non-linear. We find the determinant of the jacobian matrix to be 1/r. I could replicate the transform that Grant did, but how to animate this cartesian to polar using manim. In other words, what would be f1 and f2. I tried (x^2 + y^2)^0.5 and arctan(y/x)

def construct(self):
    def arctan(x, y):
        if x and y == 0:
            return 0
        else:
            return np.arctan(y / x)

    grid = NumberPlane()
    grid.prepare_for_nonlinear_transform()
    self.play(
        grid.apply_function,
        lambda p: p + np.array([
            (p[0]**2 + p[1]**2)**0.5,
            arctan(p[0], p[1]),
            0,
        ]),
        run_time=3,
    )

Solution

  • class AV(Scene):
        def construct(self):
            def polar2c(p):
                return np.array([
                    p[0]*np.cos(p[1]),
                    p[0]*np.sin(p[1]),
                    0
                    ])
    
            grid = NumberPlane(
                x_line_frequency=PI/4,
                y_line_frequency=PI/4,
                x_min=-PI,
                x_max=PI,
                y_min=-PI,
                y_max=PI
                )
            func = FunctionGraph(lambda x: 0.5*np.sin(5*x)+2,x_min=-PI,x_max=PI)
            grid.add(func)
            self.add(grid)
            grid.faded_lines[4:9].fade(1)
            grid.faded_lines[12:].fade(1)
            grid.background_lines[4:9].fade(1)
            grid.background_lines[12:].fade(1)
            self.play(Rotating(func,radians=PI,axis=UR,about_point=ORIGIN,run_time=2,rate_func=smooth))
            grid.generate_target()
            grid.target.prepare_for_nonlinear_transform()
            grid.target.apply_function(lambda p: polar2c(p))
    
            self.play(
                MoveToTarget(grid,run_time=4)
            )
            self.wait(3)