Comparison of two angles

Given the four points on the plane, A,B,X,Y , I want to determine which of the following two angles is less than ∢ABX or ∢ABY .

The angle ∢ABX defined as the angle BX when AB transferred to the open segment (-∞,0] . Intuitively, speaking ∢ABX , I mean the angle that you get when you turn left after visiting vertex B

I would prefer not to use cos or sqrt to maintain accuracy and minimize performance (the code will work in the embedded system).

In the case when A=(-1,0),B=(0,0) , I can compare the two angles ∢ABX and ∢ABY , calculating the point product of the vectors X,Y and see its sign.

What can I do in this case:

  • Determine whether or not the ABX turning right or left.
  • If ABX turns left, check whether Y or A on the same side of the line on the BX segment or not. If they are ∢ABX less than ABY .
  • If ABX turns right, then Y and A on the same side of BX means that ∢ABX greater than ∢ABY .

But that seems too complicated to me.

Any simpler approach?

+4
source share
6 answers

Here is some pseudo code. Does not detect a case where both angles are the same. Also does not apply to angular orientation, for example. takes all angles <= 180 degrees.

 v0 = AB v1 = XB v2 = YB dot1 = dot(v0, v1) dot2 = dot(v0, v2) if(dot1 > 0) if(dot2 < 0) // ABX is smaller if(dot1 * dot1 / dot(v1,v1) > dot2 * dot2 / dot(v2, v2) ) // ABX is smaller // ABY is smaller if(dot2 > 0) // ABY is smaller if(dot1 * dot1 / dot(v1,v1) > dot2 * dot2 / dot(v2,v2) ) // ABY is smaller // ABX is smaller 

Note that most of this excruciating pain goes away if you allow two square roots to be taken.

+2
source

Center the origin on B by doing

 X = X - B Y = Y - B A = A - B 

EDIT : You also need to normalize 3 vectors

 A = A / |A| X = X / |X| Y = Y / |Y| 

Find the two corners by doing

 acos(A dot X) acos(A dot Y) 

===

I do not understand the point of losing accuracy. You just compare, without changing the coordinates of the points in any way ...

+1
source

You might want to check out Rational Trigonometry . The ideas of distance and angle are replaced by the quadrant and scatter, which do not include sqrt and cos . See the bottom of this webpage to find out how the distribution between the two lines is calculated. The theme has its own website and even youtube channel .

+1
source

I would prefer not to use cos or sqrt to maintain accuracy.

It makes no sense.

But that seems too complicated to me.

This seems like a completely wrong direction to me.

Take the difference between the two vectors and look at the signs of the components.

What you need to be careful about is what less means. This idea is not very accurate as indicated. For example, if one point A is in quadrant 4 (x-component> 0 and y-component <0), and another point B is in quadrant 1 (x-component> 0 and y-component> 0), which makes "less "mean? The angle of the vector from the origin to A is between zero and? Pi / 2; the angle of the vector from the origin to B is between 3 [pi] / 4 and 2 [pi]. Which one is less?

0
source

Use the law of cosines: a**2 + b**2 - 2*a*b*cos(phi) = c**2

where a = | ax |, b = | bx | (| by |), c = | ab | (| ay |), and phi is your angle ABX (ABY)

0
source

I am not sure if sqrt can be dispensed with. Plain:

 AB = AB/|AB| XB = XB/|XB| YB = YB/|YB| if(dot(XB,AB) > dot (YB,AB)){ //<ABY is grater } else { ... } 
0
source

Source: https://habr.com/ru/post/1310658/


All Articles