Home > database >  How to calculate sen on javascript using math tag?
How to calculate sen on javascript using math tag?

Time:11-17

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?

CodePudding user response:

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).

  • Related