(info:dir determine direction for polygon described by ordered points) (minimum 3 points, up to 6 points) (return: #<_dir:> == 2 for CW, 3 for CCW, -1 for undetermined) (return: #<_dir:area>) (return: #<_dir:cx>, #<_dir:cy> centroid) o sub # = #1 (=4 N neg:verbose) # = #2 # = #3 # = #4 # = #5 # = #6 # = #7 # = #8 (=0) # = #9 (=0) # = #10 (=0) # = #11 (=0) # = #12 (=0) # = #13 (=0) # = 0 o if [# LT 0] # = [ABS[#]] # = 1 o endif o if [[# GE 7] or [# LE 2]] (print, dir: problem bad n=#) (debug, dir: problem bad n=#) #<_dir:> = -1 ;undetermined with message, caller must handle o endif # = 0 # = 0 # = 0 # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] o if [# eq 3] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] o endif o if [# eq 4] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] o endif o if [# eq 5] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] o endif o if [# eq 6] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] # = [# * # - # * #] # = [# + #] # = [# + [# + #] * #] # = [# + [# + #] * #] o endif #<_dir:cx> = [# / # / 3] ; signed area reqd here #<_dir:cy> = [# / # / 3] ; signed area reqd here #<_dir:area> = [ABS[#]/2] ; return unsigned area o if [# GT 0] #<_dir:> = 3 (ccw) o else o if [# LT 0] #<_dir:> = 2 (cw) o else #<_dir:> = -1 ;undetermined o endif o endif o if [# GT 0] o if [#<_dir:> eq 2] (debug, dir: CW #<_dir:> area=#<_dir:area> cx=#<_dir:cx> cy=#<_dir:cy>) o else (debug, dir: CCW #<_dir:> area=#<_dir:area> cx=#<_dir:cx> cy=#<_dir:cy>) o endif o endif o endsub ; ref: many sources for polygons on the web, for example: ; http://paulbourke.net/geometry/polyarea/