NB. built from project: ~Projects\turtle005\turtle005

   NB.  JTurtle  -  0.006
   NB.  Functions to reproduce some Logo commands
   
   NB.  A simple set of functions to povide turtle geometry
   NB.  in J. See the Lab  Turtle Geometry - JTurtle User Guide
   
   NB.  The basic idea is to build a sequence of 'steps'
   NB.  with each turtlestep a forward or turn
   NB.  A forward turtlestep is of the form  (size,0 0,1 0 0,pen)
   NB.  A turn turtlestep is of the form     (0 0 0,z,0 0,pen)
   NB.      with z a complex number on the unit circle.
   NB.  A turtlepath is a sequence of turtlesteps aggregated
   NB.  as a vector.
   NB.  A turtlestate is a vector (z0,0 0,z1,0 0,pen) with
   NB.      z0 the complex number representing the position
   NB.      z1 the complex number representing the heading
   NB.      pen  an integer step type, pen state or other 
   NB.      control information

   NB.  We use a function  show  to draw a turtlepath. It may
   NB.  be used with a left argument to specify the label 
   NB.  to be used for the isigraph control used for display.
   NB.  The function  showf  provides a fixed scale window 
   NB.  covering approximately 100 units each side of the orgin.  

   NB.  A system of defining new path functions using a Logo
   NB.  like 'to' verb is included.

   NB.  There are also tools for combining turtle graphics
   NB.  with cartesian coordinates to place objects.
   
   
   NB.  BASIC FUNCTIONS
   
   load 'graph strings primitives validate numeric trig'
   load '~system\packages\misc\fndef.ijs'


NB.  A test function 
NB.  not in standard validate script

cocurrent 'z'
 
NB.  EmptyQ

NB.  Tests if object is empty or all items are
NB.  empty boxes.

EmptyQ =: 3 : 0
if. -. isboxed y. 
 do.
  if. 0= #y. 
    do. 1
   else.  0 e. $y.
  end.
else.
 *./ > EmptyQ each y.
end.
)




NB.  Set Globals
NB.  Revised 7/9/03


NB.  Set up file using baggage check class for
NB.  text and general gl2 commands
load '~system\classes\pbc\pbc.ijs'

cocurrent 'fjturtle'
SHOWTURTLE =: 1
TS =: 0.12    NB.  Scale unit for turtles
COLOR =: RED
WINSIZE =: 200
TURTLECOLOR =: TEAL
FIXED =: 0          NB.  controls scaling

st =:  3 : 0
SHOWTURTLE =: 1
y.
:
x., st y.
)

showturtle=: st  NB.  synonym

ht =:  3 : 0
SHOWTURTLE =: 0
y.
:
x., ht y.
)

hideturtle =: ht


NB.  Modified  15/2/04
NB.  Verbs to generate new verbs which maintain
NB.  the conventions for a turtlepath and 
NB.  for transmission of function parameters via
NB.  a result vector of the form
NB.  (function argument), path

NB.  to 'functionname  arg1 arg2 arg3';'function definition '
NB.  The arguments are a vector to the right of the function 
NB.  name.  The number of arguments is found, and the arguments
NB.  are assigned the names.  The names are used in the 
NB.  function definition.  The remainder of the argument vector 
NB.  must be a turtle path.

cocurrent 'z'

NB.  (name arg0 arg1 ..argn) todo 'turtle path step definition'
NB.   'spike s' todo 'fd s bk s'

todo =: 4 : 0
split  =. (dlb  x.) i. ' '
fnam   =. split{.x.
args   =. split}.x.
nargs  =.  #;:args
nargsf =. ": nargs
if. nargs = 1 
 do. s    =. '''',args,''' =.  ','{., y.'
 else. s    =. '''',args,''' =.  ',nargsf,' {., y.'
end.
s =. s,: '(',y.,'),', nargsf,' }. ,y.'
s =. s,' : '
s =. stZ =:  s,'x. ,', fnam ,' y.'
". fnam ,'=: 3 :  s'
)

NB.  Modified form of todo to permit extended sequence of 
NB.  J commands.  Usage

NB.  todo 'spike s'
NB.  fd s bk s
NB.  )

