Version 1 of Polygon area

Updated 2004-07-23 09:46:15 by suchenwi

if 0 {Richard Suchenwirth 2004--07-23 - Given a convex polygon represented as {x y x y ...} list of its corner coordinates, I wanted to calculate its area. This is simple for the special case of the triangle, using the "Heronic formula":}

 proc triangle'area coords {
    foreach {xa ya xb yb xc yc} $coords break
    set a [expr {hypot($yb-$yc, $xb-$xc)}]
    set b [expr {hypot($ya-$yc, $xa-$xc)}]
    set c [expr {hypot($ya-$yb, $xa-$xb)}]
    set s [expr {($a+$b+$c)/2.}]
    expr {sqrt($s*($s-$a)*($s-$b)*($s-$c))} ;# Heronic formula
 }

if 0 {For the general polygon I devised this recursive approach, which de facto does a triangulation, by measuring the area between the first three points, and then calling itself with the second corner's coordinates removed from the list: }

 proc polygon::area coords {
    expr {
          [llength $coords]<6? 0 :
          [triangle'area [lrange $coords 0 5]]
          + [area [lreplace $coords 2 3]]
      }
 }

See also Drawing and rotating polygons - Regular polygons - Polygon intersection


Arts and crafts of Tcl-Tk programming