Results 1 to 7 of 7

Calculating areas of overlapping polygons

Threaded View

  1. #1
    Registered User
    Join Date
    03-01-2019
    Location
    North Carolina, US
    MS-Off Ver
    2018
    Posts
    6

    Calculating areas of overlapping polygons

    I am trying to calculate areas of overlapping polygons and there will never be more than two polygons. A person will generate a 2 series of coordinates that shape two overlapping polygons. I have limited excel abilities, and I kind of know how to manipulate and use macros but not really program them. Ive spent a few hours trying to get this thing to work but it isnt. I havent tweaked the macros I found on this and other forums, but I have brought them in to use. The macros are the following:

    IntersectComplex:
    '
    ' Algebra taken from various sources on the WWW
    '
    Option Explicit
    Public Function IntersectComplex(x1 As Double, y1 As Double, x2 As Double, y2 As Double, LineCoordinates As Range, Axis As Boolean) As Variant
    '
    ' Complex Intersect.
    ' Because the line segments are not uniformly spaced the (xy,y1)(x2,y2) could cross
    ' at any point along the other line
    '
    ' Return
    ' If intersection
    '    requested coordinate
    ' else
    '    nothing
    ' endif
    ' Axis=True returns X value
    ' Axis=False returns Y value
    '
        Dim dblCrossX As Double
        Dim dblCrossY As Double
        Dim dblTestx1 As Double
        Dim dblTesty1 As Double
        Dim dblTestx2 As Double
        Dim dblTesty2 As Double
        Dim intSegment As Integer
        
        With LineCoordinates
            For intSegment = 1 To .Rows.Count - 1
                dblTestx1 = .Cells(intSegment, 1)
                dblTesty1 = .Cells(intSegment, 2)
                dblTestx2 = .Cells(intSegment + 1, 1)
                dblTesty2 = .Cells(intSegment + 1, 2)
                If m_CalculateIntersection(x1, y1, x2, y2, dblTestx1, dblTesty1, dblTestx2, dblTesty2, dblCrossX, dblCrossY) Then
                    If Axis Then
                        IntersectComplex = dblCrossX
                    Else
                        IntersectComplex = dblCrossY
                    End If
                    Exit Function
                End If
            Next
        
            ' Special check for last pairing
            intSegment = .Rows.Count
            dblTestx1 = .Cells(intSegment, 1)
            dblTesty1 = .Cells(intSegment, 2)
            dblTestx2 = .Cells(intSegment, 1)
            dblTesty2 = .Cells(intSegment, 2)
            If m_CalculateIntersection(x1, y1, x2, y2, dblTestx1, dblTesty1, dblTestx2, dblTesty2, dblCrossX, dblCrossY) Then
                If Axis Then
                    IntersectComplex = dblCrossX
                Else
                    IntersectComplex = dblCrossY
                End If
                Exit Function
            End If
            
        End With
        IntersectComplex = CVErr(xlErrNA)    ' Null
        
    End Function
    Private Function m_CalculateIntersection(x1 As Double, y1 As Double, x2 As Double, y2 As Double, _
        x3 As Double, y3 As Double, x4 As Double, y4 As Double, _
        ByRef CrossX As Double, ByRef CrossY As Double) As Variant
    
    'Call with x1,y1,x2,y2,x3,y3,x4,y4 and returns intersect,x,y
    '
    'Where:
    ' x1,y1,x2,y2,x3,y3,x4,y4 are the end points of two line segments
    'Returns:
    ' intersect is true/false, and x,y is the interecting point if intersect is true
    '
    'Description:
    '
    'Equations for the lines are:
    ' Pa = P1 + Ua(P2 - P1)
    ' Pb = P3 + Ub(P4 - P3)
    '
    'Solving for the point where Pa = Pb gives the following equations for ua and ub
    '
    ' Ua = ((x4 - x3) * (y1 - y3) - (y4 - y3 ) * (x1 - x3)) / ((y4 - y3) * (x2 - x1)
    '     - (x4 - x3) * (y2 - y1))
    ' Ub = ((x2 - x1) * (y1 - y3) - (y2 - y1 ) * (x1 - x3)) / ((y4 - y3) * (x2 - x1)
    '     - (x4 - x3) * (y2 - y1))
    '
    'Substituting either of these into the corresponding equation for the line gives
    '     the intersection point.
    'For example the intersection point (x,y) is
    ' x = x1 + Ua(x2 - x1)
    ' y = y1 + Ua(y2 - y1)
    '
    'Notes:
    ' - The denominators are the same.
    '
    ' - If the denominator above is 0 then the two lines are parallel.
    '
    ' - If the denominator and numerator are 0 then the two lines are coincident.
    '
    ' - The equations above apply to lines,
    '     if the intersection of line segments is
    '     required then it is only necessary to test if ua and ub lie between 0 and 1.
    '     Whichever one lies within that range then the corresponding line segment
    '     contains the intersection point. If both lie within the range of 0 to 1 then
    '     the intersection point is within both line segments.
    '
        Dim dblDenominator As Double
        Dim dblUa As Double
        Dim dblUb As Double
        'Pre calc the denominator, if zero then
        '     both lines are parallel and there is no
        '     intersection
        dblDenominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1))
    
        If dblDenominator <> 0 Then
            'Solve for the simultaneous equations
            dblUa = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / dblDenominator
            dblUb = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / dblDenominator
        Else
        
            If (x1 = x3) And (y1 = y3) Then
                CrossX = x1
                CrossY = y1
                m_CalculateIntersection = True
            Else
                m_CalculateIntersection = False
            End If
            Exit Function
        End If
        
        'Could the lines intersect?
        If dblUa >= 0 And dblUa <= 1 And dblUb >= 0 And dblUb <= 1 Then
            'Calculate the intersection point
            CrossX = x1 + dblUa * (x2 - x1)
            CrossY = y1 + dblUa * (y2 - y1)
            'Yes, they do
            m_CalculateIntersection = True
        Else
            'No, they do not
            m_CalculateIntersection = False
        End If
        
    End Function

    PtInPoly:
    Public Function PtInPoly(Xcoord As Double, Ycoord As Double, Polygon As Variant) As Variant
      Dim x As Long, NumSidesCrossed As Long, m As Double, b As Double, Poly As Variant
      Poly = Polygon
      If Not (Poly(LBound(Poly), 1) = Poly(UBound(Poly), 1) And _
            Poly(LBound(Poly), 2) = Poly(UBound(Poly), 2)) Then
        If TypeOf Application.Caller Is Range Then
          PtInPoly = "#UnclosedPolygon!"
        Else
          Err.Raise 998, , "Polygon Does Not Close!"
        End If
        Exit Function
      ElseIf UBound(Poly, 2) - LBound(Poly, 2) <> 1 Then
        If TypeOf Application.Caller Is Range Then
          PtInPoly = "#WrongNumberOfCoordinates!"
        Else
          Err.Raise 999, , "Array Has Wrong Number Of Coordinates!"
        End If
        Exit Function
      End If
      For x = LBound(Poly) To UBound(Poly) - 1
        If Poly(x, 1) > Xcoord Xor Poly(x + 1, 1) > Xcoord Then
          m = (Poly(x + 1, 2) - Poly(x, 2)) / (Poly(x + 1, 1) - Poly(x, 1))
          b = (Poly(x, 2) * Poly(x + 1, 1) - Poly(x, 1) * Poly(x + 1, 2)) / (Poly(x + 1, 1) - Poly(x, 1))
          If m * Xcoord + b > Ycoord Then NumSidesCrossed = NumSidesCrossed + 1
        End If
      Next
      PtInPoly = CBool(NumSidesCrossed Mod 2)
    End Function
    My idea was to generate seperate columns with the intersecting points and points found within each others shapes, stack them into one column, order them counterclockwise using angles between the X and Y points and ranking, and calculate the area. It works sometimes but not all the time.

    The issues always come up
    -A intersection point is missed
    -Points get drawn up incorrectly adding or subtracting from the actual area

    Attached is an example of the area. Blue and orange lines are overlapping each other, and the grey is the result.

    Is there a simpler way to do this?
    Attached Images Attached Images

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Calculating # of Overlapping days with several date ranges and conditions
    By ZafferAhmed in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-30-2021, 05:44 AM
  2. Calculating overlapping date/time for Ambulances for a month
    By mpittmanfl in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-30-2019, 06:52 AM
  3. Calculating the overlapping time intervals
    By Mukund03 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-26-2015, 07:20 PM
  4. Calculating total for multiple overlapping values
    By tonyjackson in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-19-2014, 10:29 AM
  5. Calculating # of Overlapping days with several date ranges.
    By CarlSVM in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 01-06-2014, 07:09 AM
  6. Frequency Polygons what are they?
    By johnmw1 in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 03-02-2010, 03:44 AM
  7. Replies: 1
    Last Post: 03-20-2006, 03:40 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1