Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. Screen = {}
  2. Screen.__index = Screen
  3.  
  4. local function round(n)
  5. return math.floor(n+0.5)
  6. end
  7.  
  8. function Screen.create(bgColor)
  9. bgColor = bgColor or colors.black
  10. local screen = {}
  11. setmetatable(screen, Screen)
  12.  
  13. screen.width,screen.height = term.getSize()
  14. for y=1,screen.height do
  15. screen[y] = {}
  16. for x=1,screen.width do
  17. screen[y][x] = bgColor
  18. end
  19. end
  20. return screen
  21. end
  22.  
  23. function Screen:drawPixel(x,y,color)
  24. color = color or colors.white
  25. term.setCursorPos(x,y)
  26. term.setBackgroundColor(color)
  27. term.write(' ')
  28. end
  29.  
  30. function Screen:setPixel(x,y,color)
  31. color = color or colors.white
  32. screen[y][x] = color
  33. end
  34.  
  35. function Screen:draw()
  36. for y=1,self.height do
  37. term.setCursorPos(1,y)
  38. for x=1,self.width do
  39. if self[y][x] ~= 0 then
  40. term.setBackgroundColor(self[y][x])
  41. term.write(' ')
  42. else
  43. term.setCursorPos(x,y)
  44. end
  45. end
  46. end
  47. end
  48.  
  49. function Screen:drawRectangle(xi,yi,xf,yf,color)
  50. local hLine = string.rep(' ',math.abs(xf-xi)+1)
  51. local leftX = math.min(xi,xf)
  52. term.setBackgroundColor(color)
  53. term.setCursorPos(leftX,yi)
  54. term.write(hLine)
  55. term.setCursorPos(leftX,yf)
  56. term.write(hLine)
  57. local minY = math.min(yi,yf)
  58. local maxY = math.max(yi,yf)
  59. for y=minY+1,maxY-1 do
  60. term.setCursorPos(xi,y)
  61. term.write(' ')
  62. term.setCursorPos(xf,y)
  63. term.write(' ')
  64. end
  65. end
  66.  
  67. function Screen:setRectangle(xi,yi,xf,yf,color)
  68. local minX = math.min(xi,xf)
  69. local maxX = math.max(xi,xf)
  70. local minY = math.min(yi,yf)
  71. local maxY = math.max(yi,yf)
  72. for x=minX,maxX do
  73. self[yi][x] = color
  74. end
  75. for x=minX,maxX do
  76. self[yf][x] = color
  77. end
  78. for y=minY+1,maxY-1 do
  79. self[y][minX] = color
  80. self[y][maxX] = color
  81. end
  82. end
  83.  
  84. function Screen:clearRectangle(xi,yi,xf,yf)
  85. local minX = math.min(xi,xf)
  86. local maxX = math.max(xi,xf)
  87. local minY = math.min(yi,yf)
  88. local maxY = math.max(yi,yf)
  89. term.setCursorPos(minX,yi)
  90. for x=minX,maxX do
  91. term.setBackgroundColor(self[yi][x])
  92. term.write(' ')
  93. end
  94. term.setCursorPos(minX,yf)
  95. for x=minX,maxX do
  96. term.setBackgroundColor(self[yf][x])
  97. term.write(' ')
  98. end
  99. for y=minY+1,maxY-1 do
  100. self:drawPixel(minX,y,self[y][minX])
  101. self:drawPixel(maxX,y,self[y][maxX])
  102. end
  103. end
  104.  
  105. function Screen:drawCircle(xi,yi,xf,yf,color)
  106. color = color or colors.white
  107. local radius = round(math.sqrt((xf - xi)*(xf - xi) + (yf - yi)*(yf - yi)))
  108.  
  109. for yt=-radius,radius do
  110. for xt=-radius,radius do
  111. if (xi+xt >= 1 and xi+xt <= self.width) and (yi+yt >= 1 and yi+yt <= self.height) then
  112. if (radius == round(math.sqrt(xt*xt + yt*yt))) then
  113. self:drawPixel(xi+xt,yi+yt,color)
  114. end
  115. end
  116. end
  117. end
  118. end
  119.  
  120. function Screen:setCircle(xi,yi,xf,yf,color)
  121. local radius = round(math.sqrt((xf - xi)*(xf - xi) + (yf - yi)*(yf - yi)))
  122.  
  123. for yt=-radius,radius do
  124. for xt=-radius,radius do
  125. if (xi+xt >= 1 and xi+xt <= self.width) and (yi+yt >= 1 and yi+yt <= self.height) then
  126. if (radius == round(math.sqrt(xt*xt + yt*yt))) then
  127. self[yi+yt][xi+xt] = color
  128. end
  129. end
  130. end
  131. end
  132. end
  133.  
  134. function Screen:clearCircle(xi,yi,xf,yf)
  135. local radius = round(math.sqrt((xf - xi)*(xf - xi) + (yf - yi)*(yf - yi)))
  136.  
  137. for yt=-radius,radius do
  138. for xt=-radius,radius do
  139. if (xi+xt >= 1 and xi+xt <= self.width) and (yi+yt >= 1 and yi+yt <= self.height) then
  140. if (radius == round(math.sqrt(xt*xt + yt*yt))) then
  141. self:drawPixel(xi+xt,yi+yt,self[yi+yt][xi+xt])
  142. end
  143. end
  144. end
  145. end
  146. end
  147.  
  148. local function dragFigure(screen, mode, xi, yi, color)
  149. local xf,yf = xi,yi
  150. while true do
  151. local e,p1,p2,p3 = os.pullEvent()
  152. if (e == "mouse_drag") then
  153. if mode == "rectangle" then
  154. screen:clearRectangle(xi,yi,xf,yf)
  155. xf,yf = p2,p3
  156. screen:drawRectangle(xi,yi,xf,yf,color)
  157. elseif mode == "circle" then
  158. screen:clearCircle(xi,yi,xf,yf)
  159. xf,yf = p2,p3
  160. screen:drawCircle(xi,yi,xf,yf,color)
  161. end
  162. elseif ((e == "mouse_click" and p1 == 1) or (e == "key" and p1 == keys.enter)) then
  163. if mode == "rectangle" then
  164. screen:setRectangle(xi,yi,xf,yf,color)
  165. elseif mode == "circle" then
  166. screen:setCircle(xi,yi,xf,yf,color)
  167. end
  168. break
  169. elseif ((e == "mouse_click" and p1 == 2) or (e == "key" and p1 == keys.backspace)) then
  170. if mode == "rectangle" then
  171. screen:clearRectangle(xi,yi,xf,yf)
  172. elseif mode == "circle" then
  173. screen:clearCircle(xi,yi,xf,yf)
  174. end
  175. break
  176. end
  177. end
  178. end
  179.  
  180. local s = Screen.create()
  181. s:draw()
  182. local mode = "circle"
  183. local color = colors.blue
  184.  
  185. while true do
  186. local e,p1,p2,p3 = os.pullEvent('mouse_click')
  187. if (p1 == 1) then
  188. dragFigure(s, "rectangle", p2, p3, colors.red)
  189. elseif (p1 == 2) then
  190. dragFigure(s, "circle", p2, p3, colors.blue)
  191. end
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement