Re: mathematical area of closed vector path
Re: mathematical area of closed vector path
- Subject: Re: mathematical area of closed vector path
- From: mark <email@hidden>
- Date: Mon, 24 Jan 2005 19:06:19 -0800
I believe that you want to model your figures a a closed polygon (arcs being
just a set of vectors). From what I know, a concave or convex polygon's area
is: 1/2 * SUM from i = 0 TO N-1 of (x[i]*y[i+1]-x[i+1]*y[i])
Where N = # of verticies, and x[i] and y[i] are the x,y verticies of side i.
I don't think this formula covers the case when the polygon intersects
itself or has holes in it. You'll also need to check for negative area,
which can happen depending on rotation.
Here's the (C++) code that I use (where outVertices is a <vector> of x,y
verticies). I do a special case for triangles and quadrilaterals, mainly
for efficiency purposes.
Hope this helps you get startedĀ
long numOutVertices = outVertices.size();
if(numOutVertices > 2)
{
double x0 = outVertices[0].GetX();
double y0 = outVertices[0].GetY();
if (numOutVertices == 3) // use different area formula for
triangle
{ // (V1-V0)x(V2-V0) -->
(x[1]-x[0])*(y[2]-y[0])-(x[2]-x[0])*(y[1]-y[0])
// however the rotation is V0, V2, V1 (going clockwise)
theArea = (outVertices[2].GetX() -x0)*(outVertices[2].GetY()-y0) -
(outVertices[1].GetX() -x0)*(outVertices[1].GetY()-y0);
}
else if (numOutVertices == 4) // use different area formula for
quadrilateral
{ // (x1-x0)*(y3-y0)-(x3-x0)*(y1-y0), with order being v0, v3, v2, v1
double x1 = outVertices[3].GetX();
double y1 = outVertices[3].GetY();
double x3 = outVertices[1].GetX();
double y3 = outVertices[1].GetY();
theArea = (x1-x0)*(y3-y0)-(x3-x0)*(y1-y0);
}
else
{
//Check for invalid points
for(int i = 0; i < numOutVertices; i++)
{
int xInd = (i + 1) % numOutVertices;
int yInd = (i + 2) % numOutVertices;
theArea += outVertices[xInd].GetX()
* ( outVertices[yInd].GetY() - outVertices[i].GetY());
}
theArea *= 0.5;
}
if (theArea < 0) // if polygon goes clockwise, the area will be
negative
{
theArea = -theArea;
}
}
> From: "P. George" <email@hidden>
> Date: Mon, 24 Jan 2005 20:14:28 -0600
> To: email@hidden
> Subject: mathematical area of closed vector path
>
> How would one go about figuring out the mathematical area (xxx units
> squared) of a closed vector path?
>
> Note: These paths will not be simple little boxes or circles. Think
> complex paths--composed of mulitple line segments and arc segments
> (both convex and concave).
>
> Thanks.
>
> - Philip
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden