Shoelace formula or algorithm

I love the power of mathematics to make difficult things tractable by an algorithm. One such algorithm which I have always been impressed by is the so called shoelace formula/algorithm. It was mentioned by Johan Carl Friedrich Gauss 1777 -1855 (thank you Wikipedia for the dates) in his writings and so is often referred to as Gauss’ Area Formula although he did not invent it. I have written a version of it in SketchUp ruby code for anyone who is interested. Here it is -

SquareInches2SquareMetres = 0.00064516
model=Sketchup.active_model
entities = model.entities
face = nil
#find the only or last face in the models entities collection
#if you want an accurate area then the face must be parallel to the xy plane. If not this calculates the area of the 'shadow' of the plain, cast by an overhead sun.
entities.each{|e| if e.is_a? Sketchup::Face then face = e end}
#get the SketchUp outer loop of this face. A loop is the chain of vertices in clockwise order around the face. I think it is clockwise but it doesn't really matter for this purpose.
loop = face.outer_loop
#get the vertices
vertices = loop.vertices
#turn the vertices into SketchUp points
points_array = []
vertices.each{|vertex| points_array = points_array + [vertex.position]}
#now determin the area using the shoelace algorithm
area = 0.0
points_array.each_with_index{|point, index|
  if index + 1 < points_array.length
    point2 = points_array[index +1]
  end
  if point2
  #shoelace algorithm
    area = area + ((point.x * point2.y) - (point2.x * point.y))
  else
  #use the first point with the last point when all the other points have been done
    area = area + ((point.x * points_array[0].y) - (points_array[0].x * point.y))
  end
}
#divide by 2 and get the absolute. I  have converted the result to metres but that is optional. Leave it in square inches if you prefer.
area = (area/2.0*SquareInches2SquareMetres).abs
puts("area is #{area} square metres.")

Enjoy!

1 Like

This is probably what the face.area method uses already.
This algorithm is sometimes called the “surveyors” formula because in olden days most roads bounding property were straight (or if curved, the property lines ignored them) and all you needed to calculate area were endpoints of property boundaries. With the invention of faster automobiles, and the desire for meandering roads and cul de sacs, many roads from highways to roads in suburban neighborhoods became curved. I had to develop my own algorithm within 2DXY SiteSurvey to accurately calculate area for property boundaries than include arcs. Accuracy is crucial for calculating legally allowable lot coverage.

1 Like