This API are usefull for those to love to create great pixel art and complex draw.
Tutorial and functions
Spoiler
Current Functions:Spoiler
This function is delayed by now due to some problems[[setMonitor( appear, side )
Determine the redirect to the monitor:
appear; boolean: if the API use the monitor.
side; side: the side the monitor is.
]]
setChar( char )
Determine what char will be used to the draw of lines, rectangles, point or poligons
char; char: the character to use.
Objects:
-line (x0, y0, x1, y1):creates a line from x0, y0 to x1, y1
Methods of line:
draw(): draw the line in the selected output (console or monitor).
move(x, y): move the line x units to the right and y units to the down (negatives allowed).
length(): returns the length of the line.
tostring(): returns the coordinates converted into a string.
-rectangle(x0, y0, x1, y1): creates a rectangle with x0,y0 and x1, y1 opposed
Methods of rectangle:
draw(): draw the rectangle.
move(x, y): move the rectangle x units to the right and y units to the down (negatives allowed).
area(): returns the area of the rectangle.
-point(x, y, shape, radius): create a point with the coordinates, shape and radius
Methods of point
draw(): draw the point.
move(x, y): move the point x units right and y units down (negatives allowed).
setShape(shape): change the shape of the point(square, cross).
setRadius(radius): change the radius of the point.
-poligon(data): create a poligon with the vertex
Methods of poligon.
draw(): draw the poligon.
(Caution: image is experimental, i dont recommend the use but if you want…)
-image(data): creates a image with data as image.
draw(x, y): draw the image in x and y as coordinates.
Tutorial
Spoiler
A simple code to see some of the features of the current version (0.5):
term.clear()
line = draw.newLine(1,1,7,9) --create a line from 1,1 to 7,9
line:draw() --draw the line into the console
line = line:move(5,3) --move the line 5 to right and 3 to down
line:draw()
sleep(2)
term.clear()
rectangle = draw.newRectangle(1,1,5,6) --create a create a rectangle with 1,1 and 5,3 as opossed vertex
rectangle:draw(true) --draw the rectangle with color
sleep(1)
draw.setChar("H") --select H as the default char
rectangle = draw.newRectangle(1,9,8,14)
rectangle:draw(false) --draw the rectangle (only the edges)
sleep(1)
term.clear()
imagen = { --create an array
" OOOO OOOO ",
" OOOO OOOO ",
" OOOO OOOO ",
" OOOO OOOO ",
" OOOO ",
" OOOO ",
" OOOOOOOOOOOO ",
" OOOOOOOOOOOO ",
" OOOOOOOOOOOO ",
" OOOOOOOOOOOO ",
" OOOO OOOO ",
" OOOO OOOO ",
}
image = draw.newImage(imagen) --create an image with the previous array
image:draw(1,1) --draw the image in the selected coordinates
term.clear()
point1 = draw.newPoint(5,6,"cross",2)
point1:draw()
sleep(1)
term.clear()
point2 = draw.newPoint(12,15,"square",3)
point2:draw()
sleep(1)
term.clear()
point3 = draw.newPoint(12,12,"diamond",3)
point3:draw()
sleep(1)
term.clear()
point4 = draw.newPoint(13,17,"cross2",3)
point4:draw()
sleep(1)
term.clear()
vertices = {
1, 1,
1, 14,
7, 16,
}
poligon = draw.newPoligon( vertices )
poligon:draw()
term.setCursorPos(17,17)
More coming soon… ;)/>/>
V 0.1 (Easy version):
Spoiler
local inc
local incx
local incy
local dist
function getVersion()
return 0.1
end
local function approx(var)
decimal = var-math.floor(var)
entera = math.floor(var)
if decimal>= 0.5 then
return entera+1
else
return entera
end
end
function line(x0, y0, x1, y1)
x0 = tonumber(x0)
y0 = tonumber(y0)
x1 = tonumber(x1)
y1 = tonumber(y1)
if x0==x1 and y0==y1 then
return false
end
if x0==x1 then
if y0>y1 then
inc=-1
else
inc=1
end
for y=y0, y1, inc do
term.setCursorPos(x0, y)
write("O")
end
return true
end
if y0==y1 then
if x0>x1 then
inc=-1
else
inc=1
end
for x=x0, x1, inc do
term.setCursorPos(x, y0)
write("O")
end
return true
end
if x0~=x1 and y0~=y1 then
m=((y1 - y0)/(x1 - x0))
if m<1 and m>-1 then
n=y0-m*x0
if x0>x1 then
inc=-1
else
inc=1
end
for x=x0, x1, inc do
y=m*x+n
y=approx(y)
term.setCursorPos(x, y)
write("O")
end
return true
else
m=(x1-x0)/(y1 - y0)
n=x0-m*y0
if y0>y1 then
inc=-1
else
inc=1
end
for y=y0, y1, inc do
x=m*y+n
x=approx(x)
term.setCursorPos(x, y)
write("O")
end
return true
end
end
end
function rectangle(x0, y0, x1, y1)
x0 = tonumber(x0)
y0 = tonumber(y0)
x1 = tonumber(x1)
y1 = tonumber(y1)
if x0<x1 then
inc=1
else
inc=-1
end
for x=x0, x1, inc do
term.setCursorPos(x, y0)
write("O")
term.setCursorPos(x, y1)
write("O")
end
if y0<y1 then
inc=1
else
inc=-1
end
for y=y0, y1, inc do
term.setCursorPos(x0, y)
write("O")
term.setCursorPos(x1, y)
write("O")
end
end
function rectangleFull(x0, y0, x1, y1)
x0 = tonumber(x0)
y0 = tonumber(y0)
x1 = tonumber(x1)
y1 = tonumber(y1)
if x0<x1 then
incx=1
else
incx=-1
end
if y0<y1 then
incy=1
else
incy=-1
end
for y=y0, y1, incy do
for x=x0, x1, incx do
term.setCursorPos(x, y)
write("O")
end
end
end
function triangle(x0, y0, x1, y1, x2, y2)
x0 = tonumber(x0)
y0 = tonumber(y0)
x1 = tonumber(x1)
y1 = tonumber(y1)
x2 = tonumber(x2)
y2 = tonumber(y2)
drawLine(x0, y0, x1, y1)
drawLine(x1, y1, x2, y2)
drawLine(x2, y2, x0, y0)
end
V 0.3(Object-oriented version)
Spoiler
side = "left"
local monitor = peripheral.wrap( side )
function setMonitor(appear, side)
if appear then
term.redirect( monitor )
else
term.restore()
end
end
local function approx(var)
decimal = var-math.floor(var)
entera = math.floor(var)
if decimal>= 0.5 then
return entera+1
else
return entera
end
end
local line = {
draw = function ( self )
drawline( self )
return true
end,
move = function(self, x, y)
return newLine(
self.x0 + x,
self.y0 + y,
self.x1 + x,
self.y1 + y
)
end,
length = function( self )
return math.sqrt( (self.x1-self.x0)^2 + (self.y1-self.y0)^2 )
end,
tostring = function( self )
return self.x0..", "..self.y0..", "..self.x1..", "..self.y1
end,
}
local lmetatable = {
__index = line,
__draw = line.draw,
__move = line.move,
-- __turn = line.turn,
__length = line.length,
__tostring = line.tostring,
}
function newLine(x0, y0, x1, y1)
local l = {
x0 = x0 or 0,
y0 = y0 or 0,
x1 = x1 or 0,
y1 = y1 or 0
}
setmetatable( l, lmetatable )
return l
end
function drawline( line )
if line.x0==line.x1 and line.y0==line.y1 then
return false
end
if line.x0==line.x1 then
if line.y0>line.y1 then
inc=-1
else
inc=1
end
for y=line.y0, line.y1, inc do
term.setCursorPos(line.x0, y)
write("O")
end
return true
end
if line.y0==line.y1 then
if line.x0>line.x1 then
inc=-1
else
inc=1
end
for x=line.x0, line.x1, inc do
term.setCursorPos(x, line.y0)
write("O")
end
return true
end
if line.x0~=line.x1 and line.y0~=line.y1 then
m=((line.y1 - line.y0)/(line.x1 - line.x0))
if m<1 and m>-1 then
n=line.y0-m*line.x0
if line.x0>line.x1 then
inc=-1
else
inc=1
end
for x=line.x0, line.x1, inc do
y=m*x+n
y=approx(y)
term.setCursorPos(x, y)
write("O")
end
return true
else
m=(line.x1-line.x0)/(line.y1 - line.y0)
n=line.x0-m*line.y0
if line.y0>line.y1 then
inc=-1
else
inc=1
end
for y=line.y0, line.y1, inc do
x=m*y+n
x=approx(x)
term.setCursorPos(x, y)
write("O")
end
return true
end
end
end
local rectangle = {
draw = function ( self, mode)
if mode then
if monitor then
drawrectanglefull( self ,true)
else
drawrectanglefull( self ,false)
end
else
if monitor then
drawrectangle( self ,true)
else
drawrectangle( self ,false)
end
end
return true
end,
move = function(self, x, y)
return newRectangle(
self.x0 + x,
self.y0 + y,
self.x1 + x,
self.y1 + y
)
end,
area = function( self )
return (self.x1-self.x0)^2 + (self.y1-self.y0)^2
end,
}
local rmetatable = {
__index = rectangle,
__draw = rectangle.draw,
__move = rectangle.move,
-- __turn = rectangle.turn,
__area = rectangle.area,
}
function newRectangle(x0, y0, x1, y1)
local r = {
x0 = x0 or 0,
y0 = y0 or 0,
x1 = x1 or 0,
y1 = y1 or 0
}
setmetatable( r, rmetatable )
return r
end
function drawrectangle( rectangle )
if rectangle.x0<rectangle.x1 then
inc=1
else
inc=-1
end
for x=rectangle.x0, rectangle.x1, inc do
term.setCursorPos(x, rectangle.y0)
write("O")
term.setCursorPos(x, rectangle.y1)
write("O")
end
if rectangle.y0<rectangle.y1 then
inc=1
else
inc=-1
end
for y=rectangle.y0, rectangle.y1, inc do
term.setCursorPos(rectangle.x0, y)
write("O")
term.setCursorPos(rectangle.x1, y)
write("O")
end
end
function drawrectanglefull( rectangle )
if rectangle.x0<rectangle.x1 then
incx=1
else
incx=-1
end
if rectangle.y0<rectangle.y1 then
incy=1
else
incy=-1
end
for y=rectangle.y0, rectangle.y1, incy do
for x=rectangle.x0, rectangle.x1, incx do
term.setCursorPos(x, y)
write("O")
end
end
end
local image = {
draw = function (self, x, y )
drawimage( self, x, y )
return true
end,
}
local imetatable = {
__index = image,
__draw = image.draw,
}
function newImage(data)
local i = {
height = #data or 0,
data = data or 0
}
setmetatable( i, imetatable )
return i
end
function drawimage( image, x, y )
y = y - 1
for h=1, image.height, 1 do
y = y + 1
term.setCursorPos(x,y)
write(image.data[h])
end
return true
end
V 0.4
Spoiler
local side = "left"
local character = "O"
local screen = false
local monitor = peripheral.wrap( side )
function setMonitor( appear, side )
screen = appear
side = side
end
local function approx( var )
decimal = var-math.floor(var)
entera = math.floor(var)
if decimal>= 0.5 then
return entera+1
else
return entera
end
end
function setChar( char )
character = char
end
function draw( x, y )
if screen then
term.redirect( monitor )
term.setCursorPos(x,y)
write(character)
term.restore()
else
term.setCursorPos(x,y)
write(character)
end
end
local line = {
draw = function ( self )
drawline( self )
return true
end,
move = function(self, x, y)
return newLine(
self.x0 + x,
self.y0 + y,
self.x1 + x,
self.y1 + y
)
end,
length = function( self )
return math.sqrt( (self.x1-self.x0)^2 + (self.y1-self.y0)^2 )
end,
tostring = function( self )
return self.x0..", "..self.y0..", "..self.x1..", "..self.y1
end,
}
local lmetatable = {
__index = line,
__draw = line.draw,
__move = line.move,
-- __turn = line.turn,
__length = line.length,
__tostring = line.tostring,
}
function newLine( x0, y0, x1, y1 )
local l = {
x0 = x0 or 0,
y0 = y0 or 0,
x1 = x1 or 0,
y1 = y1 or 0
}
setmetatable( l, lmetatable )
return l
end
function drawline( line )
if line.x0==line.x1 and line.y0==line.y1 then
return false
end
if line.x0==line.x1 then
if line.y0>line.y1 then
inc=-1
else
inc=1
end
for y=line.y0, line.y1, inc do
draw(line.x0, y)
end
return true
end
if line.y0==line.y1 then
if line.x0>line.x1 then
inc=-1
else
inc=1
end
for x=line.x0, line.x1, inc do
draw(x, line.y0)
end
return true
end
if line.x0~=line.x1 and line.y0~=line.y1 then
m=((line.y1 - line.y0)/(line.x1 - line.x0))
if m<1 and m>-1 then
n=line.y0-m*line.x0
if line.x0>line.x1 then
inc=-1
else
inc=1
end
for x=line.x0, line.x1, inc do
y=m*x+n
y=approx(y)
draw(x, y)
end
return true
else
m=(line.x1-line.x0)/(line.y1 - line.y0)
n=line.x0-m*line.y0
if line.y0>line.y1 then
inc=-1
else
inc=1
end
for y=line.y0, line.y1, inc do
x=m*y+n
x=approx(x)
draw(x, y)
end
return true
end
end
end
local rectangle = {
draw = function ( self, mode)
if mode then
if monitor then
drawrectanglefull( self ,true)
else
drawrectanglefull( self ,false)
end
else
if monitor then
drawrectangle( self ,true)
else
drawrectangle( self ,false)
end
end
return true
end,
move = function(self, x, y)
return newRectangle(
self.x0 + x,
self.y0 + y,
self.x1 + x,
self.y1 + y
)
end,
area = function( self )
return (self.x1-self.x0)^2 + (self.y1-self.y0)^2
end,
}
local rmetatable = {
__index = rectangle,
__draw = rectangle.draw,
__move = rectangle.move,
-- __turn = rectangle.turn,
__area = rectangle.area,
}
function newRectangle(x0, y0, x1, y1)
local r = {
x0 = x0 or 0,
y0 = y0 or 0,
x1 = x1 or 0,
y1 = y1 or 0
}
setmetatable( r, rmetatable )
return r
end
function drawrectangle( rectangle )
if rectangle.x0<rectangle.x1 then
inc=1
else
inc=-1
end
for x=rectangle.x0, rectangle.x1, inc do
draw(x, rectangle.y0)
draw(x, rectangle.y1)
end
if rectangle.y0<rectangle.y1 then
inc=1
else
inc=-1
end
for y=rectangle.y0, rectangle.y1, inc do
draw(rectangle.x0, y)
draw(rectangle.x1, y)
end
end
function drawrectanglefull( rectangle )
if rectangle.x0<rectangle.x1 then
incx=1
else
incx=-1
end
if rectangle.y0<rectangle.y1 then
incy=1
else
incy=-1
end
for y=rectangle.y0, rectangle.y1, incy do
for x=rectangle.x0, rectangle.x1, incx do
draw(x, y)
end
end
end
local image = {
draw = function (self, x, y )
drawimage( self, x, y )
return true
end,
}
local imetatable = {
__index = image,
__draw = image.draw,
}
function newImage( data )
local i = {
height = #data or 0,
data = data or 0
}
setmetatable( i, imetatable )
return i
end
function drawimage( image, x, y )
if screen then
term.redirect( monitor )
y = y - 1
for h=1, image.height, 1 do
y = y + 1
term.setCursorPos(x,y)
write(image.data[h])
end
term.restore()
else
y = y - 1
for h=1, image.height, 1 do
y = y + 1
term.setCursorPos(x,y)
write(image.data[h])
end
end
return true
end
V 0.5
Spoiler
local character = "O"
local var
local char
local data
local line
local rectangle
local function approx(var)
return math.floor(var + 0.5)
end
function setChar( char )
character = char
end
function draw( x, y )
term.setCursorPos(x,y)
write(character)
end
local point = {
draw = function( self )
drawpoint(self.x, self.y, self.shape, self.radius)
return true
end,
move = function(self, x, y)
return newPoint( self.x+x, self.y+y, self.shape, self.radius )
end,
setShape = function( self, shape1 )
return newPoint(self.x, self.y, shape1, self.radius)
end,
setRadius = function( self, radius)
return newPoint(self.x, self.y, self.shape, radius)
end,
}
local dmetatable ={
__index = point,
__draw = point.draw,
__move = point.move,
__setshape = point.setShape,
__setradius = point.setRadius,
}
function newPoint( x, y, shape, radius )
local d = {
x = x or 0,
y = y or 0,
shape = shape or 0,
radius = radius or 0
}
setmetatable( d, dmetatable )
return d
end
function drawpoint( x, y, shape, radius )
if shape=="cross" then
for i=-radius, radius do
term.setCursorPos(x+i, y)
write(character)
term.setCursorPos(x, y+i)
write(character)
end
end
if shape=="square"then
for incx=-radius, radius do
for incy=-radius, radius do
term.setCursorPos(x+incx, y+incy)
write(character)
end
end
end
end
local line = {
draw = function ( self )
drawline( self.x0,self.y0,self.x1,self.y1 )
return true
end,
move = function(self, x, y)
return newLine(
self.x0 + x,
self.y0 + y,
self.x1 + x,
self.y1 + y
)
end,
length = function( self )
return math.sqrt( (self.x1-self.x0)^2 + (self.y1-self.y0)^2 )
end,
tostring = function( self )
return self.x0..", "..self.y0..", "..self.x1..", "..self.y1
end,
}
local lmetatable = {
__index = line,
__draw = line.draw,
__move = line.move,
-- __turn = line.turn,
__length = line.length,
__tostring = line.tostring,
}
function newLine( x0, y0, x1, y1 )
local l = {
x0 = x0 or 0,
y0 = y0 or 0,
x1 = x1 or 0,
y1 = y1 or 0
}
setmetatable( l, lmetatable )
return l
end
function drawline( x0, y0, x1, y1 )
if x0==x1 and y0==y1 then
return false
end
if x0==x1 then
if y0>y1 then
inc=-1
else
inc=1
end
for y=y0, y1, inc do
draw(x0, y)
end
return true
end
if y0==y1 then
if x0>x1 then
inc=-1
else
inc=1
end
for x=x0, x1, inc do
draw(x, y0)
end
return true
end
if x0~=x1 and y0~=y1 then
m=(y1 - y0)/(x1 - x0)
if m<1 and m>-1 then
n=y0-m*x0
if x0>x1 then
inc=-1
else
inc=1
end
for x=x0, x1, inc do
y=m*x+n
y=approx(y)
draw(x, y)
end
return true
else
m=(x1-x0)/(y1 - y0)
n=x0-m*y0
if y0>y1 then
inc=-1
else
inc=1
end
for y=y0, y1, inc do
x=m*y+n
x=approx(x)
draw(x, y)
end
return true
end
end
end
local rectangle = {
draw = function ( self, mode)
if mode then
if monitor then
drawrectanglefull( self ,true)
else
drawrectanglefull( self ,false)
end
else
if monitor then
drawrectangle( self ,true)
else
drawrectangle( self ,false)
end
end
return true
end,
move = function(self, x, y)
return newRectangle(
self.x0 + x,
self.y0 + y,
self.x1 + x,
self.y1 + y
)
end,
area = function( self )
return (self.x1-self.x0)^2 + (self.y1-self.y0)^2
end,
}
local rmetatable = {
__index = rectangle,
__draw = rectangle.draw,
__move = rectangle.move,
-- __turn = rectangle.turn,
__area = rectangle.area,
}
function newRectangle(x0, y0, x1, y1)
local r = {
x0 = x0 or 0,
y0 = y0 or 0,
x1 = x1 or 0,
y1 = y1 or 0
}
setmetatable( r, rmetatable )
return r
end
function drawrectangle( rectangle )
if rectangle.x0<rectangle.x1 then
inc=1
else
inc=-1
end
for x=rectangle.x0, rectangle.x1, inc do
draw(x, rectangle.y0)
draw(x, rectangle.y1)
end
if rectangle.y0<rectangle.y1 then
inc=1
else
inc=-1
end
for y=rectangle.y0, rectangle.y1, inc do
draw(rectangle.x0, y)
draw(rectangle.x1, y)
end
end
function drawrectanglefull( rectangle )
if rectangle.x0<rectangle.x1 then
incx=1
else
incx=-1
end
if rectangle.y0<rectangle.y1 then
incy=1
else
incy=-1
end
for y=rectangle.y0, rectangle.y1, incy do
for x=rectangle.x0, rectangle.x1, incx do
draw(x, y)
end
end
end
local poligon = {
draw = function( self )
drawPoligon( self )
return true
end
}
local pmetatable = {
__index = poligon,
__draw = poligon.draw,
}
function newPoligon( vertices )
local p = {
vertex = vertices
}
setmetatable( p, pmetatable)
return p
end
function drawPoligon( poligon )
i=-1
while true do
i=i+2
if poligon.vertex[i+2]==nil or poligon.vertex[i+3]==nil then
drawline( poligon.vertex[i], poligon.vertex[i+1], poligon.vertex[1], poligon.vertex[2] )
break
else
drawline( poligon.vertex[i], poligon.vertex[i+1], poligon.vertex[i+2], poligon.vertex[i+3] )
end
end
end
local image = {
draw = function (self, x, y )
drawimage( self, x, y )
return true
end,
}
local imetatable = {
__index = image,
__draw = image.draw,
}
function newImage( data )
local i = {
height = #data or 0,
data = data or 0
}
setmetatable( i, imetatable )
return i
end
function drawimage( image, x, y )
y = y - 1
for h=1, image.height, 1 do
y = y + 1
term.setCursorPos(x,y)
write(image.data[h])
end
return true
end
Work to do:
-More Objects(circles…)
-Make the code more easy-to-read(comment the code, improve some variables, etc)
-And the best of all: 3D support (by now it only can draw a cube in oblique projection, but this will be better)
-Upload the code to github or other server instead of copy the code into the post(edit this is a huge pain)
-Custom colors(with custom fonts like in MiM)
-Any idea you can say.
(Thank you to mrgreaper for the usefull (and a little obvious :mellow:/>/> ) advice)