Given the n + 1 vertices x[0], y[0] .. x[N], y[N] of a simple polygon described in a clockwise direction, then the polygon's area can be calculated by:

abs( (sum(x[0]*y[1] + ... x[n-1]*y[n]) + x[N]*y[0]) -
     (sum(x[1]*y[0] + ... x[n]*y[n-1]) + x[0]*y[N])
   ) / 2
(Where abs returns the absolute value)

Task

Write a function/method/routine to use the the Shoelace formula to calculate the area of the polygon described by the ordered points:

     (3,4), (5,11), (12,8), (9,5), and (5,6) 



Show the answer here, on this page.




Option Explicit
 
Public Function ShoelaceArea(x() As Double, y() As Double) As Double
Dim i As Long, j As Long
Dim Area As Double
 
  j = UBound(x())
  For i = LBound(x()) To UBound(x())
    Area = Area + (y(j) + y(i)) * (x(j) - x(i))
    j = i
  Next i
  ShoelaceArea = Abs(Area) / 2
End Function
 
Sub Main()
Dim v As Variant
Dim n As Long, i As Long, j As Long
  v = Array(3, 4, 5, 11, 12, 8, 9, 5, 5, 6)
  n = (UBound(v) - LBound(v) + 1) \ 2 - 1
  ReDim x(0 To n) As Double, y(0 To n) As Double
  j = 0
  For i = 0 To n
    x(i) = v(j)
    y(i) = v(j + 1)
    j = j + 2
  Next i
  Debug.Print ShoelaceArea(x(), y())
End Sub