Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Compute the Area of an Irregular Polygon?

    General Talk
    r20 python
    3
    6
    1.6k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      bentraje
      last edited by

      Hi,

      I'm trying to model a floorplan. And to be dead-accurate, I'm thinking if there is a way to compute an area of an irregular polygon?

      This is a sample but I guess something like this.
      https://www.dropbox.com/s/rfa5p36n3dtiqcc/c4d081_area_irregular_shape.JPG?dl=0

      Thank you for looking at my problem.

      1 Reply Last reply Reply Quote 0
      • M
        mp5gosu
        last edited by

        Hi @bentraje,

        you can caluculate the area as normal polygons, if you use GetNGonTranslationMap(). So you have a list of regular polygons.
        To calculate a polygon area, simply use some kind of this function:

        # Calculate face area
        def CalculateFaceArea(op, polyIndex):
            poly = op.GetPolygon(polyIndex)
            v0 = op.GetPoint(poly.a)
            v1 = op.GetPoint(poly.b)
            v2 = op.GetPoint(poly.c)
            v3 = op.GetPoint(poly.d)
            
            if poly.IsTriangle():
                return (0.5 * ((v1-v0) % (v2-v0)).GetLength())
            else: # if quad
                return (0.5 * ((v1-v0) % (v2-v0))).GetLength() + (0.5 * ((v3-v2) % (v0-v2)).GetLength()) 
        
        1 Reply Last reply Reply Quote 1
        • r_giganteR
          r_gigante
          last edited by r_gigante

          Hi Bentraje,

          with regard to computing the area of a non self-intersecting planar polygon you can use:

          # area of a polygon using 
          # shoelace formula 
            
          # (X[i], Y[i]) are coordinates of i'th point. 
          
          def polygonArea(X, Y, n): 
              # Initialze area 
              area = 0.0
          
              # Calculate value of shoelace formula 
              j = n - 1
              for i in range(0,n): 
                  area += (X[j] + X[i]) * (Y[j] - Y[i]) 
                  j = i   # j is previous vertex to i
              # Return absolute ;
              return int(abs(area / 2.0)) 
          

          Best, Riccardo

          1 Reply Last reply Reply Quote 3
          • B
            bentraje
            last edited by

            @mp5gosu

            Thanks for the response, but for some reason, it only works in quads. You can see my test here where it breaks:
            https://www.dropbox.com/s/en84oigny07a1vb/c4d081_area_irregular_shape_v2.JPG?dl=0

            1 Reply Last reply Reply Quote 0
            • B
              bentraje
              last edited by

              @r_gigante

              Thanks for the response. The shoelace formula was new to me and it was interesting after reading some explanation.
              Unfortunately, for some reason it breaks.
              It works with a 4 point plane, but not with a 16-point plane.

              Here is the revised code I used

              import c4d
              
              x = []
              y = []
              
              def main():
                  baseVector = op.GetAllPoints()
              
                  
                  for vector in baseVector:
                      x.append(vector[0])
                      y.append(vector[1])
                      
                      
              def polygonArea(X, Y, n): 
                  # Initialze area 
                  area = 0.0
              
                  # Calculate value of shoelace formula 
                  j = n - 1
                  for i in range(0,n): 
                      area += (X[j] + X[i]) * (Y[j] - Y[i]) 
                      j = i   # j is previous vertex to i
                  # Return absolute ;
                  return int(abs(area / 2.0)) 
                      
              
              if __name__=='__main__':
                  main()
                  print polygonArea(x, y, 16)
              
              

              You can also check the 4 point and 16 point plane here:
              https://www.dropbox.com/s/6qfhy9yw3l3xdil/c4d081_area_irregular_shape.c4d?dl=0

              The area (since this is a square) should result to 160,000 cm2

              Thank you for looking at the problem.

              1 Reply Last reply Reply Quote 0
              • r_giganteR
                r_gigante
                last edited by

                Hi Bentraje, thanks for following up and sorry for coming late here.

                I've checked your geometry and the issue is not with the algorithm, but in both cases with the geometries you're using to compute the area who don't shown vertexes being properly "ordered".
                The algorithm I've briefly presented requires the vertexes to be ordered as if you're visiting them by walking on the outer edge of your n-gon or polygon in CCW or CW. It is then mandatory to generate an ordered list of vertexes before passing the data to the function to get the correct result.

                62d2c929-154a-4c85-b558-af1536d3153d-image.png

                In order to come out with such an ordered list I warmly recommend you to visit the following old-threads:

                • Get all ngons
                • PolygonObject.GetPolygonTranslationMap()
                • Get outer edges of each Ngon

                Best, Riccardo

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post