NB.  todo  calls for input from the input device
NB.  and continues to accept lines until it reads
NB.  a right parenthesis in the first position of a line.

NB.  The result of the expression entered will be the 
NB.  steps added to the path.  Note the definition of an 
NB.  auxilliary function   nameZZ  to generate and return
NB.  the path steps.

NB.  The common usage of this form is in the definition
NB.  of recursive functions.

todo1 =: 3 : 0
split  =. (dlb  y.) i. ' '
fnam   =. split{.y.
args   =. split}.y.
nargs  =.  #;:args
nargsf =. ": nargs
if. nargs = 1 
 do. s    =. '''',args,''' =.  ','{., y.'
 else. s    =. '''',args,''' =.  ',nargsf,' {., y.'
end.
g =. 0 : 0
s =. (,: s)
s1 =. (,s),  stoa g
". fnam,'ZZ =: 3 : s1'
s =. s, '(,',fnam,'ZZ y.), ', nargsf,' }. ,y.'
s =. s,' : '
s =. s,'x. ,', fnam ,' y.'
". fnam ,'=: 3 :  s'
)

 
to =: 3 : 0
if. 2=#y. do.
'nom def' =. y.
nom todo def
else.
nom =. >y.
todo1 nom
end.
)

ForItems =: "_1
ForAtoms =: "0
ForRows  =: "1

By =: ,.


NB.  Verify environment
NB.  modify functions in _jgl2_ locale if not windows
NB.  deleting references to glwindoworg 
cocurrent 'fjturtle'

NB. pclear   clear graphics window
pclear_jzgraph_ =: 3 : 0
pselect''
glclear''
glwantresize''
if. -.IFJAVA
 do. glwindoworg 0 0
end.
glwindowext ,~ EXTENT
if. #y. do.
  gdbrush y.
  gdrect _1 _1 2 2
end.
)

NB.*psize v resize of graph window
psize_jzgraph_ =: 3 : 0
getstate''
getsize''
'w h'=. WH
if. IFANI do.
  glwindowext EXTENT
else.
  select. * w - h
  case. 0 do.
    s=. 0 0
  case. 1 do.
    s=. 0 ,~ (-:h-w) % h
  case. _1 do.
    s=. 0 , (-:w-h) % w
  end.
if. -. IFJAVA
  do. glwindoworg <. s * EXTENT
end. 
  glwindowext EXTENT * (% <./) WH % INIWH
end.
)


   NB.  Basic motions in TurtleJ
NB.  Revised 7/9/03
cocurrent 'fjturtle'
  
   NB.  BASIC FUNCTIONS
   
   NB.  angles to radians
tocis =: [: r. 2p1"_*]%360"_
   
NB.  Main movement functions 
  
forward =: fd =: 3 : 0
if. 1 = #y.
 do. y.,0 0 1 0 0 2
 else.
 (({.y.), 0 0 1 0 0 2),}.y.
end.
:
x.,fd y.
)
   
back =: bk =: 3 : 0
fd (-{.y.),}.y.
:
x.,bk y.
)
   
   NB.  Change of direction  
right =: rt =: 3 : 0
if. 1 = #y.
 do. 0 0 0 ,(tocis -y.),0 0 1
 else.
  (0 0 0,(tocis -{.y.),0 0 1),}.y.
end.
:
x.,rt y.
)
   
left =: lt =: 3 : 0
rt (-{.y.),}.y.
:
x.,lt y.
)

   NB.  multiple sets of similar movements  
repeat =: 3 : 0
if. isboxed y.
 do.
   'n path' =. y.
   convertpath ,( n#,: path),"1 ' '
   return.
 else.
n =. {.y.
yt =. }. y.
, (n,#yt)$yt
end.
:
x., repeat y.
)

NB.  Also define turtle

turtle =: 3 : 0
s =. {. ,y.
's1 s2'  =. s* 1,2 o. 5r6*0.5p1
(rt _90 fd s2 rt 105 fd s1 rt 150 fd s1 rt 105 fd s2 rt 90),}. ,y.
:
x., turtle y.
)




