Laya rocker core algorithm

Press release event:

Press to move the core code:

 

 Slope to radians to angle (or sine and cosine value)

official 

1 The slope is the radians from a point to radians a and b Math.atan2( by -ay, bx -ax)

2 radians turning angle radians*180/Π

3 Find the value of sine and cosine in radians Math.Sin Cos (radians)

Knowledge point supplement: 

public static double Cos (double d);

Returns the cosine value of the specified angle.

d  Double

The angle measured in radians.

The angle (  d ) must be in radians. Multiply by  Math.PI  /180 to convert degrees to radians.

         That is to say: To find the cosine value, you must first turn from angle to radians (conversion formula: angle * Π /180)

Then Cos (radians) get the cosine value 

Slope:

1. Math.atan() 
Math.atan() accepts a parameter: the usage is as follows: 
angel=Math.atan(slope) 
angel is the radian value of an angle, slope is the slope of the straight line, which is a number, this number can be negative Any value between infinity and positive infinity. 
However, it is more complicated to use it to calculate. Because of its periodicity, a number has more than one arctangent value. For example, the value of atan(-1) may be 45 degrees, or It may be 225 degrees. This is his periodicity. For the tangent function, his period is 180 degrees, so two angles that differ by 180 degrees have the same tangent and slope: 
tanθ=tan(θ+180) 
However, Math.atan() can only return an angle value, so it is very complicated to determine his angle, and the tangent of 90 degrees and 270 degrees is infinite, because the divisor is zero, we are also more difficult to deal with~! So we are more The second function will be used. 

2. Math.atan2() 
Math.atan2() accepts two parameters x and y, the method is as follows: 
angel=Math.atan2(y,x) 
x specifies the number of the x coordinate of the point. 
y specifies the number of the y coordinate of the point. 
The calculated result angel is a radian value, and can also represent the angle of the opposite corner of a right-angled triangle, where x is the length of the adjacent side and y is the length of the opposite side. 
Let's test these two functions below: 
x=Math.atan(1)//Calculate the radian value corresponding to the number whose tangent value is 1 
trace(x) //output a radian value 0.785398163397448 
x=180*x/Math. PI//Convert to angle value 
trace(x) //output 45 
x=Math.atan2(7,7) 
trace(x)//output 0.785398163397448 
x=180*x/Math.PI//convert to angle value 
trace(x)//output 45 
x =Math.atan2(7,-7) 
trace(x)2.35619449019234 
x=180*x/Math.PI//Convert to angle value 
trace(x)135 
x=Math.atan2(-7,7) 
trace(x) //Output -0.785398163397448 
x=180*x/Math.PI//convert to angle value 
trace(x)//output -45 
x=Math.atan2(-7,-7) 
trace(x)//output-2.35619449019234 
x=180*x/Math.PI//converted to angle value 
trace(x)//output -135 
//From these tests, we can see that through the automatic adjustment of the coordinate system, we can freely calculate that we are in different quadrants The angle corresponding to the position. 

3. Calculate the inclination angle of the line between two points. 
This method is very useful. 
Math.atan2() function returns the inclination angle of the straight line between the point (x, y) and the origin (0, 0). So how to calculate the inclination angle of the straight line between any two points? You only need to separate the x and y coordinates of the two points Subtract to get a new point (x2-x1, y2-y1). Then use it to find the angle. Use the following conversion to calculate the angle between the two points. 
Math.atan2(y2 -y1,x2-x1) 
But what we get is a radian value, under normal circumstances we need to convert it to an angle. 
Let's use a piece of code to test this conversion. 
//Test, calculate the point (3 ,3) and (5,5) constitute the angle of the line 
x=Math.atan2(5-3,5-3) 
trace(x)//output 0.785398163397448 
x=x*180/Math.PI//conversion Is the angle 
trace(x)//output 45.

 

Guess you like

Origin blog.csdn.net/LM514104/article/details/109101223