You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Manim中实现笛卡尔坐标到极坐标的变换动画

笛卡尔坐标转极坐标的Manim变换演示实现

核心变换规则

按照参考公式,笛卡尔坐标$(x,y)$到极坐标的映射逻辑为:平面内任意点变换后的新坐标,横坐标为点到原点的距离$r$,纵坐标为点与x轴正方向的夹角$\theta$,计算规则如下:

  • $r = \sqrt{x^2 + y^2}$
  • $\theta = \arctan2(y, x)$

笛卡尔转极坐标参考公式

原有代码问题

你的测试代码存在两个核心错误,无法输出正确效果:

  1. 变换公式方向写反:pos[0]*np.cos(pos[1]), pos[0]*np.sin(pos[1])是极坐标转笛卡尔坐标的计算逻辑,和需求方向相反
  2. 额外添加了无意义的点乘操作np.dot(..., pos),完全破坏了坐标映射逻辑

正确实现代码

import numpy as np
from manim import *

class Transformation(LinearTransformationScene):
    def construct(self):
        # 保留你原本的线段测试用例,可自行替换为其他需要演示变换的图形
        line = Line(
            start=np.array([0,0,0]),
            end=np.array([2,1,0])
        )
        # 如需展示网格、基向量变换效果,可取消下面两行注释
        # self.add_transformable_mobject(self.get_basis_vectors())
        # self.add(self.get_grid())

        # 严格按数学公式实现映射,未使用复平面逻辑,后续拓展自定义函数只需修改该函数内的坐标计算规则即可
        def cart2polar(pos):
            x, y = pos[0], pos[1]
            r = np.sqrt(x**2 + y**2)
            theta = np.arctan2(y, x)
            return np.array([r, theta, 0])

        self.add_transformable_mobject(line)
        self.apply_nonlinear_transformation(cart2polar, run_time=5)
        self.wait(3)

拓展说明:如果需要实现极坐标转笛卡尔、或其他自定义坐标变换,只需要替换cart2polar函数内的坐标计算逻辑,整体演示框架不需要改动。

内容的提问来源于stack exchange,提问作者Abdullah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 06:18:17