graph.bas with syntax coloring

This HTML file was generated with Kalle's syntaxcolor.py


   1' Draws up to 14 graphs in XY coordinates simultaneously.
   2' By Kalle (http://qalle.net)
   3
   4CONST SCREENWIDTH% = 640
   5CONST SCREENHEIGHT% = 480
   6CONST BGCOLOR% = 0
   7CONST FRAMECOLOR% = 1
   8CONST FIRSTGRAPHCOLOR% = 2
   9
  10' *** start of settings meant to be edited by end user ***
  11
  12' number of graphs to draw (0-14)
  13CONST GRAPHCOUNT% = 14
  14
  15' minimum/maximum X/Y coordinate
  16CONST XMIN = -3.142
  17CONST XMAX = 3.142
  18CONST YMIN = -1
  19CONST YMAX = 1
  20
  21' define colors; format: BBGGRR (&H0 to &H3F3F3F)
  22' "pallette" is an intentional misspelling
  23' first four graph colors should be distinguishable to end user with
  24' protanopia or deuteranopia (tested with iPhone app CVSimulator)
  25DIM pallette&(15)
  26pallette&(BGCOLOR) = &H0  ' black
  27pallette&(FRAMECOLOR) = &H202020  ' gray
  28pallette&(FIRSTGRAPHCOLOR) = &H3F3F ' yellow
  29pallette&(FIRSTGRAPHCOLOR + 1) = &H3F0000 ' blue
  30pallette&(FIRSTGRAPHCOLOR + 2) = &H3F3F3F ' white
  31pallette&(FIRSTGRAPHCOLOR + 3) = &H3F ' red
  32pallette&(FIRSTGRAPHCOLOR + 4) = &H3F00 ' green
  33pallette&(FIRSTGRAPHCOLOR + 5) = &H3F3F00  ' cyan
  34pallette&(FIRSTGRAPHCOLOR + 6) = &H3F003F  ' magenta
  35pallette&(FIRSTGRAPHCOLOR + 7) = &H203F  ' orange
  36pallette&(FIRSTGRAPHCOLOR + 8) = &H3F0020  ' purple
  37pallette&(FIRSTGRAPHCOLOR + 9) = &H3F2000  ' sky blue
  38pallette&(FIRSTGRAPHCOLOR + 10) = &H1020  ' brown
  39pallette&(FIRSTGRAPHCOLOR + 11) = &H2000  ' dark green
  40pallette&(FIRSTGRAPHCOLOR + 12) = &H2020  ' tan
  41pallette&(FIRSTGRAPHCOLOR + 13) = &H202000  ' teal
  42
  43' define formulas for graphs
  44DEF FNy (graph%, x)
  45    SELECT CASE graph%
  46        CASE 0: FNy = SIN(x)
  47        CASE 1: FNy = SIN(x - .1)
  48        CASE 2: FNy = SIN(x - .2)
  49        CASE 3: FNy = SIN(x - .3)
  50        CASE 4: FNy = SIN(x - .4)
  51        CASE 5: FNy = SIN(x - .5)
  52        CASE 6: FNy = SIN(x - .6)
  53        CASE 7: FNy = SIN(x - .7)
  54        CASE 8: FNy = SIN(x - .8)
  55        CASE 9: FNy = SIN(x - .9)
  56        CASE 10: FNy = SIN(x - 1)
  57        CASE 11: FNy = SIN(x - 1.1)
  58        CASE 12: FNy = SIN(x - 1.2)
  59        CASE 13: FNy = SIN(x - 1.3)
  60    END SELECT
  61END DEF
  62
  63' *** end of settings meant to be edited by end user ***
  64
  65IF GRAPHCOUNT < 0 OR GRAPHCOUNT > 14 THEN
  66    PRINT "GRAPHCOUNT must be 0-14."
  67    END
  68END IF
  69
  70IF XMIN >= XMAX THEN
  71    PRINT "XMAX must be greater than XMIN."
  72    END
  73END IF
  74
  75IF YMIN >= YMAX THEN
  76    PRINT "YMAX must be greater than YMIN."
  77    END
  78END IF
  79
  80' 640x480x16 mode
  81SCREEN 12
  82
  83' set palette
  84PALETTE USING pallette&
  85
  86' draw frame
  87LINE (0, 0)-(SCREENWIDTH - 1, SCREENHEIGHT - 1), FRAMECOLOR, B
  88
  89' draw Y axis
  90IF XMIN < 0 AND XMAX > 0 THEN
  91    zerocoordx% = -XMIN * SCREENWIDTH / (-XMIN + XMAX)
  92    LINE (zerocoordx%, 0)-(zerocoordx%, SCREENHEIGHT - 1), FRAMECOLOR
  93END IF
  94
  95' draw X axis
  96IF YMIN < 0 AND YMAX > 0 THEN
  97    zerocoordy% = -YMIN * SCREENHEIGHT / (-YMIN + YMAX)
  98    LINE (0, zerocoordy%)-(SCREENWIDTH - 1, zerocoordy%), FRAMECOLOR
  99END IF
 100
 101xdiff = XMAX - XMIN
 102ydiff = YMAX - YMIN
 103
 104' at each X pixel coordinate, draw each graph as a vertical line to avoid
 105' gaps in steep lines
 106FOR pixelx% = 0 TO SCREENWIDTH - 1
 107    coordxmin = XMIN + (pixelx% - .5) / (SCREENWIDTH - 1) * xdiff
 108    coordxmax = XMIN + (pixelx% + .5) / (SCREENWIDTH - 1) * xdiff
 109
 110    FOR graph% = 0 TO GRAPHCOUNT - 1
 111        coordymin = FNy(graph%, coordxmin)
 112        coordymax = FNy(graph%, coordxmax)
 113
 114        pixelymin% = (1 - (coordymin - YMIN) / ydiff) * (SCREENHEIGHT - 1)
 115        pixelymax% = (1 - (coordymax - YMIN) / ydiff) * (SCREENHEIGHT - 1)
 116
 117        LINE (pixelx%, pixelymin%)-(pixelx%, pixelymax%), 2 + graph%
 118    NEXT
 119NEXT
 120
 121i$ = INPUT$(1)
 122