;; Draw a function of form y = f(x) where all arguments are from the command
;; line in the following format:
;; function, startx, endx, scalex, scaley
;; function is in the format  "f(#x)"  ex:  "#x^2-4*#x" for x^2-4x

;; Version 2.0 By Brian J. Bartlett (CIS 72037,570)

;; try running it like this:          scripit scriptname "func" 0 20 1 1
;; or, to use the default formula:    scripit scriptname "" 0 20 1 1
;; To see a debug of the process try: 
;;                    scripit >ram:dump -d scriptname "" 0 20 1 1
;; then have a look at ram:dump.
;;
;;      USE CONTROL-LEFT MOUSE BUTTON TO BREAK OUT!!!
;;       unless you like waiting for 10 minutes for the script to end

begin

;;Con Open "ram:dump"	;Debugging option
;;Verbose Full

if #argcount  < 5             ;Did user supply enough args?
    GoSub Complain            ;Complain bitterly (Show usuage and quit)
    end
endif

if $arg[1]                    ;Did user supply a forumla?
    Let $Funct  = $arg[1]     ;Yes, copy it into $Funct
else
    Let $Funct  = "(#x^2-8*#x)"   ;No, copy default formula into $Funct
endif

#startx = $arg[2]             ;Get all user args.
#endx   = $arg[3]
#scalex = $arg[4]
#scaley = $arg[5]

;; Now that the assigns are done, get a window
#width  = #Scr.width
If #width < 44
    Con Echo "Screen too slim"
    End
Endif

#height = #Scr.height
If #height < 44
    Con Echo "Screen to short"
    End
Endif

$Con   ?= "CON:0/0/" #width "/" #height "/Plot" 
Con Open $Con

S Window Plot                   ;Select plotting window
W Front                         ;Bring it to the front

#middlex = #width/2             ;Calculate middle of screen width
#middley = #height/2            ;Calculate middle of screen height
#bottom  = #height - 11         ;Calculate right edge
#right   = #width - 11          ;Calculate bottom edge

Gfx Clear                       ;Clear the borders
Gfx Savemode 0                  ;Save previous modes
Gfx Pen 1,0,JAM1                ;Set pen for drawing Axes
Gfx Box #middlex,11,#middlex,#bottom    ;Draw Axes
Gfx Box 11,#middley,#right,#middley     ;Draw Axes
Gfx move 10,10                  ;Move pen
Gfx text "Scripit Plotter"      ;Label the Window
Gfx Pen 3,1,JAM1                ;Set pen for plotting function

;;Now start the loop, plotting points as we find them
#x = #startx                    ;Set starting point
While #x <= #endx               ;Loop until done
    Let #pointx = ((#x/#scalex)+(#Width/2))     ;Scale the drawing point
    If #pointx < 10             ;Make sure it's not off the left edge
        Goto skip               ;If it is, skip it
    endif
    If #pointx > (#Width-10)    ;Make sure it's not off the right edge
        Goto skip               ;If it is, skip it
    endif
    Let #y = $Funct             ;Calculate y co-ordinate
    Let #pointy = ((#Height/2)-(#y/#scaley))    ;Scale it
    If #pointy < 10             ;Make sure it's not off the top edge
       Goto skip                ;If it is, skip it
    endif
    If #pointy > (#Height-10)   ;Make sure it's not off the bottom edge
        Goto skip               ;If it is, skip it
    endif
    Gfx Pixel #pointx, #pointy  ;Now plot it on the provided window
Skip:                  
    #x = #x+1                   ;Increment x for next point
EndWhile

Finish:
Gfx RestoreMode 0               ;Restore Gfx to previous modes
Wait 30000                      ;Wait for 10 minutes to show off the graph
End

Label Complain
con cls                       ;show usage message
con style 33;1
con echo "Plot.  A Scripit script.\n"
con style 0
con echo "Draw a function of form y = f(x) where all arguments are from the command"
con echo "line in the following format:"
con echo "function, startx, endx, scalex, scaley"
con echo "function is in the format  f(#x)  ex:  #x^2-4*#x for x^2-4x\n"
con style 2
con echo "By Brian J. Bartlett (CIS 72037,570)" " "
con style 0
;; In the following lines we're using ~ instead of " since we need to actually
;; print the double quote character.
con text ~Try running it like this:\n\tScripit ~ $scriptname ~ "func" 0 20 1 1\n\n~
con text ~Or, to use the default formula:\n\tScripit ~ $scriptname ~ "" 0 20 1 1\n\n~
con text ~To see a debug of the process try:\n\tScripit >ram:dump -d ~ $scriptname ~ "" 0 20 1 1\n~
con echo "\tthen have a look at ram:dump.\n"
con echo "\tUSE CONTROL-LEFT MOUSE BUTTON TO BREAK OUT!!!"
con echo "\tunless you like waiting for 10 minutes for the script to end.\n"
Return
