本文介绍了笛卡尔到极坐标(3d 坐标)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何在 3D 空间中的笛卡尔坐标系和极坐标系(和反坐标系)之间进行转换?最好有一个 c# 示例,但任何东西都将不胜感激.谢谢!

How do you convert between Cartesian and Polar (and back) coordinate systems in 3D space? Preferably with a c# example but anything would really be appreciated. Thanks!

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

EditWhen 20% of the change is taken into account (not forming a sphere)

编辑 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);
    }

推荐答案

从笛卡尔到极坐标:

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

从极坐标到笛卡尔坐标:

From Polar to Cartesian:

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

我还没有测试过.

您可以重写以减少浮点运算次数.

You can rewrite to reduce number of floating point operations.

这篇关于笛卡尔到极坐标(3d 坐标)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:14