Calculating Area

Discussion in 'AutoCAD' started by Mihai, Jan 6, 2004.

  1. Mihai

    Mihai Guest

    Does anybody have an algorithm for calculating the area defined by an array of coordinates (or points)? I need something independent of AutoCAD ActiveX (mean the Area member of AcadEntity class). An analytic approach should be better. Thanks, if anybody...
     
    Mihai, Jan 6, 2004
    #1
  2. Mihai

    LochDhu Guest

    LochDhu, Jan 6, 2004
    #2
  3. found this at http://www.foundrysearch.com/downloads/polygonarea.zip . I
    haven't checked it. It looks like it is for a 1-based array. Also, it uses
    an open polygon, since the first 3 lines close the poly.

    'Greene's Theorem area calculation
    pointCount = pointCount+1
    pointX(pointCount) = pointX(1)
    pointY(pointCount) = pointY(1)
    #main.gb "line ";pointX(pointCount-1);" ";pointY(pointCount-1);"
    ";pointX(pointCount);" ";pointY(pointCount);
    for i = 1 to pointCount-1
    x1 = pointX(i)
    x2 = pointX(i+1)
    y1 = pointY(i)
    y2 = pointY(i+1)
    TrapArea(i) = (x2 - x1)*y1 + (1/2)*(x2 - x1)*(y2 - y1)
    next i
    x1 = pointX(pointCount)
    y1 = pointY(pointCount)
    x2 = pointX(1)
    y2 = pointY(1)
    TrapArea(pointCount) = (x2 - x1)*y1 + (1/2)*(x2 - x1)*(y2 - y1)
    TotalArea = 0
    For i = 1 to pointCount
    TotalArea = TotalArea + TrapArea(i)
    Next i
    #main.area abs(TotalArea)
    wait


    array of coordinates (or points)? I need something independent of AutoCAD
    ActiveX (mean the Area member of AcadEntity class). An analytic approach
    should be better. Thanks, if anybody...
     
    James Belshan, Jan 6, 2004
    #3
  4. This is one way of doing it.

    pts is an array of doubles such that
    pts(0,0) = x1
    pts(0,1) ) = y1
    pts(1,0) = x2
    pts(1,1) = y2


    the last point of the array should be equal to the first (closed polygon)


    Public Function AreaCalc(pts() As Double) As Double
    Dim j As Long
    Dim i As Long
    Dim k As Long
    Dim M As Double
    Dim N As Double

    j = UBound(pts)
    k = LBound(pts)

    For i = k To j - 1
    M = M + pts(i, 0) * pts(i + 1, 1)
    N = N + pts(i + 1, 0) * pts(i, 1)
    Next i
    AreaCalc = Abs(M - N) / 2#

    End Function

    --
    Saludos, Ing. Jorge Jimenez, SICAD S.A., Costa Rica


    array of coordinates (or points)? I need something independent of AutoCAD
    ActiveX (mean the Area member of AcadEntity class). An analytic approach
    should be better. Thanks, if anybody...
     
    Jorge Jimenez, Jan 6, 2004
    #4
  5. Mihai

    gadski Guest

    if you take away the 'abs' from
    AreaCalc = Abs(M - N) / 2#
    it will give you a +ve or -ve area,
    this is good for subtracting 'holes',

    just make sure your polylines
    are counterclockwise for areas,
    and clockwise for holes,
    or viceversa.

    cheers
     
    gadski, Jan 6, 2004
    #5
  6. Mihai

    Mihai Guest

    Thanks to everybody !
     
    Mihai, Jan 7, 2004
    #6
  7. Mihai, the example I posted is simple.
    It lacks error handling and does not include the normalizing of the
    coordinates to minimize rounding errors.
     
    Jorge Jimenez, Jan 7, 2004
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.