No matter you want to draw by yourself with canvas, or just because you can’t understand the operation of others’ canvas, you have come to the right place today. There are basic teaching here, which will bring you back to the horrible life in junior high school. In addition, I will summarize some practical application examples of trigonometric functions in canvas later

(~ o ~ 3 ~) ~

Learning materials

  • Zero basic high school mathematics trigonometric functions
  • The length of three sides of a triangle is known

Positive and negative angles

  • Positive Angle— Counterclockwise rotation
  • Negative Angle— Clockwise rotation

Canvas Angle +- I remember a long time. (*. > Д <) o ゜

Angle, radian

Angles, radians all describe circles

  • The Angle –>
    • Words:degree
    • Symbol: °
    • You know, if you divide a circle into 360 parts, each of those parts has a central Angle of 1 degree
  • radian –>
    • Words:radian
    • Symbol: rad
    • When arc length is equal to radius, the corresponding central Angle is called 1rad

So you see, radians are still angles

  • There’s a formula for angles and radians:
1rad PI is 180 degreesCopy the code
  • The angles and radians convert to each other
1 = PI / 180 1 = PI / 180 1 = PI / 180 1 = PI / 180Copy the code

Why are we talking about radians and angles? There’s a very real need, because in Math, the sine, the cosine, and the tan are all in radians, so if you’re going to convert radians, if you’re going to convert degrees, if you’re going to convert degrees, if you’re going to convert degrees, you’re going to have to figure out what’s going on here, right

Trigonometric functions

Right triangles

The calculation of trigonometric functions is extended to: the calculation of right triangles, there are several theorems in right triangles, also known as the Pythagorean theorem

  • The sum of squares of 2 sides is equal to the sum of squares of hypotenuse
  • sin— Plain, diagonal/hypotenuse
  • cos— Yu Xuan, adjacent/hypotenuse
  • tan— Tangent, right side/adjacent side

2. Regular triangles

See: junior high school mathematics often test type, known triangle three sides long, Angle

The Pythagorean theorem only applies to right triangles, so how do you calculate a normal triangle, the idea is to extend the hypotenuse of the triangle, to form a new right triangle

  • Target triangle

  • Extend hypotenuse new triangle

  • So finally:

This is a train of thought, don’t come across everyone will not calculate

Math

Math is also a new class to me, and I have never used it before except for random number. However, when it comes to canvas custom drawing, the main battlefield of Math will come. A long time ago I saw danniu custom view in various operations is not understand, not only Math do not understand, is baidu, know what this method after I also do not understand. The root of the problem is that we forget all trigonometry, learn another trigonometry, do Math, and we won’t be embarrassed

1. E, PI

Math class has 2 constants: E, PI, PI=3.1415926

2. Maximum, minimum, absolute value

System.out.println("The greater value of 10 and 20:" + Math.max(10.20));
System.out.println("Smaller values of 15.6 and 15:" + Math.min(15.6.15));
System.out.println("Absolute value of -12:" + Math.abs(-12));
Copy the code
Larger values of 10 and 20:20 Smaller values of 15.6 and 15:15.0-12 Absolute values of 12Copy the code

3. An integer

static double ceil(double a) Returns the smallest integer greater than or equal to a, understood as rounded up
static double floor(double a) Returns the largest integer less than or equal to a, understood as a downward round
static double rint(double a) Returns the integer value closest to a, or even if there are two equally close integers
static int round(float a) rounded
static long round(double a) rounded
  • rintRounded, but at 0.5, take even numbers, such as rint(24.5)=24
  • round — 真*四舍五入,round(24.5)=25
Scanner input = new Scanner(System.in);
System.outprintln(Please enter a number:);
double num = input.nextDouble();

System.out.println("Greater than or equal to"+ num +The smallest integer of ":" + Math.ceil(num));
System.out.println("Less than or equal to"+ num +The largest integer of ":" + Math.floor(num));
Copy the code
Please enter a number: 99.01 Minimum integer greater than or equal to 99.01:100.0 Maximum integer less than or equal to 99.01:99.0Copy the code

4. Trigonometric functions

Sine, cos, tan we should be familiar with, +a is the inverse value, toDegree() radian turn Angle, toRndian() Angle turn radian

System.out.println{"Sine of 90 degrees:" + Math.sin(Math.PI/2));
System.out.println("Cosine of 0 degrees:" + Math.cos(0));
System.out.println("The arctangent of 1:" + Math.atan(l));
System.out.println("120 degrees radian value:" + Math.toRadians(120.0));
Copy the code
Sine of 90 degrees: 1.0 Cosine of 0:1.0 Arctangent of 1:0.7853981633974483 Radian of 120 degrees: 2.0943951023931953Copy the code

5. Exponential operation

System.out.println("The cubic value of 4:" + Math.pow(4.3));
System.out.println("Square root of 16:" + Math.sqrt(16));
System.out.println("Log base 10 of 2:" + Math.log1O(2));
Copy the code
The cubic value of 4:64.0 Square root of 16:4.0 logarithm of base 2:0.3010299956639812Copy the code