get angle of a line from horizon

久未见 提交于 2019-12-23 08:35:05

问题


I want to know how to get an angle of a line A-B from horizontal axis X. Other questions in SO do that only between two lines. I'm aware I can always draw second line A-C and calculate but I'm wondering if there's a faster method.

EDIT: I'm very sure I'm not doing a premature optimization.


回答1:


You can use atan for that.

angle = atan((By-Ay)/(Bx-Ax))



回答2:


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



回答3:


If

  1. The angle is small,
  2. you can live with small inaccuracies, and
  3. You can use the angle in radians and not degrees,

then there is a fast solution: Under these conditions, you can assume that tan(a) = a = atan(a), and hence just omit the atan() call.




回答4:


You could also use arccosine, if your line is in the form [r_x,r_y], where r_x is the change in x and r_y is the change in y.

angle = arccos( r_x/( r_x*r_x + r_y*r_y ) )

It's slightly more opaque, but it's basically the dot product law:

angle = arccos (r . v)

Where r and v are both unit vectors (vectors of length 1). In our case, v is the vector [1,0], and r is

[r_x,r_y] / (r_x^2+r_y^2)

in order to make it a unit vector.




回答5:


The x-axis is actually a line with equation

y = 0

so you could use the solution you have already.




回答6:


If you need all four quadrants, Atan2 is more suitable than Atan.

public static int GetAngleBetweenPoints(PointF pt1, PointF pt2)
{
    float dx = pt2.X - pt1.X;
    float dy = pt2.Y - pt1.Y;

    int deg = Convert.ToInt32(Math.Atan2(dy, dx) * (180 / Math.PI));
    if (deg < 0) { deg += 360; }

    return deg;
}


来源:https://stackoverflow.com/questions/3153861/get-angle-of-a-line-from-horizon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!