levshx

graph.lua

Mar 7th, 2022 (edited)
1,404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.73 KB | None | 0 0
  1. -- lib by levshx
  2. local graph = {}
  3. local component = require("component")
  4. local computer=require("computer")
  5. local serial = require("serialization")
  6. local unicode = require("unicode")
  7. local gpu = component.gpu
  8.  
  9. function sign(x)
  10.     if x<0 then
  11.       return -1
  12.     elseif x>0 then
  13.       return 1
  14.     else
  15.       return 0
  16.     end
  17.  end
  18.  
  19. function graph.setColorMinecraft(index) --Список цветов
  20.     if (index ~= "r") then back = gpu.getForeground() end
  21.     if (index == "0") then gpu.setForeground(0x333333) end
  22.     if (index == "1") then gpu.setForeground(0x0000ff) end
  23.     if (index == "2") then gpu.setForeground(0x00ff00) end
  24.     if (index == "3") then gpu.setForeground(0x24b3a7) end
  25.     if (index == "4") then gpu.setForeground(0xff0000) end
  26.     if (index == "5") then gpu.setForeground(0x8b00ff) end
  27.     if (index == "6") then gpu.setForeground(0xffa500) end
  28.     if (index == "7") then gpu.setForeground(0xbbbbbb) end
  29.     if (index == "8") then gpu.setForeground(0x808080) end
  30.     if (index == "9") then gpu.setForeground(0x0000ff) end
  31.     if (index == "a") then gpu.setForeground(0x66ff66) end
  32.     if (index == "b") then gpu.setForeground(0x00ffff) end
  33.     if (index == "c") then gpu.setForeground(0xff6347) end
  34.     if (index == "d") then gpu.setForeground(0xff00ff) end
  35.     if (index == "e") then gpu.setForeground(0xffff00) end
  36.     if (index == "f") then gpu.setForeground(0xffffff) end
  37.     if (index == "g") then gpu.setForeground(0x00ff00) end
  38.     if (index == "r") then gpu.setForeground(back) end
  39.   end
  40.  
  41. function graph.text(x,y,text)
  42.     y = y + 1
  43.     for i = 1, unicode.len(text) do
  44.         gpu.set(x+i,y, unicode.sub(text, i,i))
  45.     end
  46. end
  47.  
  48. function graph.colorText(x,y,text)
  49.     -- Example:
  50.     -- graph.colorText(0,0,"&4 RED &2 GREEN &3 BLUE &6 ORANGE")
  51.     y = y + 1
  52.     local n = 1
  53.     for i = 1, unicode.len(text) do
  54.         if unicode.sub(text, i,i) == "&" then
  55.             graph.setColorMinecraft(unicode.sub(text, i + 1, i + 1))
  56.         elseif unicode.sub(text, i - 1, i - 1) ~= "&" then
  57.             gpu.set(x+n,y, unicode.sub(text, i,i))
  58.             n = n + 1
  59.         end
  60.     end
  61. end
  62.  
  63. function graph.box(x, y, width, height, symbol)
  64.     if not symbol then
  65.         symbol = unicode.char(0x2588)  -- FULL BLOCK SYMBOL
  66.     end
  67.     gpu.fill(x, y, 1, height, symbol)
  68.     gpu.fill(x, y, width, 1, symbol)
  69.     gpu.fill(x+width-1, y, 1, height, symbol)
  70.     gpu.fill(x, y+height-1, width, 1, symbol)
  71. end
  72.  
  73. function graph.circle(X1, Y1, R, symbol)
  74.     X1 = math.ceil( X1 / 2 )
  75.     if not symbol then
  76.         symbol = unicode.char(0x2588)..unicode.char(0x2588)  -- FULL BLOCK SYMBOL
  77.     end    
  78.     -- R - радиус, X, Y - координаты центра
  79.     local x = 0
  80.     local y = R
  81.     local delta = 1 - (2 * R)
  82.     local errror = 0
  83.     while (y >= x) do
  84.         gpu.set((X1 + x)*2, Y1 + y, symbol)
  85.         gpu.set((X1 + x)*2, Y1 - y, symbol)
  86.         gpu.set((X1 - x)*2, Y1 + y, symbol)
  87.         gpu.set((X1 - x)*2, Y1 - y, symbol)
  88.         gpu.set((X1 + y)*2, Y1 + x, symbol)
  89.         gpu.set((X1 + y)*2, Y1 - x, symbol)
  90.         gpu.set((X1 - y)*2, Y1 + x, symbol)
  91.         gpu.set((X1 - y)*2, Y1 - x, symbol)
  92.         error = 2 * (delta + y) - 1
  93.         if ((delta < 0) and (errror <= 0)) then
  94.             delta = delta +( 2 * (x+1) + 1)
  95.             x = x + 1
  96.             goto continue
  97.         end    
  98.         if ((delta > 0) and (errror > 0)) then
  99.             delta =delta-(2 * (y-1) + 1)
  100.             y = y - 1
  101.             goto continue
  102.         end    
  103.         delta = delta + (2 * ((x+1)) - (y-1))
  104.         x = x +1
  105.         y = y-1
  106.         ::continue::
  107.     end
  108. end
  109.  
  110. function graph.point(x, y, symbol)
  111.     x = x + 1
  112.     y = y + 1
  113.     if not symbol then
  114.         symbol = unicode.char(0x2588)  -- FULL BLOCK SYMBOL
  115.     end  
  116.     gpu.set(x, y, symbol)
  117. end
  118.  
  119.  
  120. function graph.line(x1, y1, x2, y2, symbol)
  121.     if (y1>y2) then
  122.         local xx = x1
  123.         local yy = y1
  124.         x1 = x2
  125.         y1 = y2
  126.         x2 = xx
  127.         y2 = yy
  128.     end
  129.     if (x1>x2) then
  130.         local xx = x1
  131.         local yy = y1
  132.         x1 = x2
  133.         y1 = y2
  134.         x2 = xx
  135.         y2 = yy
  136.     end
  137.     local dx, dy = math.abs(x1-x2), math.abs(y1-y2)
  138.     local symbol = value or unicode.char(0x2588)
  139.     if dx > 0 or dy > 0 then
  140.         if dx > dy then
  141.             local y = y1
  142.             for x = x1, x2, sign(x2-x1) do
  143.                 graph.point( x, math.floor(y+ 0.5), symbol)
  144.                 y = y + (dy / dx) * sign(y2-y1)
  145.             end
  146.         else
  147.             local x = x1
  148.             for y = y1, y2, sign(y2-y1) do
  149.                 graph.point(math.floor(x + 0.5), y, symbol)
  150.                 x = x + (dx / dy) * sign(x2-x1)
  151.             end
  152.         end
  153.     end
  154. end
  155.  
  156.  
  157.  
  158. return graph
Add Comment
Please, Sign In to add comment