Advertisement
1980geeksquad

Min/Max data visualization

Mar 14th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. function setup()
  2.  
  3.     displayMode(FULLSCREEN)
  4.  
  5.     --creating random table
  6.     --you can also set custome table with tbl={num1,num2...}
  7.     tbl={}
  8.     for i=0,25 do
  9.         table.insert(tbl,math.random(0,100))
  10.     end
  11.    
  12.     --setting temp var for comparison
  13.     --set to the first value in the table
  14.     max=tbl[1]
  15.     min=tbl[1]
  16.    
  17.     --initializing line position
  18.     lmn=1
  19.     lmx=1
  20.    
  21.     --finds max value
  22.     --compares every instance to max
  23.     --if it is bigger, change var max
  24.     for i=2, #tbl do
  25.         if tbl[i]>max then
  26.             max=tbl[i]
  27.             lmx=i
  28.         end
  29.     end
  30.  
  31.     --finds min value
  32.     for i=2, #tbl do
  33.         if tbl[i]<min then
  34.             min=tbl[i]
  35.             lmn=i
  36.         end
  37.     end
  38. end
  39.    
  40. function draw()
  41.     --setting draw values
  42.     background(0, 0, 0, 255)
  43.     textAlign(CENTER)
  44.     strokeWidth(2)
  45.    
  46.     --draw the table centered
  47.     for i=1, #tbl do
  48.         fill(255,255,255,tbl[i]*2)
  49.         rect(WIDTH/2-(#tbl*30)/2+i*30-15,HEIGHT/2-tbl[i],30,tbl[i]*2)
  50.         fill(255,255,255,255)
  51.         text(tbl[i],WIDTH/2-(#tbl*30)/2+i*30,HEIGHT/2)
  52.     end
  53.    
  54.     --draw min/max values and lines
  55.     fill(255,255,255,255)
  56.     stroke(255,255,255,255)
  57.    
  58.     text(max,WIDTH/2,HEIGHT/2+150)
  59.     line(WIDTH/2-(#tbl*30)/2+lmx*30,HEIGHT/2+max,WIDTH/2,HEIGHT/2+140)
  60.    
  61.     text(min,WIDTH/2,HEIGHT/2-150)
  62.     line(WIDTH/2-(#tbl*30)/2+lmn*30,HEIGHT/2-10-min,WIDTH/2,HEIGHT/2-140)
  63.    
  64.    
  65.    
  66.     fill(255, 255, 255, 255)
  67.     text("Touch to run again",WIDTH/2,HEIGHT-20)
  68. end
  69.  
  70.  
  71. function touched(touch)
  72.     --touch to run again
  73.     if touch.state==BEGAN then
  74.         setup()
  75.     end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement