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.




INCLUDE "H6:REALMATH.ACT"
 
PROC Area(INT ARRAY xs,ys BYTE count REAL POINTER res)
  BYTE i,next
  REAL x1,y1,x2,y2,tmp1,tmp2
 
  IntToReal(0,res)
  IntToReal(xs(0),x1) IntToReal(ys(0),y1)
  FOR i=0 TO count-1
  DO
    next=i+1
    IF next=count THEN
      next=0
    FI
    IntToReal(xs(next),x2) IntToReal(ys(next),y2)
 
    RealMult(x1,y2,tmp1)
    RealAdd(res,tmp1,tmp2)
    RealMult(x2,y1,tmp1)
    RealSub(tmp2,tmp1,res)
 
    RealAssign(x2,x1) RealAssign(y2,y1)
  OD
 
  RealAbs(res,tmp1)
  IntToReal(2,tmp2)
  RealDiv(tmp1,tmp2,res)
RETURN
 
PROC PrintPolygon(INT ARRAY xs,ys BYTE count)
  BYTE i
 
  FOR i=0 TO count-1
  DO
    PrintF("(%I,%I)",xs(i),ys(i))
    IF i<count-1 THEN
      Print(", ")
    ELSE
      PutE()
    FI
  OD
RETURN
 
PROC Test(INT ARRAY xs,ys BYTE count)
  REAL res
 
  Area(xs,ys,count,res)
  Print("Polygon: ")
  PrintPolygon(xs,ys,count)
 
  Print("Area: ")
  PrintRE(res) PutE()
RETURN
 
PROC Main()
  INT ARRAY
    xs(5)=[3 5 12 9 5],
    ys(5)=[4 11 8 5 6]
 
  Put(125) PutE() ;clear screen
 
  Test(xs,ys,5)
RETURN