英文:
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时,它返回 -219.816218。
然而,这个在线计算器:https://www.omnicalculator.com/math/regular-polygon-area
返回的是769.4。我的方法有什么问题吗?我使用的公式在这里有说明。
英文:
I have this method for calculating regular polygon area:
    public double getArea()  {
        return (sideLength *
            sideLength * sides) /
            (4 * Math.tan(180 / (double) sides));
    }
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
使用以下返回语句
return (sideLength * sideLength * sides) / (4 * Math.tan((180 / sides) * 3.14159 / 180));
在这里,*(3.14159 / 180) 被添加用于将面积从度转换为弧度。
英文:
Use the following return statement
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将角度转换为弧度 - 如下所示:
Math.tan(Math.toRadians(180 / (double) sides))
或者一开始就在弧度中进行这个计算。
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:
Math.tan(Math.toRadians(180 / (double) sides))
Or do this computation in radians to start with.
Math.tan(Math.PI / sides)
答案3
得分: 1
问题在于 Math.tan 函数默认使用弧度作为单位。改用以下方式:
(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:
(4*Math.tan(Math.PI/180 * 180/(double) sides))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论