I am trying to write a program in Python to calculate the area of a polygon. The formula is :

area = n * s * a / 2
a = s / 2 * tan(pi/n)

My code is:

import math

def area(s,n):
    a = s/(2* math.tan((math.pi/n)))
    b = (n*s*a)/2
    return b

The problem is that it calculates a total different thing. For example: area (2,5) = 3.63, when it is really 6.887.

Does anyone see a mistake?

有帮助吗?

解决方案

import math

def area(s,n):
    a = s/(2* math.tan((math.pi/n)))
    b = (n*s*a)/2
    return b

area(2, 5)
#>>> 6.881909602355868

def area(s,n):
    a = s/2* math.tan((math.pi/n))
    b = (n*s*a)/2
    return b

area(2, 5)
#>>> 3.6327126400268046

Really though, don't use single letter names. Making things unreadable does nobody any good.

If your mathematics doesn't make semantic sense, rewrite it so that it does. Your confusion is a testament to this.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top