Home Web Front-end JS Tutorial How to find angle in javascript

How to find angle in javascript

Nov 01, 2021 pm 03:20 PM
javascript

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

How to use insertBefore in javascript

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

See all articles