|
-
Oct 17th, 2005, 03:19 PM
#1
Thread Starter
Hyperactive Member
Is it possible to use the loop to calculation of geometric area?
Last edited by Tamgovb; Oct 17th, 2005 at 03:22 PM.
I know, I know, my English is bad, sorry .....
-
Oct 18th, 2005, 04:17 AM
#2
Re: Is it possible to use the loop to calculation of geometric area?
Yes. What sort of data are you working on?
If you have an array of 2d points that define your shape then you can use this: (C# code by the way)
Code:
int GetPolyArea(Point[] pts)
{
//C#
uint i, j; //uints loop faster than ints
int area = 0;
int upper = pts.Length;
for (i = 0 ; i < upper ; i++)
{
j = (i + 1) % upper;
area += (pts[i].X * pts[j].Y) - (pts[i].Y * pts[j].X);
}
area >>= 1;
return(area < 0 ? -area : area);
}
The polygon must not be tangled up (ie, none of its lines cross eachother) like a figure 8 for instance.
That's about as fast as it gets.
Last edited by wossname; Oct 18th, 2005 at 04:26 AM.
I don't live here any more.
-
Oct 18th, 2005, 02:11 PM
#3
Fanatic Member
Re: Is it possible to use the loop to calculation of geometric area?
Nice formula, I've never seen it 
My way is much simpler to visualize. You can consider it as the integration between points i and j. The needed division by two (average Y value) is done later as well.
area += (pts[j].X - pts[i].X) * (pts[i].Y + pts[j].Y);
For the rest, it's the same as Wossname's.
This way should be a tad faster theoretically, because there's a multiplication less and a subtraction more.
If you don't make the area positive (see the return statement), then you can derive the direction of the points (vertexes) which enclose the area. If the area is negative, then the vertexes run counter clockwise, and if the area is positive, then the vertexes run clockwise. I'm not sure if this is also true for Wossname's way.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|