NB.  Start up position and heading
NB.  Revision  7/9/03
NB.  Omit  setpos, setx
NB.  Normal usage at beginning of turtlepath
NB.  
cocurrent'fjturtle'
to 'setxy x y';'(x j. y),0 0 1 0 0 12'
to 'setx x';'x,0 0 1 0 0 13'
to 'sety y';'y,0 0 1 0 0 14'

to 'plusx x';'x,0 0 1 0 0 8'
to 'plusy y';'y,0 0 1 0 0 9'

setpos =: setxy

NB.  home in a path creates a  
NB.  move to the origin.  

home =: 3 : 0
if.   0=# y.
 do.  setxy 0 0
else. setxy 0 0,y.
end.
:
x., home y.
)

NB.  Use to set heading at beginning of path
to 'seth deg';'0 0 0 ,(0j1*tocis -deg),0 0 11'
to 'sethm deg';'0 0 0 ,(tocis deg), 0 0 11'

to 'sethtowards xx yy';'(xx j. yy),0 0 1 0 0 6'
to 'fddistance xx yy';'(xx j. yy), 0 0 1 0 0 7'

setheading =: seth







NB.  glcall and xyh
NB.  glcall (gl2 command);path
cocurrent 'fjturtle'

glcall =: 3 : 0
if. -. (isboxed y.) *. 2 =#y.
 do.  'Domain Error in glcall' return. 
  else.
  'text p'=. y.
  n=. put__bc1 text
  n,0 0 0 0 0 61,p
end.
:
x., glcall y.
)

xyh =: 3 : 0
NB.  saves location in variable during execution of show
if.   -. (isboxed y.) *. 2 =#y.
 do.  'Domain Error in xyh' return. 
  else.
   'varname p' =. y.
   vz =. put__bc1 varname
   vz,0 0 0 0 0 25,p
end.
:
x. , xyh y.
)



NB.  Some more graphics using cartesians
NB.  These functios reset the start location
NB.  for paths.
NB.  
cocurrent 'fjturtle'

topos =: 4 : '< x. , > y.' "1 0 

spos =: 4 : '< penup seth 0 setpos x. pendown ,,>y.'

NB.  Usage   show  (x,y) start (boxed) path(s)
start =: 4 : 0
xy =. x.
if. isboxed xy do. xy =. > ,xy end.

y =. y.
if. -. isboxed y
 do. 
   if. ischaracter y
     do. y =. convertpath y
   end.
   y =. < y
end.

y =. convertpath each y

if. -. 2=#$xy
 do. xy =. array2 xy
end.
if. 1=#y
 do.  xy spos"1 _ y
 else. xy spos"1 0 (#xy)$y
end.
)


NB.  Uses (complex left arg) startj (boxed) path(s)
startj =: 4 : 0
y =. y.
if. -. isboxed y
 do. y =. <y
end.
if. 1 =#x. 
 do.
  (+. ,x.) start y
 else.
  (+.,x.) start (#,x.)$y
end.
)

NB.  Calculation of plotting data from input stream

NB.  preprocess

Note 1
Cases 
0    initialise  
1    rt lt
2    fd bk

6    sethtowards newpos  ( moves heading to face newpos) 
7    fddistance newpos  (moves distance to newpos on current heading)
8    plusx
9    plusy

Set
11   seth
12   setxy
13   setx
14   sety



Queries
21   heading         n.a.
22   pos = xycor     n.a.
23   xcor            n.a
24   ycor            n.a.
25   xyh

31   home

Controls
41   penup
42   pendown
51   color
61   glcall
71   line
72   fill
)
cocurrent 'fjturtle'

