We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Calculate Polygon Area
Page Index Toggle Pages: 1
Calculate Polygon Area? (Read 525 times)
Calculate Polygon Area?
Nov 27th, 2006, 3:37am
 
If I create a polygon using beginShape(POLYGON); and add some vertices, is there any way to have processing tell me the area of the polygon. Processing must know since it renders the polygon. I just don't want to re-calculate it if I don't have to.

By the way, I am looking into rendering and calculating serveral polygons, each with a couple hundred vertices.

Thanks
Re: Calculate Polygon Area?
Reply #1 - Nov 27th, 2006, 9:01pm
 
I think this'll do it:


Pick a point on the polygon and begin moving in a counter-clockwise fashion around the shape. (If you accidentally move clockwise, you’ll get the same number but as a negative value.) Assuming the first point you choose is (x1, y1) and the last point, before you close the shape, is (xn, yn), you multiply the first x value with the second y value and subtract the second x value times the first y value. You continue this process all the way around the shape. Obviously, the last point will be the first point (x1, y1), since you’re dealing with a closed shape. Finally, take half of the entire value.
½[(x1 y2 - x2 y1)  + (x2 y3 - x3 y2) + . . . (xn-1 yn - xn yn-1)]
Page Index Toggle Pages: 1