Home > Web Front-end > JS Tutorial > body text

How to find angle in javascript

藏色散人
Release: 2021-11-01 15:43:00
Original
5715 people have browsed it

Javascript method to find the angle: 1. Create the "function angle(start,end){...}" method; 2. Execute "angle({x:0,y:0},{x: 1,y:1})" statement is enough.

How to find angle in javascript

The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

How to find the angle in javascript?

Angle calculation in js:

—, js About trigonometric function algorithm, understanding of radians and angles, and mutual conversion of angles

Two units of degree and arc

1. The definition of radian

We know that the definition of "degree" is, "Two rays shoot from the center to the circumference of the circle, forming a The angle and the arc directly opposite the angle. When the length of this arc is exactly equal to one-360th of the circumference of the circle, the angle between the two rays is 1 degree. (Figure 1)

2. How is radian defined? The definition of radian is: two rays are emitted from the center of the circle to the circumference, forming an included angle and an arc directly opposite the included angle. When the length of this arc is exactly equal to the circle When the radius of The length of the arc is different. The degree is equal to 1/360 of the circumference of the circle, while the radian is equal to the radius.

Simply put, the definition of radian is that when the length of the arc subtended by the angle is equal to the radius, The size of the angle is 1 radian.

Related pictures on this topic are as follows:

How to find angle in javascript If the length of the arc subtended by the angle is several times the radius, then the size of the angle Just a few radians.

Their relationship can be expressed and calculated by the following formula:

角(弧度)=弧长/半径
Copy after login

The circumference of a circle is 2π times the radius, so a circumferential angle (360 degrees) is 2π radians.

The length of a semicircle is π times the radius, so a straight angle (180 degrees) is π radians.

3. Conversion between degrees and radians

A circle: π :3.14......

360° =2π;

π =180; In this way, we know that 1 degree is equal to π/180;

Write "π" in the js code as "PI". And because "π" and "sin" are both "mathematical functions", according to regulations, "Math." must be added in front (Math is the abbreviation of "Mathematics" in English), and then written as "Math." .PI", "Math.sin", that is to say, π should be written as Math.PI in js, and sin() should be written as Math.sin();

as follows:

sin30° It has to be written as Math.sin (30*Math.PI/180). The part in parentheses is to convert 30° into radians, that is, 30×π/180.

"I'm quite happy to spend a day automating a task through programming, unless the task only takes 10 seconds to complete manually"

It's actually a math problem, right?

function angle(start,end){
    var diff_x = end.x - start.x,
        diff_y = end.y - start.y;
    //返回角度,不是弧度
    return 360*Math.atan(diff_y/diff_x)/(2*Math.PI);
}
那么执行
angle({x:0,y:0},{x:1,y:1})就会返回45(度)
Copy after login

Recommended study: "

javascript basic tutorial"

The above is the detailed content of How to find angle in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template