[Math] How quickly we forget – basic trig. Calculate the area of a polygon

geometrytrigonometry

I think the easiest way to do this is with trigonometry, but I've forgotten most of the maths I learnt in school. I'm writing a program (for demonstrative purposes) that defines a Shape, and calculates the interior angles of the shape, based on the number of sides.

Now I'm trying to calculate the area based on the length of each of those sides.

So given a square, we know it has 4 sides. And given that the length of each of those sides is 8, we should be able to calculate the area (I know a square is easy… but the same formula needs to work for polygons with more/less sides).

What I've done, is this:

  1. Calculated the interior angles in the polygon as interior_angle = 180 - (360 / num_sides).
  2. Divided the polygon into an equal number of isosceles triangles and then divided each of those into 2 right-angled triangles, assuming this simplifies the logic. This is where I'm stuck.

Since the interior angle of the polygon is known, I've divided that by 2 in order to get the angle on one corner of these triangles, knowing that there's another angle of 90ยบ. So my logic tells me:

# Polgygon: sides = 4, length = 8
interior_angle = 180 - (360 / sides) = 90
a = interior_angle / 2 = 45
# Given that tan(a) = height / base
base = length / 2 = 4
tan(a) = height / base
# therefore
height = tan(a) * base = tan(45) * 4

This gives me 6.47 as the height (I think that's wrong… shouldn't it just be a round 3?).

Now to get the area of the entire polygon, I just have to calculate the area of each triangle and multiple that by the number of sides:

area = 0.5 * height * length * sides

For my 8 * 8 square, this gives me 51.83, so I've clearly got my logic wrong. If there's a simpler way to calculate the area of a uniform polygon, based on the number of sides and the length of each side, please educate me ๐Ÿ™‚ I just need to convert the maths into code for a computer program.

Best Answer

You will find simple formulae in http://en.wikipedia.org/wiki/Polygon. Look for the various formulae A = ... in the section Area and centroid. You will find the appropriate formula depending whether you know the cartesian coordinates of the polygon vertices or the lengths of the sides and the (exterior) angles at the vertices.