无法计算正多边形的面积 – 使用正切公式得到错误结果

huangapple go评论110阅读模式
英文:

Can't calculate the area of regular polygon - wrong result using the tangent formula

问题

我有一个用于计算正多边形面积的方法:

  1. public double getArea() {
  2. return (sideLength *
  3. sideLength * sides) /
  4. (4 * Math.tan(180 / (double) sides));
  5. }

sideLengthsides 都等于10时,它返回 -219.816218

然而,这个在线计算器:https://www.omnicalculator.com/math/regular-polygon-area
返回的是769.4。我的方法有什么问题吗?我使用的公式在这里有说明。

英文:

I have this method for calculating regular polygon area:

  1. public double getArea() {
  2. return (sideLength *
  3. sideLength * sides) /
  4. (4 * Math.tan(180 / (double) sides));
  5. }

for sideLength and sides both being equal to 10 it returns -219.816218.
"
However this online calculator: https://www.omnicalculator.com/math/regular-polygon-area
returns 769.4. What is wrong with my method? The formula I use is specified here
.

答案1

得分: 1

使用以下返回语句

  1. return (sideLength * sideLength * sides) / (4 * Math.tan((180 / sides) * 3.14159 / 180));

在这里,*(3.14159 / 180) 被添加用于将面积从转换为弧度

英文:

Use the following return statement

  1. return (sideLength * sideLength * sides) / (4 * Math.tan((180 / sides) * 3.14159 / 180));

Here, *(3.14159 / 180) is added to convert the area from degree converted to radians

答案2

得分: 1

三角函数的参数是以弧度而不是角度定义的。使用Math.toRadians将角度转换为弧度 - 如下所示:

  1. Math.tan(Math.toRadians(180 / (double) sides))

或者一开始就在弧度中进行这个计算。

  1. Math.tan(Math.PI / sides)
英文:

The arguments to trigonometric functions are defined on radians, not degrees. Use Math.toRadians to convert the angle in degrees to radians - like this:

  1. Math.tan(Math.toRadians(180 / (double) sides))

Or do this computation in radians to start with.

  1. Math.tan(Math.PI / sides)

答案3

得分: 1

问题在于 Math.tan 函数默认使用弧度作为单位。改用以下方式:

  1. (4*Math.tan(Math.PI/180 * 180/(double) sides))
英文:

The problem is that the Math.tan function uses radians as it's default unit of measure. Use this instead:

  1. (4*Math.tan(Math.PI/180 * 180/(double) sides))

huangapple
  • 本文由 发表于 2020年4月10日 10:40:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61133228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定