Advertisement
incinirate

Circle Generator Computercraft

Jun 19th, 2014
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. --Variables
  2.  
  3. local tArgs={...}
  4.  
  5. loading = false
  6.  
  7. if shell==nil then
  8.   loading=true
  9. end
  10.  
  11. tx,ty=term.getSize()
  12. tx,ty=math.floor(tx),math.floor(ty)
  13.  
  14. pi=math.pi
  15.  
  16. px=tx/2
  17. py=ty/2
  18.  
  19. points={}
  20.  
  21. if #tArgs>1 then
  22.   if tonumber(tArgs[1]) and tonumber(tArgs[2]) then
  23.     if tonumber(tArgs[2])/4~=math.floor(tonumber(tArgs[2])/4) then
  24.       print("Usage: circles [radius] [npts]")
  25.       print("npts must be divisible by 4")
  26.       error()
  27.     end
  28.     r=tonumber(tArgs[1])
  29.     d=tonumber(tArgs[2])
  30.   else
  31.     print("Usage: circles [radius] [npts]")
  32.     error()
  33.   end
  34. elseif not loading then
  35.   print("Usage: circles [radius] [npts]")
  36.   error()
  37. end
  38.  
  39. --End Variables
  40.  
  41. --Formulas/Functions
  42.  
  43. local function getPoints(a)
  44.   x = r * math.cos(a)
  45.   y = r * math.sin(a)
  46.   return x,y
  47. end
  48.  
  49. ----Below functions are if you are using me as an API
  50.  
  51. function setX(xval)
  52.   local sxv = tostring(xval)
  53.   local pos=0
  54.   repeat
  55.     pos=pos+1
  56.   until sxv:sub(pos,pos)=="." or pos==#sxv
  57.   if pos==#sxv then pos=pos+1 end
  58.   local num=tonumber(sxv:sub(1,pos-1))+0.5
  59.   px=num
  60. end
  61.  
  62. function setY(yval)
  63.   local syv = tostring(yval)
  64.   local pos=0
  65.   repeat
  66.     pos=pos+1
  67.   until syv:sub(pos,pos)=="." or pos==#syv
  68.   if pos==#syv then pos=pos+1 end
  69.   local num=tonumber(syv:sub(1,pos-1))+0.5
  70.   py=num
  71. end
  72.  
  73. function drawCircle(ra,da,cls)
  74.   if ra and da then else
  75.     return false
  76.   end
  77.   r=ra
  78.   d=da
  79.   ----Above functions are if you are using me as an API
  80.  
  81.   --End Formulas/Functions
  82.  
  83.   --[[                   DEPRECATED
  84.   repeat
  85.   repeat
  86.     shell.run("clear")
  87.     write("Radius:")
  88.     r = tonumber(read())
  89.   until r~=nil
  90.   until r>0
  91.  
  92.   repeat
  93.   repeat
  94.     shell.run("clear")
  95.     write("Points (Must be divisible by 4):")
  96.     d = tonumber(read())
  97.   until d~=nil
  98.   until d>3 and d/4==math.floor(d/4)
  99.   ]]--                                           DEPRECATED
  100.  
  101.   if not loading and cls then shell.run("clear") elseif cls
  102.     term.clear()
  103.   end
  104.  
  105.   --Get All Points
  106.  
  107.   for i=1,4 do
  108.     ip=d/4
  109.     cpi=(pi/2)*(i-1)
  110.     npi=(pi/2)*(i)
  111.     for j=0,ip-1 do
  112.       a=j*(pi/(ip+3))+cpi
  113.       nx,ny = getPoints(a)
  114.       table.insert(points,{nx,ny})
  115.       --print("Angle: "..a)
  116.     end
  117.   end
  118.  
  119.   --End Point Getting
  120.  
  121.   term.setBackgroundColor(colors.white)
  122.   for i=1,#points do
  123.     --print(points[i][1])
  124.     --print(points[i][2])
  125.     term.setCursorPos(px-points[i][1],py-points[i][2])
  126.     write(" ")
  127.   end
  128.   term.setBackgroundColor(colors.black)
  129.   return true
  130. end
  131. if #tArgs>1 then drawCircle(r,d) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement