(determine direction for polygon described by ordered points) (minimum 3 points, up to 6 points,may fail for pathological cases) (return: _dir: == 2 for cw, 3 for ccw) o sub (not_a_subfile) # = #1 (=4 no. of points) # = #2 (minimum 3 points) # = #3 # = #4 # = #5 # = #6 # = #7 # = #8 (=0 0 if not used) # = #9 (=0) # = #10 (=0) # = #11 (=0) # = #12 (=0) # = #13 (=0) (print dir:n=# # # # # # # # #) (compute direction using cross product: _cross:dir, ignore if EQ 0) # = 0 o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o if [# EQ 4] o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o endif o if [# EQ 5] o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o endif o if [# EQ 6] o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o call [#][#][#][#][#][#] # = [# + #<_cross:dir>] o endif o if [[# GE 7] or [# LE 0]] (print, dir:problem bad n=#) (debug, dir:problem bad n=#) o endif ;o if [# eq 0] ; (print, dir:problem dir=#) ; (debug, dir:problem dir=#) ; m2 ;o endif (above computes cross product, to give traversal direction, swap) o if [# GT 0] #<_dir:> = 2 (cw) o else #<_dir:> = 3 (ccw) o endif o if [# EQ 0] #<_dir:> = 3 (use ccw) o endif (print dir:direction=#<_dir:>) (debug dir:direction=#<_dir:>) o endsub ;wikipedia ;convex polygon: any line drawn through the polygon and not tangent to ; an edge or corner) meets its boundary exactly twice. ;above uses cross product 1-2 X 3-2 ;below uses cross product 2-1 X 3-2 (negative of above, opposite test) ;rhttp://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/clockwise.htm ;If the polygon is known to be convex then one only has to consider the ;cross product between any two adjacent edges. A positive cross product ;means we have a counterclockwise polygon. There are some tests that may ;need to be done if the polygons may not be "clean". In particular two ;vertices must not be coincident and two edges must not be colinear. ;For the more general case where the polygons may be convex, it is ;necessary to consider the sign of the cross product between adjacent ;edges as one moves around the polygon. If there are more positive cross ;products then the overall polygon is ordered counterclockwise. There are ;pathological cases to consider here as well, all the edges cannot be ;colinear, there must be at least 3 vertices, the polygon must be simple, ;that is, it cannot intersect itself or have holes.