I want to calculate the area
inside an object which is any closed object like rectangle
,square
,polygon
etc.
I can not get exact solution to do this.Please help to find it.
Thanks.
I want to calculate the area
inside an object which is any closed object like rectangle
,square
,polygon
etc.
I can not get exact solution to do this.Please help to find it.
Thanks.
For arbitrary polygons with known vertice coordinates you can use Shoelace formula
A = 1/2 * Abs(Sum{i=0..n-1} (X[i] * Y[i+1] - X[i+1] * Y[i]) )
where for i=n-1
take i+1=0
I am using this method which will give me closed object's area.It may be helpful for you.
-(double)areaWithXpts :(NSArray *)xpts withYpts :(NSArray *)ypts totalPoints :(int)numPoints
{
double area=0;
int j=numPoints-1;
for(int i=0 ; i < numPoints;i++)
{
area=area+([[xpts objectAtIndex:j] floatValue]+[[xpts objectAtIndex:i] floatValue])*([[ypts objectAtIndex:j] floatValue]-[[ypts objectAtIndex:i] floatValue]);
j=i;
}
double areaVal=area/2;
NSLog(@"Area is :%f",areaVal);
return areaVal;
}
Usage :
Array of X points
NSArray *xArray =[NSArray arrayWithObjects:@"0", @"10", @"10", @"29",nil];
Array of Y points
NSArray *yArray =[NSArray arrayWithObjects:@"4", @"26", @"26", @"4",nil];
Total points = Array count.
Note: x array and y array both have same count.
double objectArea = [self areaWithXpts:xArray withYpts:yArray totalPoints:4];
Or you can check this answer also - Area calculation in objective c
And about area with curve shape object : you can find reference from this link - Curve shape object area reference