tommy2805

test disegno di un grafico

Jan 3rd, 2022 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.79 KB | None | 0 0
  1. local history = {}
  2.  
  3. local function calculateHeight(maxHeight, value, maxValue)
  4.   return math.floor(maxHeight * (value / maxValue) + 0.5)
  5. end
  6.  
  7. local function drawGraph(history, xPos, yPos, length, height, maxValue)
  8.   local len = #history
  9.   for x = length, 1, -1 do
  10.     local i = length - x + 1
  11.     for y = 1, calculateHeight(height, history[i], maxValue) do
  12.       -- draw a pixel at that position.
  13.       term.setCursorPos(xPos + x,  yPos + y)
  14.       term.write(' ')
  15.     end
  16.   end
  17. end
  18.  
  19. local function addHistory(value)
  20.   table.insert(history, value)
  21.   if #history > 50 then
  22.     table.remove(history, 1)
  23.   end
  24. end
  25.  
  26.  
  27. addHistory(83)
  28. addHistory(29)
  29. addHistory(37)
  30. addHistory(99)
  31. addHistory(75)
  32. addHistory(16)
  33. addHistory(1)
  34. addHistory(5)
  35.  
  36. drawGraph(history, 3, 15, 9, 12, 100)
Add Comment
Please, Sign In to add comment