The angle between the three peaks

For example, GetAngle ((0,0), (100,0), (100,100)) = 90. How do I find the angle between three 2D points.

+6
c ++ c algorithm
source share
4 answers

Given points A, B and C, do you need the angle between AB and AC? First, we calculate the vectors AB and AC - these are only the coordinates B of the minus coordinates A, as well as for AC. Take the dot product of two vectors. This is just the product of the x coordinate and the product of the y-coordinates of the vectors. Divide this number by the length AB and again by the length AC. This result is the cosine of the angle between the two vectors, so take arccos () and you have it.

+9
source share

The problem with using only a point product here is that it is unstable near 0 or 180 degrees - the slope of acos () approaches infinity near +/- 1.0, which will lead to a loss of accuracy.

To fix this, you can calculate the pseudo-cross product and use atan2 () as follows:

// given A, B, C are 2D points: BA= B - A; CA= C - A // vector subtraction, to get vector between points dot= BA.x * CA.x + BA.y * CA.y pcross= BA.x * CA.y - BA.y * CA.x angle= atan2(pcross, dot) // this should be the angle BAC, in radians 

This should be numerically reliable if one of the legs of the corner does not have zero length.

Please note that this will also give you an angle sign, depending on whether the BAC goes clockwise or counterclockwise; The acos () method will always give you a positive value. Of course, if you want only a positive angle, you can take abs(angle) ; the atan2 () method will still be more reliable and probably faster.

+6
source share

Use a point product:

(a,b,c) dot (d,e,f) = ad + be + bf .

 A dot B = length(A)*length(B)* cos(theta) 

theta = arccos((A dot B)/(length(A)*length(B)) - the angle between the vectors A and B.

+1
source share

This is easy if you have basic knowledge of linear algebra.

The vector v (in the sense of linear algebra, not std :: vector;)) is the collection v = (x, y, z).

The norm is the length of the vector | v | = sqrt (xx + yy + z * z)

The inner product of two vectors v1 = (x1, y1, z1) and v2 = (x2, y2, z2) is equal to v1 Β· v2 = x1 * x2 + y1 * y2 + z1 * z2

The angle of the vectors v1 and v2 is a = acos (v1 Β· v2 / (| v1 | * | v2 |))

0
source share

All Articles