Results 1 to 3 of 3

Thread: Is it possible to use the loop to calculation of geometric area?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    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 .....

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  3. #3
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551

    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
  •  



Click Here to Expand Forum to Full Width