Search Unity

How to find the area of an irregular shape in 2D?

Discussion in '2D' started by Thrazamund, Nov 10, 2017.

  1. Thrazamund

    Thrazamund

    Joined:
    Apr 14, 2017
    Posts:
    36
    I am making a fruit ninja clone and want to add some extra functionality. I have an asset that will slice a 2d sprite object with a collider into child objects with mesh renderers and accurate polygon colliders in real time. I was wondering how could I calculate the area of these children and compare them when they are created? I want to be able to ask the player to cut an object into three pieces and then find out if those three pieces are relatively the same size and the if they are award the player more points. Any help for this or better ideas of how to achieve this same goal would be greatly appreciated. Thanks.
     
  2. Thrazamund

    Thrazamund

    Joined:
    Apr 14, 2017
    Posts:
    36
    Anyone have any ideas? Is this not possible?
     
  3. ELeigh

    ELeigh

    Joined:
    Dec 26, 2014
    Posts:
    2
  4. Thrazamund

    Thrazamund

    Joined:
    Apr 14, 2017
    Posts:
    36
    Thanks. I've tried that one before without it working. I used the https://docs.unity3d.com/ScriptReference/Mesh-vertices.html and fed those into a list then tried to use that list in the equation in your link. The major issue I believe is that equation is getting the surface of a 3d polygon and I'm working with 2D objects.
     
  5. Thrazamund

    Thrazamund

    Joined:
    Apr 14, 2017
    Posts:
    36
    I'm also pretty new to Unity and my math knowledge is lacking so I could be doing it wrong.
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    2D objects are still 3D objects. They have a mesh and you can calculate the surface area in the same way.
     
    Hyblademin likes this.
  7. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    This is a pretty well-known formula, and it will work for you, we just need to troubleshoot.

    The author of the code snippet for finding the area specialized their function for flat polygons in the x-z plane. I'm assuming you're using the x-y plane, and you need to change all the transform.position.z parts to transform.position.y parts. If this is correct and you haven't already done this, try it and tell us if that works.

    If not, could you elaborate on what it is or isn't doing?
     
  8. SIRIUSTECHSOLUTIONS

    SIRIUSTECHSOLUTIONS

    Joined:
    Apr 14, 2016
    Posts:
    9
    Here is some code that does the trick very well, found it upon deeper search on the internet
    Code (CSharp):
    1.  
    2. private float CalculateArea()
    3.     {
    4.         var result = 0f;
    5.         for (int p = meshVertices.Length - 1, q = 0; q < meshVertices.Length; p = q++)
    6.             result += (Vector3.Cross(meshVertices[q], meshVertices[p])).magnitude;
    7.  
    8.         return result * .5f;
    9.     }
    10.  
     
    AmbroiseRabier likes this.