简体   繁体   中英

How to calculate sen on javascript using math tag?

I'm coding a site that calculates the area of regular polygons, and I'm having problems building the formula.

Here is the formula:

((a**2)*b)/(4*tan(π/b)))

a = Size of the side
b = Number of the sides

For exemple:

a = 10

b = 5

Area = 172

i tryed these syntaxs, but the result is not 172:

((10**2) * 5) / (4 * Math.tan(math.PI / 5))
((10**2) * 5) / (4 * Math.tan(3.1415 / 5))

I now that the problem is here: "(4 * Math.tan(3.1415 / 5))" , because if i put the directly the value of the "tan(π/5)", which is "0.726542528", the formula works...

((10**2) * 5) / (4 * 0.726542528) = 172

Which is the correct syntax?

If you need ((a**2)b)/(4tan(π/b))) implemented in JS, then the first step is to actually write that out fully instead of omitting implied operations:

((a**2) * b) / (4 * tan(π/b))

And of course, a bunch of parentheses are irrelevant here:

a**2 * b / (4 * tan(π/b))

Then you map that to JS, which it pretty much already is except for that tan and π , both of which we can get from the Math object:

const { PI:π, tan } = Math;

function getPolygonArea(a,b) {
  return a**2 * b / (4 * tan(π/b));
}

Done. That formula works now and yields 172.04774005889672 for getPolygonArea(10,5) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM