#! /usr/bin/gawk -f 

# Usage: $0 \
#    -v min=MIN \
#    -v xf=XCOL [ -v xn=XNUM ] \
#    -v yf=YCOL [ -v yn=YNUM ] \
#  < FILE
#
# Input: a list of entries, containing two numeric fields "x" and "y".
# Output: a list with the original entries, augmented with
# a new single-digit field TYPE defined as "0" if x^2 + y^2 < min^2,
# else 1 + floor(8.999999*atan(y/x)/(Pi/2))
#
# If "xn" and "yn" are given, the values of "x" and "y" are
# scaled by w/xn and w/yn before 
 
BEGIN { 
  if ((xn == "") && (yn == ""))
    { xscale = 1; yscale = 1; }
  else
    { w = sqrt(xn*xn + yn*yn);
      xscale = w/xn; yscale = w/yn;
    }
  ascale = 8.9999999/atan2(1,0);
}

function type(xnum, ynum,   xs, ys)
{ 
  # Computes entry type from xnum, ynum
  if (min == "") 
    { printf "must define min\n" > "/dev/stderr"; exit 1; }
  
  xs = xscale*xnum;
  ys = yscale*ynum;
  if (xs*xs + ys*ys < min*min) 
    { return "0"; }
  else
    { return (1+int(ascale*atan2(ys,xs)) ""); }
}

/./ { 
  if ((xf == "") || (yf == "")) 
    { printf "must define xf,yf\n" > "/dev/stderr"; exit 1; }
  x = $(xf);
  y = $(yf);
  printf "%s %s\n", $0, type(x,y); next; }

(1) { next; }