Search code examples
c#vectorangledegrees

How to convert z coordinate to an angle in c#?



I've been searching the net for hours to convert z-axis coordinate to an angle. I've already got x,y to angle and now to complete it i need the z. Here is the code for x,y:

        private float XYToDegrees(Point xy, Point origin)
    {
        int deltaX = origin.X - xy.X;
        int deltaY = origin.Y - xy.Y;

        double radAngle = Math.Atan2(deltaY, deltaX);
        double degreeAngle = radAngle * 180.0 / Math.PI;

        return (float)(180.0 - degreeAngle);
    }

and this code to run the function:

XYToDegrees(new Point(2334, -447), new Point(2433, -659)) - 270;

The purpose of the z coordinate to angle is to get the camera angle aligned with the object I'm looking at.


Solution

  • It is very simmilar, just another triangle

    private float XYZToDegrees(double deltaX, double deltaY, double deltaZ)
    {
        double deltaXY = Math.Sqrt(deltaY * deltaY + deltaX * deltaX);
        double radAngle = Math.Atan2(deltaZ, deltaXY);
        double degreeAngle = radAngle * 180.0 / Math.PI;
    
        return (float)degreeAngle;
    }