Area of irregular shape
I'm writing a space game with customisable spaceship shapes, but I'm a bit overwhelmed by a problem, I want to figure out the mass of an irregular 2d shape (the spaceship) based on its area.
The shape itself is stored as a sort of linked list
Point x1, y1 -----> Point x2, y2 ----> Point x3, y3 ----> point x4, y4
and the last point joins back onto the first point. Every 1 unit (unit being 1 integer distance between points) cubed has a mass of 1.
so a triangle of, say:
would have a mass of 17/2 * 12 = 102.
I know I will have to split the main polygon down into triangles, but using every 3 points will not work because sometimes a line back between points could overlap.
I hope I make sense, could I please have any advice on how to go about splitting my shape into triangles.
Is your polygon limited to the convex case? If so...
Do you have them in either counter-clockwise or clockwise order? If so just pick the first point as your starting point. So you have 6 points represented like so:
Let point 1 be the bottom and start moving in a clockwise motion.
It will be a bit more work for concave polygons. Let me know if that's your case.
Yeah...or Salem's way for the general case.
also keep in mind that you don't have to have exact area. You can approximate the shape by enclosing it with a box, or a circle, or some other basic geometry shape for which you know how to calculate the area.
cool, I have it now, thanks for the help :)
btw this site gives polygon area source code for any of you searchers: