Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Neil Smith · Apr 11, 2014 at 02:11 PM · surfaceareapolygons

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Randomman159 · Apr 11, 2014 at 02:24 PM 0
Share

Is this polygon on a flat plane? (The plane may be rotated)

avatar image Neil Smith · Apr 11, 2014 at 05:49 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
8

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Neil Smith · Apr 11, 2014 at 05:47 PM 0
Share

$$anonymous$$any thanks for your answer. It doesn't fully work for myself, but it's definitely pointed me in the right direction.

avatar image fafase · Apr 11, 2014 at 06:13 PM 0
Share

You should tell what is not fully working for you, that may help others in the same situation if we all find a solution.

avatar image kzaarka · Jul 27, 2017 at 02:52 PM 0
Share

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?

avatar image fafase kzaarka · Aug 13, 2017 at 08:27 PM 0
Share

Has to be from elsewhere. $$anonymous$$ethod returns absolut value so cannot be negative.

avatar image AdamBebko · Nov 06, 2017 at 06:19 PM 0
Share

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.

avatar image Bunny83 AdamBebko · Nov 06, 2017 at 07:16 PM 1
Share

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.

avatar image Bunny83 AdamBebko · Nov 06, 2017 at 09:10 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges