_Jacques

screenAPI.lua

Jul 7th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- This program is meant to give a variety of options to illustrate using the computercraft screen.
  2.  
  3.  
  4.  
  5.  
  6.  
  7. function wrapMon() --if there is a monitor, wraps it and returns true, otherwise returns the termAPI and returns false
  8.     if peripheral.find('monitor') then
  9.         monitor = peripheral.find('monitor')
  10.         isMonitor = true
  11.     else
  12.         monitor = _G.term
  13.         isMonitor = false
  14.     end
  15.     return monitor, isMonitor
  16. end  
  17.  
  18. function middle() --returns the middle of the screen in coordinates x, y.
  19.     width, height = monitor.getSize()
  20.     x = math.floor(width/2)
  21.     y = math.floor(height/2)
  22.     return x, y
  23. end
  24.  
  25. function drawBox(x1, y1, x2, y2, color, char) -- draws a box given the coordinates of two points
  26.     local colorTable = {}
  27.     local xinc, yinc = 1, 1
  28.     if x1 < x2 then
  29.         xinc = 1
  30.     else
  31.         xinc = -1
  32.     end
  33.     if y1 < y2 then
  34.         yinc = 1
  35.     else
  36.         yinc = -1
  37.     end
  38.     for i = x1, x2, xinc do
  39.         colorTable[i] = {}
  40.         for k = y1, y2, yinc do
  41.             colorTable[i][k] = color
  42.             drawDot(i, k, color, char)
  43.         end
  44.     end
  45.     return colorTable
  46. end
  47.  
  48. function drawDot(x, y, color, char)
  49.     if not char then
  50.         char = ' '
  51.     end
  52.     monitor.setCursorPos(x, y)
  53.     monitor.setBackgroundColor(color)
  54.     monitor.write(char)
  55.     monitor.setBackgroundColor(colors.black)
  56.     return x, y, color, char
  57. end
  58.  
  59. function drawLine(x1, y1, x2, y2, color) --returns table with x and y position of each pixel with the color, ie table[x][y]
  60.     local colorTable={}
  61.     local x = x2 - x1
  62.     local y = y2 - y1
  63.     if x < 0 then
  64.         xinc = -1
  65.     else
  66.         xinc = 1
  67.     end
  68.     if y < 0 then
  69.         yinc = -1
  70.     else
  71.         yinc = 1
  72.     end
  73.     if math.abs(y/x)>1 then --if the gradient is more than 1, goes through the y values and finds the corresponding x value through a mathematical line approximation equation. This avoids making thick lines by forcing only one pixel per row, essentially giving 1 pixel thick lines.
  74.         for i = x1, x2, xinc do
  75.             colorTable[i] = {}
  76.         end
  77.         for i = y1, y2, yinc do
  78.             local xpos = math.floor((x1 + (x/y)*(i-y1)+0.5))
  79.             drawDot(xpos, i, color)
  80.             colorTable[xpos][i] = color
  81.         end
  82.     elseif math.abs(y/x)<1 then --Works in a similr principle except forces one pixel per column
  83.         for i = x1, x2, xinc do
  84.             local ypos = math.floor((y1+(y/x)*(i-x1))+0.5)
  85.             drawDot(i, ypos, color)
  86.             colorTable[i] = {}
  87.             colorTable[i][ypos] = color
  88.         end
  89.     else
  90.         for i = x1, x2, xinc do --in the case the line goes down 45 degrees the math is much simpler
  91.             local ypos = y1+(y/x)*(i-x1)
  92.             drawDot(i, ypos, color)
  93.             colorTable[i] = {}
  94.             colorTable[i][ypos] = color
  95.         end
  96.     end
  97.     return colorTable
  98. end
Add Comment
Please, Sign In to add comment