preprocess =: 3 : 0
:
x =. ,x.
select. n =. {:x
NB.  fd and bk
case. 0 do.
   'd th' =. 0 3{ x
   'p h' =. 0 3{  y.
   h =. th * h
   p =. p + d * h
   (p,0 0,h,0 0 0)
NB. rt and lt
case. 1 do.
   'd th' =. 0 3{ x
   'p h' =. 0 3{  y.
   h =. th * h
   p =. p + d * h
   (p,0 0,h,0 0 1)
case. 2 do.
   'd th' =. 0 3{ x
   'p h' =. 0 3{  y.
   h =. th * h
   p =. p + d * h
   (p,0 0,h,0 0 2)
case. 6 do.
   'd th' =. 0 3{x
   'p h' =. 0 3{y.
   h1  =. 0j1*(r. 1{*. d-p)%h
   (p,0 0,h1,0 0 6)
case. 7 do.
   'd th' =. 0 3{x
   'p h'  =. 0 3{y.
    d1 =. {. *.d-p
    p =. p+d1*h
    (p,0 0,h,0 0 7)
case. 8 do.
   ' d'   =. 0{x
   'p h'  =. 0 3{y.
   ((p+d),0 0,h,0 0 8)
case. 9 do.
   'd'    =. 0{x
   'p h'  =. 0 3{y.
   ((p+j.d),0 0,h,0 0 9)
  NB. seth
case. 11 do.
   th =. 3{x
   'p h' =. 0 3{y.
   (p,0 0,th,0 0 11)
  NB. setxy
case. 12 do.
   'xjy' =. {.x
   'p h' =. 0 3{y.
   ((xjy), 0 0 ,h, 0 0 12)
  NB.  setx
case. 13 do.
    sx   =. {.x
   'p h' =. 0 3{y.
    ((sx j. {:,+.p),0 0,h,0 0 13)
  NB.  sety
case. 14 do.
    sy   =. {.x
   'p h' =. 0 3{y.
    ((({. ,+.p) j. sy),0 0,h,0 0 13)
NB. return state of turtle  x,y,h
case. 25 do.
    gtest =:'p h' =. 0 3{y.
    ". (get__bc1 {.x),' =:(,+.p), dfr r.^:_1 h'
    y.
NB. home
case. 31 do.
   'p h' =. 0 3{y.
   (0 0 0 ,h,0 0 31)
NB. pd pu color now not processed
end.
)

array7 =: 3 : 0
if. -. 0=7|#,y. 
 do. 'Domain Error in array7' return.
 else. 
   (((#,y.)%7),7)$,y.
end.
) 


NB.  processmain tp
NB.  result is of form  array7  with 
NB.  for 2D case
NB.  0 1 2  xjy 0 0
NB.  3 4 5  cis 0 0
NB.  6      type
NB.  ready to be used as input to show.

processmain =: [: |. [: preprocess/ \. [: |. reqarray7

NB.  Test addition for case where rows of zeros in array7





NB.  New control for jturtle

NB.  Initial control functions
cocurrent 'fjturtle'

to 'color r g b';'(7{.r,g,b,0 0 0 50)'
colour =: color

to 'penup ';'0 0 0 0 0 0 41'
pu =: penup
to 'pendown ';'0 0 0 0 0 0 42'
pd =: pendown

to 'line ';'0 0 0 0 0 0 71'
to 'fill ';'0 0 0 0 0 0 72'

NB.  Control and movement globals

CONTROL =: 41 42 50 61 71 72 
MOVE=: 0 1 2 6 7 8 9 11 12 13 14 21 22 23 24 25 31

NB.  functions to generate default controls, heading and position
cinit =: 3 : 0
pendown line color COLOR,y.
)

minit =: 3 : 0
0 0 0 0j1 0 0 11 0 0 0 1 0 0 12,y.
)

NB.  Utilities to split path data into boxes

neq =: [: +/\ 1:,}.~:}:  NB.  requires boolean vector argument
                         NB.  gives count for each string of equal
                         NB.  values
boxneq =: neq@[ < /. ]   NB.  boolean boxneq list

NB.  Convert to boxed arrays of movement and control actions

NB.  preproc1  converts from path to boxed array matrix

preproc0 =: 3 : 0   NB.  for showing turtle
y =. reqarray7 y.
cm =. ({:"1 y ) e. MOVE
cmi =. I. cm
y =. (processmain cmi{y) cmi}y
cmb =. {. each  cm boxneq cm
cmb,.cm boxneq y
)

reqarray7 =: 3 : 0
y =. array7 y.
(0<{:"1 y)#y
)



NB.  preproc1  is the case to use when initialization is 
NB.  required to set heading 0j1 and loc  0 0
NB.  and with default line and pendown setting

preproc1 =: [: preproc0 [: minit cinit


NB.  Add starting position for each movement vector
NB.  from position at end of last block of movements

preproc2 =: 3 : 0 
mi =. I. (<1) = v0 =. {."1 y.
v1 =. 1{"1 y.
if. 1<#mi
 do.
  for_i.
   }. i. #mi
  do.
   vi =. <({: >((i-1){mi){v1),>(i{mi){v1
   v1 =. vi (i{mi)}v1
  end. 
end.
v0,.v1
)

proc0 =: [: preproc2 preproc0
proc1 =: [: preproc2 preproc1
process  =: proc1

mtoxy =: 3 : 0 "1
't tp' =. y.
if. t = 0
 do. y.
 else.
 t; (+. {."1 tp ) 0 1}"1 tp
end.
)

deldup =: {. ,([: -. [: *. /"1 }.=}:)#}:

xyproc0 =: [: mtoxy proc0
xyproc1 =: [: mtoxy proc1

NB.   mtoxy preproc z1
NB.  converts path to x y coordinates in boxed groups
NB.  still to link to controls and to plot.

NB.  For some processing delete controls

delc =: 3 : 0
((<1)=0{"1 y.) # y.
)





NB.  Coordinates for drawing turtle

NB.  automatically scale items to fit graph form
NB.  scale turtlepath to fit range  (_1,1)
cocurrent 'fjturtle'

NB.  scale to fit inside window (_1,1)
NB.  array2 tpasxy    Rank 1
array2 =: 3 : 0
n =. #y.
n2 =. <.-:n
(n2,2)$y.
)

extremes =: >./,<./
scalefactors =: (([: -: +/),-/)@extremes

scalevals =: 3 : 0
sf =. scalefactors y.
(y.-{.sf)%0.55*{:sf
:
(y.-{.x.)%0.55*{:x.
)

NB.  scale  tptoxy turtlepath
NB.  uses combined scale factors to scale all 
NB.  turtleplots for plotting
NB.  To preserve aspect ratio same scale factor
NB.  must be used for x and y
NB.  but the two components may be translated
NB.  differently to center plot
NB.  right argument must be vector of form ,x,.y
NB.  In dyadic form left argument is of form
NB.  (SFX,SFY) prior to adjustment to match 
NB.  the scaling multipliers
NB.  0 1 0 1 leaves data unchanged

scale =: 3 : 0
if. isboxed y.
 do. 
  xyb   =:  (2&{."1) each y.
  x     =.  {."1 each  xyb
  y     =.  1&{"1  each  xyb
  SFX   =:  scalefactors > (,&.>) / x
  SFY   =:  scalefactors > (,&.>) / y
  SFXY  =:  >./({:SFX),{:SFY
  SFX   =:  ({.SFX),SFXY
  SFY   =:  ({.SFY),SFXY 
  grx   =.  SFX&scalevals each x
  gry   =.  SFY&scalevals each y
  gr    =.  (,.&.>)/"1 grx,.gry
  res   =.  gr to2"0 0 y.
 else.
  scale <y.
end.
:
if. 0 = +/x.
 do.  scale y.
      return.
 else.
'SFX SFY' =: 0 2{x.       NB. x. is xmean,xrange,ymean,yrange
SFXY      =: >./1 3{x.
SFX       =: SFX,SFXY
SFY       =: SFY,SFXY
if. isboxed y.
 do.
  xyb   =.  (2&{."1) each y.
  x     =.  {."1 each  xyb
  y     =.  1&{"1  each  xyb 
  grx   =. SFX&scalevals each x
  gry   =. SFY&scalevals each y
  gr    =. (,.&.>)/"1 grx,.gry
  res   =.  gr to2"0 0 y.
 else.
  x. scale <y.
end.
end.
)

to2 =: 4 : 0
<(>x.) 0 1 }"1 >y.
)


   


NB.  Plotting functions
NB.  Revised 5/2/04

NB.  Plot using graph class

cs =: show

show =: 3 : 0
NB. k =. 'graph', ":>18!:5 ''

k =. ":>18!:5 ''
if. k -: 'base' do. k =. 'Graph' else. k =. 'Graph',k end.
k show ,y.
:
y =. y.
if. EmptyQ y
do. x. show fd 0
    return. 
end.
if. ischaracter y.
 do. y =. convertpath y.
end.
if. isboxed y
 do. y =. convertpath each y
end.
gdopen x.
gdpen 0.5 0
gdpencolor COLOR
if. isboxed y
 do. 
    x. show >Jg/ ,y
 else.
    showB ,y
end.
showT ''
gdshow''
)

Jg =: 4 : 0
< (>x.),penup minit pendown ,>y.
)


showB =: 3 : 0
'' showB y.
:
gp =: }. proc1 ,y.
gr =: x. scale (0:{"1 +.) each  1{"1 mtoxy "1  delc gp
gc =. I.  (<1)={."1 gp
gq =: (0{"1 gp),. gr gc}1{"1 gp
NB.  add turtle
gpl =: {:>{:{: gp
grl =: _2{. >{: gr
for_j.
  i. # gq
do.
  drawg j{gq
end.
)


showT =: 3 : 0 
gpl =: {:>{:{: gp
grl =: 2{. {: >{: {: gr
if. SHOWTURTLE
 do.
   grloc =: setxy grl seth 90-(360%2p1)*{: , *. 3{gpl
   gt2 =: ,+. {."1  processmain  grloc,turtle TS
   TEAL gdlines gt2
end.
) 

NB.  converts a path represented by a noun character string or array to
NB.  a numeric vector, and leaves a numeric representation unchanged.

convertpath =: 3 : 0
if. ischaracter y. 
do.
ZQW =. ,y.
if. +./ (CR,LF ) e. ZQW
  do. ZQW =. ZQW rplc CR;' ';LF;' '
end.
  ZQW =. ". ZQW
 else.
  y.
end.
)


lastxy =: 3 : 0
+. {. {:>{:{: gp
)

lasth =: 3 : 0
-((360%2p1)* 1{*. 3{{:>{:{: gp)-90
)

drawg =: 3 : 0 "1
if. (0{y.)=<0
 do.
  cvals =. >1{y.
  cn    =. {:"1 cvals
  for_i.
       i. #cn
   do.
   select. i{cn
    case. 41 do. UP=:1
    case. 42 do. UP=:0
    case. 50 do. gdpencolor (3{. ,i{cvals)<.255
    case. 61 do. ". get__bc1 {.i{cvals
    case. 71 do. gdfn =: gdlines
                 glbrushnull_jgl2_ ''
    case. 72 do. gdfn =: gdpolygon
                 glbrush_jgl2_ ''
   end.
  end.
  return.
end.
NB.  do drawing
if. UP 
   do.  return.
end.
gdfn  ,0 1{"1 >1{y.
)



NB.  Fixed format plot using graph class
showf =: 3 : 0
k =. ":>18!:5 ''
if. k -: 'base' do. k =. 'Graph' else. k =. 'Graph',k end.
k showf ,y.
:
y =. y.
if. EmptyQ y
do. x. showf fd 0
    return. 
end.
if. ischaracter y.
 do. y =. convertpath y.
end.
if. isboxed y
 do. y =. convertpath each y
end.
gdopen x.
gdpen 0.5 0
gdpencolor COLOR
if. isboxed y
 do. 
    (4$ 0,WINSIZE)&showB >Jg/ y
 else.
    (4$0,WINSIZE)&showB ,y
end.
showT ''
gdshow''
)

cs =: show

cocurrent 'fjturtle'
NB.  File operations

NB.  Save plot as file

save =: 3 : 0
glfile y.
glsave 1 10000 10000
)

NB.  Save plot to clipboard.  Note plot inverted if pasted to Word
clip =: 3 : 0
glfile ''
glsave 1 10000 10000
)



NB.  Oncircle
cocurrent 'fjturtle'

cside =: 3 : 0
'r d' =. 2 {. y.
n =. 360%d
s =. r * 1 o. 1p1*d%360
x =. -r * 2 o. 1p1*d%360
penup lt 90 fd x rt 90 pendown fd s rt d ,(repeat (n-1) fd (2*s) rt d) fd s pu rt 90 fd (-x) lt 90 pendown '', 2}.y.
:
x.,cside y.
)

to 'oncircle r d';'cside r, d'

NB.  generates circle radius r with n sides
NB.  tangent at current position and heading
polyrn =: 3 : 0
polyrd (0{y.),(360%(1{y.)),2}.y.
: 
x. , polyrn y.
)

NB.  generates circle radius r with exterior angle
NB.  polygon deg.  
NB.  tangent at current position and heading
polyrd =: 3 : 0
'r deg' =. 2{.y.
n =. (360 *. deg) % deg
if. (n>360) +. 0 ~: 1|n 
 do.  'Domain error in circled' return. 
 else.
s =. r * 1 o. 1p1*deg%360
(rt (-:deg),( repeat n  fd (2*s) rt deg), lt (-:deg)),2}.y.
end.
: 
x.,polyrd y.
)

arcr =: 3 : 0
'r deg' =. 2{.y.
chord =. 2 * r * 1 o. 1p1%360
if. 0 = 1|deg
 do.
   (rt 0.5 ,(repeat deg fd chord rt 1), lt 0.5),2}.y.
 else.
   res = 1|deg
   chordres =. 2 * r * 1 o. 1p1%360
   g =. rt 0.5 ,(repeat deg fd chord rt 1), lt 0.5
   g, rt (res%2) fd chordres rt (res%2),2}.y.
end. 
: 
x.,arcr y.
)

arcl =: 3 : 0
'r deg' =. 2{.y.
chord =. 2 * r * 1 o. 1p1%360
if. 0 = 1|deg
 do.
   (rt _0.5 ,(repeat deg fd chord rt _1), lt _0.5),2}.y.
 else.
   res = 1|deg
   chordres =. 2 * r * 1 o. 1p1%360
   g =. rt _0.5 ,(repeat deg fd chord rt _1), lt _0.5
   g, rt (-res%2) fd chordres rt (-res%2),2}.y.
end. 
: 
x.,arcl y.
)

to 'arrow s';'rt 25 bk s fd s lt 50 bk s fd s rt 25'

repetitions =: 3 : 0
roundint (360*.y.) % y.
)




NB.  Some graphics defined using the verb  to  
NB.  each of these graphics can be used in any path expression.
NB.  
cocurrent 'fjturtle'
turns =: 3 : 'roundint (360 *. y.) % y.'
to 'poly side angle' ; 'repeat (turns angle) fd side rt angle'
to 'polyn side n' ; 'repeat n fd side rt 360%n'
to 'square side'; 'repeat 4 fd side rt 90'
to 'halfsquare s';'fd (0.5*s)rt 90 fd s rt 90 fd (0.5*s)'
to 'squarec s';'pu lt 90 fd (s%2) rt 90 pd (repeat 2 halfsquare s) pu rt 90 fd (s%2) pd lt 90'
to 'circle r'; 'arcr r,360'
to 'circlec r'; 'pu lt 90 fd r rt 90 pendown circle r pu rt 90 fd r pendown lt 90  fd 0' 

to 'petal rad deg';'arcr(rad,deg)rt (180-deg)arcr(rad,deg) rt 180-deg'
to 'flower rad deg angle';'repeat ((360*.angle)%angle) petal(rad,deg) rt angle'
to 'polysn s n'; 'repeat n fd (s*1p1%n) rt 360%n'
to 'fdr s';'fd s rt 90'
to 'thing ';'(,fdr "0 ]100 100 50 50 100 25 25 50)lt 90'
to 'thing1 ';'repeat 4 thing i.0'    


bc1 =: conew 'pbc'
create__bc1''

cocurrent 'base'
('fjturtle';'jgl2';'jzgraph';'z') copath 'base'
coclass 'fjturtle'



