0

I've been trying to get this code to work properly for the past hour and I almost got it complete. Everything works, but the float verticalDegrees.

In Detail Question: How do I get this code working so it returns XYZ from horizontal degrees, vertical degrees, radius and origin?

This link helped me, but it's missing Z coordinate

This is what I have so far:

    private float[] DegreesToXYZ(float horizonalDegrees, float verticalDegrees, float radius, float[] origin)
    {
        float[] xyz = new float[3];
        double radiansH = horizonalDegrees * Math.PI / 180.0;
        double radiansV = verticalDegrees * Math.PI / 180.0;

        xyz[1] = (float)Math.Cos(radiansH) * radius + origin[1];
        xyz[0] = (float)Math.Sin(-radiansH) * radius + origin[0];
        double deltaXY = Math.Sqrt(origin[0] * origin[0] + origin[1] * origin[1]);
        xyz[2] = (float)Math.Atan2(origin[2], deltaXY);
        return xyz;
    }
M Ross
  • 3
  • 2

2 Answers2

1

This method converts spherical coordinates into cartesian coordinates:

    private static double[] DegreesToXYZ(double radius, double theta, double phi, double[] originXYZ)
    {
        theta *= Math.PI / 180;//converting degress into radians
        phi *= Math.PI / 180;//converting degress into radians

        double[] xyz = new double[3];

        xyz[0] = originXYZ[0] + radius * Math.Cos(theta) * Math.Sin(phi);//x
        xyz[1] = originXYZ[1] + radius * Math.Sin(theta) * Math.Sin(phi);//y
        xyz[2] = originXYZ[2] + radius * Math.Cos(phi);//z

        return xyz;
    }

Where theta is the 'horizontal' or 'azimuth' angle (angle from the x-axis in the x-y plane), and phi is the 'inclination' (angle from the positive z axis) or 'vertical' angle.The radius is the distance to a given point (x,y,z) in cartesian coordinates.

Slaven Tojić
  • 2,945
  • 2
  • 14
  • 33
  • It seems to be working although the horizontal and vertical are inverted. e.g: when I turn it goes opposite direction. How can I fix this? – M Ross Jan 26 '18 at 08:42
0

Seems you have spherical coordinates and want to get Cartesian coordinates. In this case

 x = x0 + r * Cos(fi)  * Sin(theta)
 y = y0 + r * Sin(fi)  * Sin(theta)
 z = z0 + r * Cos(theta)

Here fi is your "horizontal angle", theta is "vertical angle", x0..z0 are origin coordinates

MBo
  • 77,366
  • 5
  • 53
  • 86