How do you know from two points A = (x0,y0) , B = (x1,y1) , what angle is the segment AB with the axis Ox ?

  • It's a simple school geometry, not a cool алгоритм ... Find the difference in x, y, divide, and consider arctangent. - AivanF.

3 answers 3

for example

 atan2(y1 - y0, x1 - x0) 

    There is a beautiful scalar product of vectors: enter image description here

    So, since the vector AB = (x1-x0,y1-y0) , it is clear that the cosine of the angle is (x1-x0)/sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)) . Since the points you have are not the same, the division by 0 will not be 100%.

    PS But the @VladD solution is more correct. I forgot about atan2 , only atan remembered ...

    • Why more correct? - Viktor
    • one
      @viktor Because with a single function, it bypasses all divisions by zero, and moreover, it accurately takes into account the quadrant. - Harry
    • But the solution with the scalar product is generalized to the three-dimensional case, in contrast to atan2 . - VladD
    • @VladD This is true, but there is such a specific task, it is also tied to the Ox axis, that atan2 better for it ... - Harry

    The wording of the question is ambiguous. The geometric angle between the rays AB and OX should be calculated by the formula

    atan2(abs(y1-y0),x1-x0)