Comments and answers for "How to calculate the surface area of a irregular polygon"
http://answers.unity.com/questions/684909/how-to-calculate-the-surface-area-of-a-irregular-p.html
The latest comments and answers for the question "How to calculate the surface area of a irregular polygon"Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1432520/view.html
Yes, it's true that his particular implementation only works for polygons that are defined in in the world space x-z plane. However as i said in my comment it's actually just the cross product between the vectors. It does work for an arbitary **polygon** no matter how it's rotated. Though for a "polygon plane" that's not going through the origin you have to use local coordinates. So you can simply subtract one of the coordinates from all others.
<br>
Keep in mind that [a polygon is a 2 dimensional shape][1]. That means all points have to be located on the same plane. Don't confuse a polygon with a triangle mesh. If the points do not lie on the same plane it's not a polygon. Also such a "polygon" can't be triangulated arbitarily since the result would look very different. Imagine a simple "quad" where two diagonal points are moved backwards. If you triangulate this quad you either get a sharp edge on the front diagonal or the two triangles point with their single corners forward. Again, this is not a polygon.
<br>.
[Back in the old days][2] of immediate mode OpenGL we were able to give OpenGL polygon faces directly. OpenGL / the hardware triangulated them automatically. However those polygons must be convex and a polygon has to lie in one plane (not one of the 3 worldspace planes but an arbitrary plane).
<br>
If you're lookng for an implementation that works for arbitary (non overlapping, see above) polygons which can be located in any arbitrary 3d plane you may want to have a look at my [MathHelper][3]([direct download][4]) which i just created. I made two versions, one that takes an `IEnumerable<Vector3>` and another one that takes a `List<Vector3>`. The IEnumerable version is more flexible but will allocate some garbage due to the IEnumerable / IEnumerator. However it allows easy Linq integration like this:
public Transform[] points;
float area = MathHelper.PolygonArea(points.Select(t=>t.position))
Though if you have to calculate it on a per-frame basis you might want to use the List version.
[1]: https://en.wikipedia.org/wiki/Polygon
[2]: https://stackoverflow.com/a/26948167/1607924
[3]: https://www.dropbox.com/s/5oewyy5458gkt1t/MathHelper.cs?dl=0
[4]: https://www.dropbox.com/s/5oewyy5458gkt1t/MathHelper.cs?dl=1Wed, 15 Nov 2017 13:45:31 GMTBunny83Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1429991/view.html
ps: [This happens][1] 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)
[1]: https://www.dropbox.com/s/30qbl6w370sikkk/PolygonAreaOverlap.gif?raw=1Mon, 06 Nov 2017 21:10:34 GMTBunny83Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1429965/view.html
The order doesn't matter due to the:
return Mathf.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.
<br>
***edit***
Maybe 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:
<a href="https://www.dropbox.com/s/sjsug90jumgwjij/PolygonArea.gif?dl=0"><img src="https://www.dropbox.com/s/sjsug90jumgwjij/PolygonArea.gif?raw=1"></a>
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.Mon, 06 Nov 2017 19:16:16 GMTBunny83Comment by AdamBebko on AdamBebko's answer
http://answers.unity.com/comments/1429946/view.html
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.Mon, 06 Nov 2017 18:19:23 GMTAdamBebkoComment by fafase on fafase's comment
http://answers.unity.com/comments/1393292/view.html
Has to be from elsewhere. Method returns absolut value so cannot be negative.Sun, 13 Aug 2017 20:27:50 GMTfafaseComment by kzaarka on kzaarka's answer
http://answers.unity.com/comments/1385363/view.html
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?Thu, 27 Jul 2017 14:52:21 GMTkzaarkaComment by fafase on fafase's answer
http://answers.unity.com/comments/685048/view.html
You should tell what is not fully working for you, that may help others in the same situation if we all find a solution.Fri, 11 Apr 2014 18:13:29 GMTfafaseComment by Neil Smith
http://answers.unity.com/comments/685035/view.html
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.Fri, 11 Apr 2014 17:49:51 GMTNeil SmithComment by Neil Smith on Neil Smith's answer
http://answers.unity.com/comments/685034/view.html
Many thanks for your answer. It doesn't fully work for myself, but it's definitely pointed me in the right direction.Fri, 11 Apr 2014 17:47:34 GMTNeil SmithAnswer by fafase
http://answers.unity.com/answers/684919/view.html
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.Fri, 11 Apr 2014 14:26:32 GMTfafaseComment by Randomman159
http://answers.unity.com/comments/684918/view.html
Is this polygon on a flat plane? (The plane may be rotated)Fri, 11 Apr 2014 14:24:30 GMTRandomman159