!  graph     This program plots the part of the graph of y=f(x)
!            on the interval [a,b] which is inside of the rectangle 
!            { (x,y) : xmin < x < xmax  &  ymin < y < ymax }

def f(x)=sin(x)/x        ! the function to be plotted

let a=-6              ! [a,b] is the interval on which f(x) is to plotted
let b= 6

let xmin= a         ! left edge of screen
let xmax= b         ! right edge of screen
let ymin=-4            ! bottom of screen
let ymax=-ymin         ! top of screen      y/x ratio of about 2/3 is best

let n=593
let dx=(b-a)/n         ! this determines the smoothness of the plot

sub plot
  for x=a to b step dx
    plot x,f(x);
    if f(x) < ymin or f(x) > ymax then plot
  next x
end sub

sub mouser
  let xm=xmin+(xmax-xmin)/6
  let ym=ymax-(ymax-ymin)/5
  do
    get mouse: x,y,state
    let txt$=using$("###.#### ",x,y)
    plot text, at xm,ym: txt$
  loop
end sub

! main program
  open #1 : screen .05,.9,.05,.9    ! this makes a border
  set mode "egahires"               ! CGA is automatic if no EGA
  set window xmin,xmax,ymin,ymax    ! determines screen coordinates
  set color "red"
  box lines xmin,xmax,ymin,ymax     ! red outline drawn
  set color "white"
  plot xmin,0 ; xmax,0              ! draw axis
  plot 0,ymin ; 0,ymax
  set text justify "left","top"
  plot text, at xmin,0: str$(xmin)  ! label axis
  set text justify "right","top"
  plot text, at xmax,0: str$(xmax)
  set text justify "left","bottom"
  plot text, at 0,ymin: str$(ymin)
  set text justify "left","top"
  plot text, at 0,ymax: str$(ymax)
  set color "green"
  set text justify "center","top"
  if a<>xmin then plot text,at a,0 : str$(a)  ! label a,b
  if b<>xmax then plot text,at b,0 : str$(b)
  call plot                                   ! plot the function
  call mouser
end
