无法计算正多边形的面积 - 使用正切公式的结果错误
Can't calculate the area of regular polygon - wrong result using the tangent formula
我有这个计算正多边形面积的方法:
public double getArea() {
return (sideLength *
sideLength * sides) /
(4 * Math.tan(180 / (double) sides));
}
因为 sideLength
和 sides
都等于 10 它 returns -219.816218
。
“
但是这个在线计算器:https://www.omnicalculator.com/math/regular-polygon-area
returns769.4。我的方法有什么问题?我使用的公式指定 here
.
使用下面的return语句
return (sideLength * sideLength * sides) / (4 * Math.tan((180 / sides) * 3.14159 / 180));
这里加入*(3.14159 / 180)
,将面积从degree
转换为radians
三角函数的参数是按弧度而不是度数定义的。使用 Math.toRadians
将以度为单位的角度转换为弧度 - 如下所示:
Math.tan(Math.toRadians(180 / (double) sides))
或者以弧度开始计算。
Math.tan(Math.PI / sides)
问题在于 Math.tan
函数使用弧度作为其默认度量单位。改用这个:
(4*Math.tan(Math.PI/180 * 180/(double) sides))
我有这个计算正多边形面积的方法:
public double getArea() {
return (sideLength *
sideLength * sides) /
(4 * Math.tan(180 / (double) sides));
}
因为 sideLength
和 sides
都等于 10 它 returns -219.816218
。
“
但是这个在线计算器:https://www.omnicalculator.com/math/regular-polygon-area
returns769.4。我的方法有什么问题?我使用的公式指定 here
.
使用下面的return语句
return (sideLength * sideLength * sides) / (4 * Math.tan((180 / sides) * 3.14159 / 180));
这里加入*(3.14159 / 180)
,将面积从degree
转换为radians
三角函数的参数是按弧度而不是度数定义的。使用 Math.toRadians
将以度为单位的角度转换为弧度 - 如下所示:
Math.tan(Math.toRadians(180 / (double) sides))
或者以弧度开始计算。
Math.tan(Math.PI / sides)
问题在于 Math.tan
函数使用弧度作为其默认度量单位。改用这个:
(4*Math.tan(Math.PI/180 * 180/(double) sides))