# How to calculate the surface area of a irregular polygon

I want to calculate the square area (surface area) of an irregular polygon created inside Unity which is within 3d space. The polygon will have various points around its border with x,y,z world coordinates.
Can this be done?

Lucky you I did that a while ago. This only works on polygon that will not have crossing edges. The list is just the list of point you use as vertices of the polygon, they should be GameObject but you can make them Transform, you would then have to remove the transform part of the code.

``````float SuperficieIrregularPolygon(){
float temp = 0;
int i = 0 ;
for(; i < list.Count ; i++){
if(i != list.Count - 1){
float mulA = list_.transform.position.x * list[i+1].transform.position.z;_
``````

float mulB = list[i+1].transform.position.x * list*.transform.position.z;*
temp = temp + ( mulA - mulB );
}else{
float mulA = list_.transform.position.x * list[0].transform.position.z;
float mulB = list[0].transform.position.x * list*.transform.position.z;*
temp = temp + ( mulA - mulB );
}
}
temp *= 0.5f;
return Mathf.Abs(temp);
}
I did not invent that equation it comes from Wikipedia Polygon - Wikipedia go down to Area and Centroid, first equation.
And that will return the area of the flat polygon but I guess that is what you are after since you mention a square. If you need the area of non-flat area then you would have to repeat the above process for all little areas and add them up._

Guys,

I got the same problem, but with a slight twist: I got n Vector3s which are in a plane. This can be any plane imaginable but they are in it. Now I want to calculate the surface area of this irregular polygon.

I have tried a lot of code unsuccessfully and this is the best (still not good) so far:

``````    Vector3 crossProductSum = Vector3.zero;
float areaSum = 0;

for (int i = 0; i < list.Count; i++)
{
if(i != list.Count - 1)
{
Vector3 vectorOne = list *- list[0];*
``````

Vector3 vectorTwo = list[i+1] - list[0];
Vector3 newCrossProduct = Vector3.Cross(vectorOne,vectorTwo);
crossProductSum = crossProductSum + newCrossProduct;
areaSum = areaSum + newCrossProduct.magnitude/2;
}
else
{
Vector3 vectorOne = list[1] - list[0];
Vector3 vectorTwo = list - list[0];
Vector3 newCrossProduct = Vector3.Cross(vectorOne,vectorTwo);
crossProductSum = crossProductSum + newCrossProduct;
areaSum = areaSum + newCrossProduct.magnitude/2;
}
}

areaSum = Mathf.Round(areaSum*100)/100;
float area = crossProductSum.magnitude/2;

return Mathf.Abs(areaSum);
As you can see I actually use two methods. areaSum, sums the magnitutde of all crossproducts, while area adds the cross products first and takes the magnitude last. Neither works.
If I do not do the else case I get partially correct results for about 50% of the cases.
Any thoughts on how to improve this code to make it work?
Thanks for the help.