Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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.

share|improve this question
1  
Part of your problem appears to be degrees vs. radians. Specifically, $4 \cdot \tan(45)$ is 6.47. –  Harry Stern Jul 2 '11 at 8:00
    
You are completely right! Thank you! –  Chris Corbyn Jul 2 '11 at 8:11
add comment

3 Answers

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.

share|improve this answer
add comment

The classic trigonometric formula for the area of the general triangle ABC, with $\overline{AB} = a$, $\overline{BC} = b$ and $\widehat{ABC} = \gamma$ is $$A=\frac{1}{2}ab\sin \gamma$$

Just let $AB$ be the next side of your convex polygon and let $C$ be any point inside it. Iterate over the sides and you should get what you want.

If your polygon is convex you may need to make sure that you detect overlaps between your triangles so that you can discount them appropriately. Even then you may end up overestimating the area of the shape (pardon the horrible image):

enter image description here

Even if you detect the overlap between the red and yellow and orange triangles and remove the overlapping areas from the sum the correct number of times, you're still counting once the white area outside the L shape but inside the red triangle.

What you want to try with convex polygons instead is partitioning the shape in triangles, then summing up those areas. (Personally I haven't studied any such algorithm.)

share|improve this answer
    
Whoaa... thanks so much for taking the time to go into so much detail. This will take a while for me to digest :P I'm only working with regular polygons... nothing complex like this :) –  Chris Corbyn Jul 2 '11 at 8:12
add comment

For regular polygons, the formula is well known. Try the following link: http://www.mathopenref.com/polygonregulararea.html

Or do it yourself. Consider all the triangles determined by the center and two neighbor vertices. Then (if the polygon has $n$ sides) you have $n$ congruent isosceles triangles. Find the area of one of these triangles and then multiply it by $n$.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.