- Home /
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?
Is this polygon on a flat plane? (The plane may be rotated)
The polygon could be, but I would have times when it's at an angle. I think I might have solved it, so I'll post my solution here soon. Any advice would be gratefully received.
Answer by fafase · Apr 11, 2014 at 02:26 PM
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[i].transform.position.x * list[i+1].transform.position.z;
float mulB = list[i+1].transform.position.x * list[i].transform.position.z;
temp = temp + ( mulA - mulB );
}else{
float mulA = list[i].transform.position.x * list[0].transform.position.z;
float mulB = list[0].transform.position.x * list[i].transform.position.z;
temp = temp + ( mulA - mulB );
}
}
temp *= 0.5f;
return Mathf.Abs(temp);
}
I did not invent that equation it comes from Wikipedia http://en.wikipedia.org/wiki/Polygon 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.
$$anonymous$$any thanks for your answer. It doesn't fully work for myself, but it's definitely pointed me in the right direction.
You should tell what is not fully working for you, that may help others in the same situation if we all find a solution.
Awesome! It works! But it returns a negative number, for example, a square that perimeter are 40 (10,10,10,10), the area are 100. This code return me -100. Do you know the solution for this problem?
Has to be from elsewhere. $$anonymous$$ethod returns absolut value so cannot be negative.
Does the list of vertices need to be in any particular order? The wiki page says something about ordering them clockwise or counter-clockwise, but it's unclear whether that's for this formula or the centroid formula.
The order doesn't matter due to the:
return $$anonymous$$athf.Abs(temp);
Which will always return a positive area. This simply calculates the 2d cross product between two position vectors which gives you the signed area of the defined by the two vectors. Half of that area is the area of one triangle. Negative and positive areas outside the polygon will cancel each other so only the area inside the polygon will remain. This may be positive or negative but due to the Abs it's always positive.
edit
$$anonymous$$aybe i should be more cliear. Of course the vertices need to be "in order" in the sense that they form a polygon with non overlapping area. Though the winding order (clockwise / counter clockwise) doesn't matter.
second edit
I quickly made an animated gif which shows what happens:
Note that "green" area is ment "positive area" and "red" area is "negative" area. Light blue is "0" area where red and green cancel each other. The "brigther green" is basically "double" area, so the marked area has been counted twice.
ps: This happens when you have an overlapping polygon. As you can see in the result we have positive as well as negative area. Those cancel each other so the resulting area is smaller than the green area. So in this case it's not possible to get the actual area. It behaves very similar to integrating the area under a curve where you can also have negative area (below the x-axis)
Your answer
Follow this Question
Related Questions
Instantiation into particular area 0 Answers
Is it possible to assing a colour to the surface delimitated by the line renderer loop? 0 Answers
Calculating surface area of a plane covered by cameras' frustums 1 Answer
How to calculate surface area of a Mesh for Lightmapping 1 Answer
How do I lock the mouse in an area? 1 Answer