Type point (x As Double, y As Double)
Private Sub signedArea(shape As List) As Double
' NB : This function can return negative values.
Dim this, prev As point
Dim i As Int
Dim result As Double = 0.0
If (shape.Size > 2) Then
prev = shape.Get(shape.Size - 1)
For i = 0 To shape.Size - 1
this = shape.Get(i)
result = result + (prev.x * this.y - this.x*prev.y)
prev = this
Next
End If
Return result / 2
End Sub
Public Sub centroid(shape As List) As point
Dim this, prev, result As point
result.Initialize
Dim x, y, f, a As Double
Dim i, n As Int
If (shape.Size > 2) Then
n = shape.Size - 1
prev = shape.Get(n)
For i = 0 To n
this = shape.Get(i) ' Pick the next point
f = (prev.x * this.y) - (this.x * prev.y)
x = x + (prev.x + this.x) * f
y = y + (prev.y + this.y) * f
prev = this
Next
a = signedArea(shape) * 6
result.x = x / a
result.y = y / a
End If
Return result
End Sub