home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / cmplngmg / cl_jun89.arc / AWK210.ARC / GRAPH.AWK < prev    next >
Text File  |  1988-03-10  |  3KB  |  100 lines

  1. # graph - processor for a graph-drawing language
  2. #   input:  data and specification of a graph
  3. #   output: data plotted in specified area
  4.  
  5. BEGIN {                 # set frame dimensions
  6.     ht = 24; wid = 79   # height and width
  7.     ox = 6; oy = 2      # offset for x and y axes
  8.     number = "^[-+]?([0-9]+[.]?[0-9]*|[.][0-9]+)" \
  9.                             "([eE][-+]?[0-9]+)?$"
  10. }
  11. $1 == "label" {                         # for bottom
  12.     sub(/^ *label */, "")
  13.     botlab = $0
  14.     next
  15. }
  16. $1 == "bottom" && $2 == "ticks" {       # ticks for y-axis
  17.     for (i = 3; i <= NF; i++) bticks[++nb] = $i
  18.     next
  19. }
  20. $1 == "left" && $2 == "ticks" {         # ticks for x-axis
  21.     for (i = 3; i <= NF; i++) lticks[++nl] = $i
  22.     next
  23. }
  24. $1 == "range" {                         # xmin ymin xmax ymax
  25.     xmin = $2; ymin = $3; xmax = $4; ymax = $5
  26.     next
  27. }
  28. $1 == "height" { ht = $2; next }
  29. $1 == "width"  { wid = $2; next }
  30. $1 ~ number && $2 ~ number {            # pair of numbers
  31.     nd++;   # count number of data points
  32.     x[nd] = $1; y[nd] = $2
  33.     ch[nd] = $3     # optional plotting character
  34.     next
  35. }
  36. $1 ~ number && $2 !~ number {           # single number
  37.     nd++;   # count number of data points
  38.     x[nd] = nd; y[nd] = $1; ch[nd] = $2
  39.     next
  40. }
  41. END {       # draw graph
  42.     if (xmin == "") {           # no range was given
  43.         xmin = xmax = x[1]      # so compute it
  44.         ymin = ymax = y[1]
  45.         for (i = 2; i <= nd; i++) {
  46.             if (x[1] < xmin) xmin = x[i]
  47.             if (x[1] > xmax) xmax = x[i]
  48.             if (y[1] < ymin) ymin = y[i]
  49.             if (y[1] > ymax) ymax = y[i]
  50.         }
  51.     }
  52.     frame(); ticks(); label(); data(); draw()
  53.     getline <"con"
  54. }
  55. function frame() {          # create frame for graph
  56.     for (i = ox; i < wid; i++) plot(i, oy, "-")         # bottom
  57.     for (i = ox; i < wid; i++) plot(i, ht-1, "-")       # top
  58.     for (i = oy; i < ht; i++) plot(ox, i, "|")          # left
  59.     for (i = oy; i < ht; i++) plot(wid-1, i, "|")       # right
  60. }
  61. function ticks(     i) {    # creadt tick marks for both axes
  62.     for (i = 1; i <= nb; i++) {
  63.         plot(xscale(bticks[i]), oy, "|")
  64.         splot(xscale(bticks[i])-1, 1, bticks[i])
  65.     }
  66.     for (i = 1; i <= nl; i++) {
  67.         plot(ox, yscale(lticks[i]), "-")
  68.         splot(0, yscale(lticks[i]), lticks[i])
  69.     }
  70. }
  71. function label() {          # center label under x-axis
  72.     splot(int((wid + ox - length(botlab))/2), 0, botlab)
  73. }
  74. function data(      i) {    # create data points
  75.     for (i = 1; i <= nd; i++)
  76.         plot(xscale(x[i]), yscale(y[i]), ch[i]=="" ? "*" : ch[i])
  77. }
  78. function draw(      i, j) { # print graph from array
  79.     for (i = ht-1; i >= 0; i--) {
  80.         for (j = 0; j < wid; j++)
  81.             printf((j,i) in array ? array[j,i] : " ")
  82.         printf("\n")
  83.     }
  84. }
  85. function xscale(x) {        # scale x-value
  86.     return int((x-xmin)/(xmax-xmin) * (wid-1-ox) + ox + 0.5)
  87. }
  88. function yscale(y) {        # scale y-value
  89.     return int((y-ymin)/(ymax-ymin) * (ht-1-oy) + oy + 0.5)
  90. }
  91. function plot(x, y, c) {    # put character c in array
  92.     array[x,y] = c
  93. }
  94. function splot(x, y, s,     i, n) { # put string s in array
  95.     n = length(s)
  96.     for (i = 0; i < n; i++)
  97.         array[x+i, y] = substr(s, i+1, 1)
  98. }
  99.  
  100.