Get the angle of the line from the horizon

I want to know how to get the angle of the line AB from the horizontal axis X. Other questions in SO do this only between two lines. I know that I can always draw the second line of AC and compute, but I wonder if there is a faster method.

EDIT: I am very sure that I am not doing premature optimization.

+7
math c # geometry drawing
source share
6 answers

You can use atan for this.

 angle = atan((By-Ay)/(Bx-Ax)) 
+9
source share
  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; } 
+6
source share

If a

  • The angle is small
  • you can live with small inaccuracies and
  • You can use the angle in radians, not degrees,

then there is a quick solution: under these conditions, you can assume that tan (a) = a = atan (a) and therefore simply omit the call to atan ().

+1
source share

You can also use arccosine if your string 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 ) ) 

This is a bit more opaque, but it is basically the law of the product in terms of:

 angle = arccos (r . v) 

Where r and v are 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) 

to make it a unit vector.

+1
source share

The x axis is actually a line with the equation

y = 0

so you can use your existing solution.

0
source share

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; } 
0
source share

All Articles