This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
wynro's profile picture

Draw API [1.3]

Started by wynro, 13 March 2012 - 10:20 AM
wynro #1
Posted 13 March 2012 - 11:20 AM
Well as my first experiment in LUA I had created a little API to use.

This API are usefull for those to love to create great pixel art and complex draw.

Tutorial and functions
SpoilerCurrent Functions:
SpoilerThis 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
SpoilerA 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)
mrgreaper #2
Posted 15 March 2012 - 03:30 AM
P.D: Oh C'mon! More than 200 views and any single reply?

you havent explained how to use the api

give an example, show us the codes we can use , i scrolled through it was about to hit back then i saw your comment…well if you want more replies tell us how to use it :D/>/>
princepsp #3
Posted 16 March 2012 - 12:19 PM
it gives error where i will write this?
wynro #4
Posted 16 March 2012 - 03:05 PM
it gives error where i will write this?

I dont understand. If you dont say the error I cant help you.
Ray1235 #5
Posted 16 March 2012 - 05:42 PM
got an error when i made array. It wants me to end array at 2nd line.
I need help because i am making nazi zombies map.
Code:

local side = "right"
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
local tonext = 60
local wave = 1
local i = 1
wave1 = {													   
"|"
"|"
"|"
"|"
"|"
}
wave2 = {													
"| |"
"| |"
"| |"
"| |"
"| |"
}
wave3 = {													
"| | |"
"| | |"
"| | |"
"| | |"
"| | |"
}
wave3 = {											 
"| | |"
"| | |"
"| | |"
"| | |"
"| | |"
}
wave4 = {													
"| | | |"
"| | | |"
"| | | |"
"| | | |"
"| | | |"
}
wave5 = {												  
"| | | |"
"| | | |"
"+-+-+-+"
"| | | |"
"| | | |"
}
term.setCursorPos(9,1)
print("--	    SYSTEM ERROR	    --")
print("--   Power level is critical  --")
print("-- Shutting down Pack-a-punch --")
print("We'll need to turn it on somehow...")
for i=99,1,-1 do
term.clear()
term.setCursorPos(9,9)
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()
draw.setMonitor(true, "right")			  --select the monitor in the left side
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()
draw.setMonitor(false)						    --deselect the monitor
term.clear()
if wave = 1 do
image = draw.newImage(wave1)				   --create an image with the previous array
image:draw(9,9)										   --draw the image in the selected coordinates
end
if wave = 2 do
image = draw.newImage(wave2)				   --create an image with the previous array
image:draw(9,9)  
end
if wave = 3 do
image = draw.newImage(wave3)				   --create an image with the previous array
image:draw(9,9)	
end
if wave = 4 do
image = draw.newImage(wave4)				   --create an image with the previous array
image:draw(9,9)	
end
if wave = 5 do
image = draw.newImage(wave5)				   --create an image with the previous array
image:draw(9,9)	
end
if wave > 5 do
term.setCursorPosition(9,9)
print("Wave "..wave)
end
sleep(6*wave)
tonext = 6*wave
wave = wave + 1
end
wynro #6
Posted 17 March 2012 - 08:43 AM
got an error when i made array. It wants me to end array at 2nd line.
I need help because i am making nazi zombies map.

A little recomendation, for futures errors try to write the error message that gives you the computer.
Another tip when you are going to write a lot of code, try to use spoilers to avoid huge posts.
I assume that you have installed correctly the API.
Your problem is that this

wave1 = {	 
"|"
"|"
"|"
"|"
"|"
}
should be like this:

wave1 = {
"|",
"|",
"|",
"|",
"|",
}

And the ifs are:

if wave == 1 then
  image = draw.newImage(wave1)
  image:draw(9,9)
end
instead of:

if wave = 1 do
image = draw.newImage(wave1)
image:draw(9,9)
end
Note the then instead of the do, and the == instead of the =.

So the code fixed is:
Spoiler

local tonext = 60
local wave = 1
local i = 1
wave1 = {
"|",
"|",
"|",
"|",
"|",
}
wave2 = {
"| |",
"| |",
"| |",
"| |",
"| |",
}
wave3 = {
"| | |",
"| | |",
"| | |",
"| | |",
"| | |",
}
wave3 = {																					   
"| | |",
"| | |",
"| | |",
"| | |",
"| | |",
}
wave4 = {																									  
"| | | |",
"| | | |",
"| | | |",
"| | | |",
"| | | |",
}
wave5 = {																								
"| | | |",
"| | | |",
"+-+-+-+",
"| | | |",
"| | | |",
}
term.setCursorPos(9,1)
print("--		   SYSTEM ERROR		    --")
print("--   Power level is critical  --")
print("-- Shutting down Pack-a-punch --")
print("We'll need to turn it on somehow...")
for i=99,1,-1 do
  term.clear()
  if wave == 1 then
    image = draw.newImage(wave1)
    image:draw(9,9)
  end
  if wave == 2 then
    image = draw.newImage(wave2)
    image:draw(9,9) 
  end
  if wave == 3 then
	 image = draw.newImage(wave3)
    image:draw(9,9)
  end
  if wave == 4 then
    image = draw.newImage(wave4)
    image:draw(9,9)
  end
  if wave == 5 then
    image = draw.newImage(wave5)
    image:draw(9,9)
  end
  if wave > 5 then
    term.setCursorPos(9,9)
    print("Wave "..wave)
  end
  sleep(6*wave)
  tonext = 6*wave
  wave = wave + 1
end

If this code doesnt work, the problem is that you hasnt installed correctly the API

If you doesnt know about programming in LUA I recomend you to read this
princepsp #7
Posted 17 March 2012 - 03:51 PM
Oww i understand i doesnt installed too
Hapsburger #8
Posted 22 March 2012 - 04:40 AM
You can change the approx function to:

local function approx(var)
  return math.floor(var + 0.5)
end
wynro #9
Posted 22 March 2012 - 04:09 PM
You can change the approx function to:

local function approx(var)
  return math.floor(var + 0.5)
end

Thank you for the tip :(/>/>
Ray1235 #10
Posted 30 March 2012 - 01:42 PM
Thank you. I used pascal mostly before and that's why i have errors. BTW thx u :o/>/>
EDIT: Still had errors and had to do it manually and I had erased Draw API code :o/>/>
BTW i made program for checking if the power is on and controls pack-a-punch door :)/>/>
Smidge #11
Posted 01 August 2012 - 03:35 AM
You can change the approx function to:

local function approx(var)
  return math.floor(var + 0.5)
end

Thank you for the tip :ph34r:/>/>
But what if the value is negative? You would have to use math.ceil instead.
Smidge #12
Posted 01 August 2012 - 03:37 AM
I've got a pretty good circle drawing function you can use. Not only does it draw circles but can also draw ellipsis' or in other words, ovals.