Home > Web Front-end > JS Tutorial > Use js to quickly find the area of ​​a triangle

Use js to quickly find the area of ​​a triangle

藏色散人
Release: 2021-08-06 18:04:17
Original
5465 people have browsed it

Everyone should know the formula for the area of ​​a triangle, that is, the area is equal to one-half times the base times the height. Haha, it doesn’t matter if you forget. This article will introduce to you how to calculate the area of ​​a triangle using js.

First of all, let me give you a brief introduction to the triangle area formula:

The triangle area formula refers to using a formula to calculate the area of ​​a triangle, three line segments in the same plane and not on the same straight line The closed figure formed by connecting end to end is called a triangle, and its symbol is △.

As shown below:

Use js to quickly find the area of ​​a triangle

So after briefly understanding the triangle area formula, let me ask you a question: "Please write a JavaScript function to calculate The area of ​​a triangle whose three sides have lengths 4, 5, and 6”.

I don’t know if you have any calculation ideas~

The following is the method I gave:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
    var side1 = 4;
    var side2 = 5;
    var side3 = 6;
    var s = (side1 + side2 + side3)/2;
    var area =  Math.sqrt(s*((s-side1)*(s-side2)*(s-side3)));
    console.log(area);
</script>
</body>
</html>
Copy after login

The calculation result is:

9.921567416492215
Copy after login

In fact, this is a Math problem!

However, one knowledge point that everyone needs to master is Heron's formula. Heron's formula is also translated as Heron's formula, Hailong formula, Hero's formula, and Heron-Qin Jiushao formula; it is calculated directly by using the side lengths of the three sides of a triangle. The formula for the area of ​​a triangle; the expression is: S=√p(p-a)(p-b)(p-c).

The popular explanation is:

It is known that the three sides are a, b, and c. The side lengths of our example here are 4, 5, and 6 respectively;

Let p= (a b c)/2, that is, the "(side1 side2 side3)/2"

area in the code is S=√[p(p-a)(p-b)(p-c)], It is "Math.sqrt(s*((s-side1)*(s-side2)*(s-side3)));".

You need to know a function here, which is the Math.sqrt() function, which is used to return the square root of a number;

→Note: Since sqrt is a static method of Math , so it should be used like this: Math.sqrt(), not as a method of the Math instance you created.

Finally, I would like to recommend "JavaScript Basics Tutorial" ~ Welcome everyone to learn ~

The above is the detailed content of Use js to quickly find the area of ​​a triangle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
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