从地平线获得一条线的角度

VOX*_*VOX 7 c# math geometry drawing

我想知道如何从水平轴X获得AB线的角度.SO中的其他问题仅在两条线之间进行.我知道我总是可以绘制第二行AC并计算,但我想知道是否有更快的方法.

编辑:我很确定我没有做过早优化.

Bar*_*lom 10

你可以用atan它.

angle = atan((By-Ay)/(Bx-Ax))
Run Code Online (Sandbox Code Playgroud)


小智 8

    private double Angulo(int x1, int y1, int x2, int y2)
    {
        double degrees;

        // Avoid divide by zero run values.
        if (x2 - x1 == 0)
        {
            if (y2 > y1)
                degrees = 90;
            else
                degrees = 270;
        }
        else
        {
            // Calculate angle from offset.
            double riseoverrun = (double)(y2 - y1) / (double)(x2 - x1);
            double radians = Math.Atan(riseoverrun);
            degrees = radians * ((double)180 / Math.PI);

            // Handle quadrant specific transformations.       
            if ((x2 - x1) < 0 || (y2 - y1) < 0)
                degrees += 180;
            if ((x2 - x1) > 0 && (y2 - y1) < 0)
                degrees -= 180;
            if (degrees < 0)
                degrees += 360;
        }
        return degrees;
    }
Run Code Online (Sandbox Code Playgroud)