Cartesian to Polar (3d coordinates)
Asked Answered
C

3

5

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!

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

enter image description here

Edit 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);
    }

enter image description here

Chemist answered 3/6, 2012 at 5:33 Comment(4)
Preferably show us what you've tried so far...Annmarieannnora
Why the down vote (whoever did)? I am asking because I have no idea how to do this, and there doesn't appear to be a question which includes the 3rd dimension on this site...Chemist
Which polar coordinate system are you talking about here? Cylindrical? Spherical?Landlordism
@JeffMercado Sphereical.Chemist
A
7

From Cartesian to Polar:

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)

I haven't tested it yet.

You can rewrite to reduce number of floating point operations.

Accrete answered 3/6, 2012 at 5:55 Comment(5)
Thanks! On first line second block I assume you meant 'x'?Chemist
Okay, I've updated my code - it doesn't seem to work properly (see edit)Chemist
@Darestium: Can you show your code? Note that I'm using distance, latitude and longitude to represent 3D polar coordinate.Accrete
@Darestium: I found an error in the formula. Please check again.Accrete
I have edited my answer again (and I have checked with en.wikipedia.org/wiki/Spherical_coordinate_system). If it doesn't work then I'll delete my answer.Accrete
L
2

It depends on how the azimuth is measured - from the horizontal plane or from the vertical axis. I've read the Wikipedia article, but If you measure it as geographical latitude (Equator=0, Poles =+90 and -90) then you should use asin and sin.

I'm using c# in a 3D-Modelling software and there the azimuth is measured with respect to the xy-Plane and not to the z-Axis. In my case the formulas are:

lat = asin(z / r)

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

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

z = r * sin(lat)

Lisettelisha answered 19/2, 2018 at 14:3 Comment(0)
J
1

In order to take in account the 4 quadrants:

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

That is implemented in the following functions, that I checked in the 4 quadrants:

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); }
Johst answered 19/11, 2021 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.