define

The atan2 method returns a value between -pi and PI representing the offset Angle corresponding to the point (x, y). This is a counterclockwise Angle, in radians, between the positive X-axis and the point (X, y) and the origin. The function takes the following arguments: pass the y coordinate first, then the x coordinate.

Pay attention to the point

  1. The undifferentiated quadrants return values between [-π, π]
  2. The function takes the following arguments: pass the y coordinate first, then the x coordinate, note: it is the coordinate of a point
  3. Returns a counterclockwise rotation of radians
  4. Atan2 accepts separate x and y parameters, while ATAN accepts the ratio of the two parameters
  5. Angle = Radians *180/ math.pi

How do you conceptually understand the line segment from the origin (0,0) to (x,y)? The origin (0,0) is defined with respect to who?

By adjusting the coordinate system automatically, we can freely calculate the Angle corresponding to the position in different quadrants

So what does origin mean on a page? That’s the top left vertex, that’s the origin. The radian value of a line segment rotated counterclockwise from the origin to a point (coordinate) is the return value of math.atan2 ()

practice

Math.atan2(45.0) *180/Math.PI    / / 90
Math.atan2(0.0) *180/Math.PI     / / 0
Math.atan2(0.45) *180/Math.PI     / / 0
Math.atan2(-180.180) *180/Math.PI  / / - 45
Copy the code

How to calculate the inclination Angle between two points?

Math.atan2() returns the inclination of the line between the points (x,y) and the origin (0,0). So how do you calculate the inclination of a line between any two points? You just subtract the x and y coordinates from each point and you get a new point (x2 minus x1,y2 minus y1). And then you can figure out the Angle using the line between this new point and the origin coordinates and you can figure out the Angle between these two points using a transformation that follows

Math.atan2(y2-y1,x2-x1)*180/Math.PI
Copy the code

For chestnut

Calculate the Angle between points (5,5) and (3,3)

Calculate the Angle of the line from (4,2,5) to (2,4)

** Why is the Angle between the new point and the origin calculated by ATan2 the same as the inclination of the line between the two points? **

Because the two lines are parallel, the Angle between them is the same.

Application scenarios

  • The picture rotates with the two fingers, and the rotation Angle difference is calculated according to ATan2 ()

  • Determine the sliding screen direction according to the Angle

  • The foundation of special effects, many special effects need to use this API to do some Angle conversion, especially in canvas