Advertisement
TeoremaPi

Plot (pintar gráficas)

Jun 25th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. -- https://www.youtube.com/watch?v=kdyehkBjLJg
  2. monitor = peripheral.wrap("right")
  3.  
  4.  
  5. -- VARIABLE --
  6. -- limites de la gráfica
  7. xMin = -2*math.pi
  8. xMax =  2*math.pi
  9.  
  10. yMax = 1.5
  11. yMin = -1.5
  12.  
  13.  
  14. -- coloreamos el fondo
  15. monitor.setBackgroundColor(colors.black)
  16.  
  17. -- aumentamos la resolucion
  18. monitor.setTextScale(0.5)
  19.  
  20. -- borramos todo
  21. monitor.clear()
  22.  
  23. -- obtenemos las dimensiones del monitor
  24. sxMax, syMax = monitor.getSize()
  25.  
  26. -- guardamos la configuracion del ordenador
  27. computer = term.current()
  28.  
  29. -- pasamos a trabajar en el monitor
  30. term.redirect( monitor )
  31.  
  32. -- pintamos los ejes
  33. -- eje x
  34. y0 = (1-syMax)*(0-yMax)/(yMax-yMin) + 1
  35. y0 = math.floor(y0+0.5)
  36. if y0 >= 1 and y0 <= syMax then
  37.  paintutils.drawLine(1, y0, sxMax, y0, colors.gray)
  38. end
  39. -- eje y
  40. x0 = (sxMax-1)*(0-xMin)/(xMax-xMin) + 1
  41. x0 = math.floor(x0+0.5)
  42. if x0 >= 1 and x0 <= sxMax then
  43.  paintutils.drawLine(x0, 1, x0, syMax, colors.gray)
  44. end
  45.  
  46. -- pintamos todas las gráficas
  47. for j = 1, 3 do
  48.  
  49.  yScreenm1 = 1/0
  50.  
  51.  -- pintamos puntos para todos los valores de x
  52.  for k = 1, sxMax do
  53.   -- guardamos la actual posicion de x en la pantalla
  54.   xScreen = k
  55.   -- calculamos el valor de x en la funcion
  56.   x = (xMax-xMin)*(xScreen-1)/(sxMax-1) + xMin
  57.  
  58.  
  59.   -- VARIABLE --
  60.   if j == 1 then
  61.   -- calculamos el valor de y para la x actual
  62.    y = math.sin(x)
  63.    color = colors.orange
  64.   elseif j == 2 then
  65.    y = math.sin(x - math.pi*2/3)
  66.    color = colors.green
  67.   elseif j == 3 then
  68.    y = math.sin(x + math.pi*2/3)
  69.    color = colors.blue
  70.   end
  71.  
  72.  
  73.   -- calculamos la posicion en y del punto actual
  74.   yScreen = (1-syMax)*(y-yMax)/(yMax-yMin) + 1
  75.  
  76.   -- ajustamos la posicion de y para que sea un entero
  77.   yScreen = math.floor(yScreen+0.5)
  78.  
  79.   -- si el punto esta en la pantalla, lo pintamos
  80.   if math.abs(yScreen)~=math.huge and yScreen >= 1 and yScreen <= syMax then
  81.    paintutils.drawPixel(xScreen, yScreen, color)
  82.   end
  83.  
  84.   -- a continuacion llenamos los huecos para que no queden piexeles sueltos
  85.   -- pintamos en la columna anterior todos los pixeles hasta uno antes del nuevo
  86.   -- comprobamos que ningun valor sea infinito y que no pase de inf a -inf
  87.   if math.abs(yScreenm1)~=math.huge and math.abs(yScreen)~=math.hugeand and
  88.   not ((yScreenm1 < 1 and yScreen > syMax) or (yScreenm1 > syMax and yScreen < 1)) then
  89.  
  90.    -- si la grafica es creciente
  91.    if yScreen > yScreenm1 + 1 then
  92.     for m = yScreenm1 + 1, yScreen - 1 do
  93.      paintutils.drawPixel(xScreen - 1, m, color)
  94.     end
  95.  
  96.     -- si la grafica es decreciente
  97.    elseif yScreen < yScreenm1 - 1 then
  98.     for m = yScreen + 1, yScreenm1 - 1 do
  99.      paintutils.drawPixel(xScreen - 1, m, color)
  100.     end
  101.    end
  102.   end
  103.  
  104.   yScreenm1 = yScreen
  105.  
  106.  end
  107. end
  108.  
  109. -- regresamos al ordenador
  110. term.redirect( computer )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement