Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This program is meant to give a variety of options to illustrate using the computercraft screen.
- function wrapMon() --if there is a monitor, wraps it and returns true, otherwise returns the termAPI and returns false
- if peripheral.find('monitor') then
- monitor = peripheral.find('monitor')
- isMonitor = true
- else
- monitor = _G.term
- isMonitor = false
- end
- return monitor, isMonitor
- end
- function middle() --returns the middle of the screen in coordinates x, y.
- width, height = monitor.getSize()
- x = math.floor(width/2)
- y = math.floor(height/2)
- return x, y
- end
- function drawBox(x1, y1, x2, y2, color, char) -- draws a box given the coordinates of two points
- local colorTable = {}
- local xinc, yinc = 1, 1
- if x1 < x2 then
- xinc = 1
- else
- xinc = -1
- end
- if y1 < y2 then
- yinc = 1
- else
- yinc = -1
- end
- for i = x1, x2, xinc do
- colorTable[i] = {}
- for k = y1, y2, yinc do
- colorTable[i][k] = color
- drawDot(i, k, color, char)
- end
- end
- return colorTable
- end
- function drawDot(x, y, color, char)
- if not char then
- char = ' '
- end
- monitor.setCursorPos(x, y)
- monitor.setBackgroundColor(color)
- monitor.write(char)
- monitor.setBackgroundColor(colors.black)
- return x, y, color, char
- end
- function drawLine(x1, y1, x2, y2, color) --returns table with x and y position of each pixel with the color, ie table[x][y]
- local colorTable={}
- local x = x2 - x1
- local y = y2 - y1
- if x < 0 then
- xinc = -1
- else
- xinc = 1
- end
- if y < 0 then
- yinc = -1
- else
- yinc = 1
- end
- 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.
- for i = x1, x2, xinc do
- colorTable[i] = {}
- end
- for i = y1, y2, yinc do
- local xpos = math.floor((x1 + (x/y)*(i-y1)+0.5))
- drawDot(xpos, i, color)
- colorTable[xpos][i] = color
- end
- elseif math.abs(y/x)<1 then --Works in a similr principle except forces one pixel per column
- for i = x1, x2, xinc do
- local ypos = math.floor((y1+(y/x)*(i-x1))+0.5)
- drawDot(i, ypos, color)
- colorTable[i] = {}
- colorTable[i][ypos] = color
- end
- else
- for i = x1, x2, xinc do --in the case the line goes down 45 degrees the math is much simpler
- local ypos = y1+(y/x)*(i-x1)
- drawDot(i, ypos, color)
- colorTable[i] = {}
- colorTable[i][ypos] = color
- end
- end
- return colorTable
- end
Add Comment
Please, Sign In to add comment