irnotbeowulf

computercraft sine wave animation

Apr 1st, 2016 (edited)
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Draws an animated sinewave
  2. m = peripheral.wrap("right")
  3. local theta = 0
  4. local yvalues = {}
  5.  
  6. function draw()
  7.    calc()
  8.    render()
  9. end
  10.  
  11. function calc()
  12.    x,y = m.getSize()
  13.    amplitude = math.floor(y/2) -2
  14.    period = x/2
  15.    dx = (math.pi*2) / period
  16.    theta = theta + .02
  17.    
  18.    for i=0, x do
  19.       yvalues[i] = math.sin(theta) * amplitude
  20.       theta = theta + dx
  21.    end
  22.      
  23. end
  24.  
  25. function render()
  26.    m.setBackgroundColor(colors.black)
  27.    m.clear()
  28.    
  29.    for i=1, #yvalues do
  30.       m.setCursorPos(i, (y/2)+ math.floor(yvalues[i]) +1)
  31.       m.blit(" ","0", "0")
  32.      
  33.    end
  34.    
  35. end
  36.  
  37. while true do
  38.    draw()
  39.    sleep(.1)
  40. end
Add Comment
Please, Sign In to add comment