SHOW:
|
|
- or go back to the newest paste.
| 1 | - | function wrapMon() |
| 1 | + | -- This program is meant to give a variety of options to illustrate using the computercraft screen. |
| 2 | - | for i, v in ipairs(peripheral.getNames()) do |
| 2 | + | |
| 3 | - | if peripheral.getType(v) == 'monitor' then |
| 3 | + | |
| 4 | - | monitor = peripheral.wrap(v) |
| 4 | + | |
| 5 | - | return monitor |
| 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 | - | wrapMon() |
| 10 | + | isMonitor = true |
| 11 | else | |
| 12 | monitor = _G.term | |
| 13 | - | function middle() |
| 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 | - | function draw(color) |
| 20 | + | |
| 21 | y = math.floor(height/2) | |
| 22 | - | monitor.write(' ')
|
| 22 | + | |
| 23 | end | |
| 24 | ||
| 25 | function drawBox(x1, y1, x2, y2, color, char) -- draws a box given the coordinates of two points | |
| 26 | - | function drawDot(x, y, color) |
| 26 | + | local colorTable = {}
|
| 27 | local xinc, yinc = 1, 1 | |
| 28 | - | draw(color) |
| 28 | + | if x1 < x2 then |
| 29 | xinc = 1 | |
| 30 | else | |
| 31 | - | function drawLine(x1, y1, x2, y2, color) |
| 31 | + | |
| 32 | - | x = x2 - x1 |
| 32 | + | |
| 33 | - | y = y2 - y1 |
| 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 -- if x1 > x2, |
| 43 | + | |
| 44 | - | if math.abs(y/x)>1 then --if the gradient is more than 1 |
| 44 | + | |
| 45 | return colorTable | |
| 46 | - | drawDot(math.floor((x1 + (x/y)*(i-y1)+0.5)), i, color) |
| 46 | + | |
| 47 | ||
| 48 | - | elseif math.abs(y/x)<1 then --if the gradient is less than 1 |
| 48 | + | function drawDot(x, y, color, char) |
| 49 | - | for i = x1, x2, xinc do --go along |
| 49 | + | if not char then |
| 50 | - | drawDot(i, math.floor((y1+(y/x)*(i-x1))+0.5), color) |
| 50 | + | char = ' ' |
| 51 | end | |
| 52 | monitor.setCursorPos(x, y) | |
| 53 | monitor.setBackgroundColor(color) | |
| 54 | - | drawDot(i, y1+(i-x1), 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 |