【问题标题】:Cartesian to Polar (3d coordinates)笛卡尔到极坐标(3d 坐标)
【发布时间】:2012-06-03 05:33:05
【问题描述】:

如何在 3D 空间中在笛卡尔坐标系和极坐标系之间进行转换?最好使用 c# 示例,但任何事情都会受到赞赏。谢谢!

编辑 当考虑到 20% 的变化时(不形成一个球体)

编辑 2

private void Spherise() {
        for (int i = 0; i < vertices.Count; i++) {
            float radius = this.radius;
            float longitude = 0;
            float latitude = 0;

            float sphereRadius = 32;

            Color color = vertices[i].Color;

            ToPolar(vertices[i].Position - centre, out radius, out longitude, out latitude);
            Vector3 position = ToCartesian(sphereRadius, longitude, latitude) + centre;

            Vector3 normal = vertices[i].Position - centre;
            normal.Normalize();

            const float lerpAmount = 0.6f;
            Vector3 lerp = (position - vertices[i].Position) * lerpAmount + vertices[i].Position;
            vertices[i] = new VertexPositionColorNormal(lerp, color, normal);
        }
    }

    private void ToPolar(Vector3 cart, out float radius, out float longitude, out float latitude) {
        radius = (float)Math.Sqrt((double)(cart.X * cart.X + cart.Y * cart.Y + cart.Z * cart.Z));
        longitude = (float)Math.Acos(cart.X / Math.Sqrt(cart.X * cart.X + cart.Y * cart.Y)) * (cart.Y < 0 ? -1 : 1);
        latitude = (float)Math.Acos(cart.Z / radius) * (cart.Z < 0 ? -1 : 1);
    }

    private Vector3 ToCartesian(float radius, float longitude, float latitude) {
        float x = radius * (float)(Math.Sin(latitude) * Math.Cos(longitude));
        float y = radius * (float)(Math.Sin(latitude) * Math.Sin(longitude));
        float z = radius * (float)Math.Cos(latitude);

        return new Vector3(x, y, z);
    }

【问题讨论】:

  • 最好向我们展示您到目前为止所尝试的内容...
  • 为什么投反对票(不管是谁)?我之所以问,是因为我不知道该怎么做,而且似乎没有一个问题包括这个网站上的第 3 维......
  • 这里说的是哪个极坐标系?圆柱形?球形?
  • @JeffMercado Sphereical。

标签: c# xna cartesian polar-coordinates


【解决方案1】:

从笛卡尔到极地:

r = sqrt(x * x + y * y + z * z)
long = acos(x / sqrt(x * x + y * y)) * (y < 0 ? -1 : 1)
lat = acos(z / r)

从极地到笛卡尔:

x = r * sin(lat) * cos(long)
y = r * sin(lat) * sin(long)
z = r * cos(lat)

我还没有测试过。

您可以重写以减少浮点运算的数量。

【讨论】:

  • 谢谢!在第一行第二块我假设你的意思是'x'?
  • 好的,我已经更新了我的代码——它似乎不能正常工作(见编辑)
  • @Darestium:你能展示你的代码吗?请注意,我使用距离、纬度和经度来表示 3D 极坐标。
  • @Darestium:我发现公式中有错误。请再次检查。
  • 我再次编辑了我的答案(我已经与en.wikipedia.org/wiki/Spherical_coordinate_system 核对过)。如果它不起作用,那么我将删除我的答案。
【解决方案2】:

这取决于方位角的测量方式 - 从水平面或从垂直轴。我已阅读 Wikipedia 文章,但如果您将其测量为地理纬度(赤道 = 0,波兰 = +90 和 -90),那么您应该使用 asinsin .

我在 3D 建模软件中使用 c#,并且方位角是相对于 xy 平面而不是 z 轴测量的。就我而言,公式是:

lat = asin(z / r)

x = r * cos(lat) * cos(long)

y = r * cos(lat) * sin(long)

z = r * sin(纬度)

【讨论】:

    【解决方案3】:

    为了考虑到 4 个象限:

    r = sqrt(x * x + y * y + z * z)
    long = atan2(y,x);
    lat = acos(z / r);
    

    这是在以下功能中实现的,我检查了 4 个象限:

    double modulo(vector <double> xyz) { return sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2] + 1e-130); }
    void cartesian_to_polar(vector <double> a, double& r, double& lat, double& lon) { r = modulo(a); lon = atan2(a[1], a[0]); lat = acos(a[2] / r); }
    void polar_to_cartesian(double r, double lat, double lon, vector <double>& a) { a[2] = r * cos(lat); a[0] = r * sin(lat) * cos(lon); a[1] = r * sin(lat) * sin(lon); }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      相关资源
      最近更新 更多