


%begin
   %include "level1:graphinc.imp"
 ! %external %routine %spec quad %alias "FRED_GRAPHICS_QUAD" %c
 !             (%integer x0, y0, x1, y1, x2, y2, x3, y3)


   %routine cursor(%integer %name x, y)
      %own %integer ol = 0, or = 0, ob = 0, ot = 0,
                    cx=512, cy=128, ti = 0
      %integer ox, oy, l, r, t, b
   !  %while cputime<ti %cycle; %repeat
   !  ti = ti+10
      ox = x
      oy = y
      cx = cx+rel mouse x
      cy = cy+rel mouse y
      cx = 0 %if cx<0
      cx = 687<<1 %if cx>687<<1
      cy = 0 %if cy<0
      cy = 511<<1 %if cy>511<<1
      x = cx>>1
      y = cy>>1
      %if x#ox %or y#oy %start
         l = x-8
         r = x+8
         b = y-8
         t = y+8
         l = 0 %if l<0
         r = 687 %if r>687
         b = 0 %if b<0
         t = 511 %if t>511
         enable(7)
         colour(0)
         line(ol, oy, or, oy)
         line(ox, ob, ox, ot)
         colour(7)
         line(l, y, r, y)
         line(x, b, x, t)
         ox = x
         oy = y
         ol = l
         or = r
         ob = b
         ot = t
      %finish
   %end
   %routine getxy(%integer %name x, y)

      %while mouse buttons&2#0 %cycle; %repeat
      %while mouse buttons&2=0 %cycle
         %cycle
           cursor(x, y)
         %repeat %until x<=512 %and y<=128
      %repeat
   %end
   %routine rband(%integer x0, y0, %integer %name x1, y1)
      %integer ox, oy
      x1 = x0
      y1 = y0
      %while mouse buttons&2#0 %cycle; %repeat
      %while mouse buttons&2=0 %cycle
         ox = x1
         oy = y1
         %cycle
           cursor(x1, y1)
         %repeat %until x1<=512 %and y1<=128
         %if ox#x1 %or oy#y1 %start
            enable(7)
            colour(0)
            line(x0, y0, ox, oy)
            colour(7)
            line(x0, y0, x1, y1)
         %finish
      %repeat
        enable(7)
        colour(0)
        line(x0, y0, x1, y1)
   %end 
   %routine rband2(%integer x0, y0, x2, y2, %integer %name x1, y1)
      %integer ox, oy
      x1 = x0
      y1 = y0
      %while mouse buttons&2#0 %cycle; %repeat
      %while mouse buttons&2=0 %cycle
         ox = x1
         oy = y1
         cursor(x1, y1)
         %if ox#x1 %or oy#y1 %start
            enable(7)
            colour(0)
            line(x0, y0, ox, oy)
            line(ox, oy, x2, y2)
            colour(7)
            line(x0, y0, x1, y1)
            colour(3)
            line(x1, y1, x2, y2)
         %finish
      %repeat
      enable(7)
      colour(0)
      line(x0, y0, x1, y1)
      line(x1, y1, x2, y2)
   %end
   %integer x0, y0, x1, y1, x2, y2, x3, y3,count,i
   %recordformat xy (%integer x,y)  
   %record (xy) %array coords(0:128*512)
   %record (xy) %array c(0:128*512)

   %on %event 0 %start
      print symbol(27)
      print symbol('R')
      %stop
   %finish


   clear
   enable(8)
   colour(8)
   line(0,0,0,128)
   line(0,128,512,128)
   line(512,128,512,0)
   line(512,0,0,0)
   x0 = 0
   y0 = 0
   getxy(x0, y0)
   count=0
   %cycle
      coords( count )_x = x0 
      coords( count )_y = y0
      rband(x0, y0, x1, y1)
      enable(8)
      colour(8)
      line(x0, y0, x1, y1)
      %exit %if x1<coords(0)_x+5 %and x1>coords(0)_x-5 %and %c
                y1<coords(0)_y+5 %and y1>coords(0)_y-5 
      y0 = y1
      x0 = x1
      count=count+1
   %repeat
   %string(255) file
   %integer %name coord
   prompt("Output File:") ; read(file)
   open output(1,file)
   select output(1)
   %for i=0,1,count %cycle
     print(coords(i)_x,3,0) ; print(coords(i)_y,3,0)
     newline
   %repeat
   close output
   select output(0)
   printstring("Done")
   newline   
   open input(1, file)
   select input(1)
   %for i=0,1,count %cycle
     coord == c(i)_x
     read(coord)
     coord == c(i)_y
     read(coord)
     write(c(i)_x, 2) ; space
     write(c(i)_y, 3) ; newline
   %repeat
   close input
   select input(0)
%end %of %program

