: f sqr 3 - ;

 0    variable x
 0    variable xmin
 1    variable xmax
 0.1  variable xstep
-10   variable ymin
 10   variable ymax

   0 constant scrXmin
 239 constant scrXmax
  63 constant scrYmin
   0 constant scrYmax

: scaleX
  dup xmax @ > if drop xmax @ endif
  dup xmin @ < if drop xmin @ endif
  xmin @ -
  xmax @ xmin @ -
  /
  scrXmax scrXmin -
  *
  scrXmin +
;

: scaleY
  dup ymax @ > if drop ymax @ endif
  dup ymin @ < if drop ymin @ endif
  ymin @ -
  ymax @ ymin @ -
  /
  scrYmax scrYmin -
  *
  scrYmin +
;

: plotAxes
  graphics
  ymin @ 0 <
  if
    ymax @ 0 >
    if
      scrXmin 0 scaleY scrXmax 0 scaleY line
    endif
  endif
  xmin @ 0 <
  if
    xmax @ 0 >
    if
      0 scaleX scrYmin 0 scaleX scrYmax line
    endif
  endif
;

0 variable oldx
0 variable oldy
0 variable newx
0 variable newy

: rline
  newy ! newx !
  newx @ oldx @ = not
  newy @ oldy @ = not +
  if
   oldx @ oldy @ newx @ newy @ line
  endif
  newx @ oldx !
  newy @ oldy !
;

: plotf
  xstep ! xmax ! xmin !
  plotAxes
  xmin @ x !
  x @   scaleX oldx !
  x @ f scaleY oldy !
  repeat
    x @ scaleX x @ f scaleY rline
    x @ xstep @ + x !
    x @ xmax @ >
  until
  key drop
  text
;

: zerof
  xstep ! xmax ! xmin !
  xmin @ f ymin !
  xmax @ f ymax !
  ymin @ ymax @ * 0 <
  if
    repeat
      xmin @ xmax @ + 2 / dup dup
      f dup ymin @ * 0 <
      if
       ymax !
       xmax !
      else
       ymin !
       xmin !
      endif
      .
      xmax @ xmin @ - xstep @ <
    until
    xmax @ xmin @ + 2 /
  else
    7 emit
  endif
; 

save plot
quit
