c#如何从水平和垂直角度获取XYZ坐标

时间:2018-01-26 05:30:25

标签: c# math coordinates degrees

我一直试图让这段代码在过去一小时内正常运行,我几乎完成了它。一切正常,但float verticalDegrees

详细问题:如何使此代码正常工作,以便从水平度,垂直度,半径和原点返回XYZ?

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

这是我到目前为止所做的:

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

2 个答案:

答案 0 :(得分:1)

此方法将球面坐标转换为笛卡尔坐标:

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

其中theta是'水平'或'方位角'角度(x-y平面中x轴的角度),phi是'倾斜'(角度来自正面) z轴)或'垂直'角。radius是笛卡尔坐标中给定点(x,y,z)的距离。

答案 1 :(得分:0)

似乎你有spherical coordinate s并希望获得笛卡尔坐标。在这种情况下

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

此处fi是您的"水平角度",theta是"垂直角度",x0..z0是原点坐标