Advertisement
billysback

polygrid

Sep 13th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | None | 0 0
  1.  
  2. local width, height = term.getSize()
  3.  
  4. local function removePower(n, digits)
  5.     local number = n
  6.     while number > 10^digits do
  7.         number = number/10
  8.     end
  9.     return number
  10. end
  11.  
  12. local curn = math.random()
  13. local function addNoise(n, amt)
  14.     local dec = removePower(curn, 0)
  15.     amt = (amt + dec)*10
  16.     local number = (math.random(amt*2)-amt)/10
  17.     n = n + number
  18.     curn = math.abs(curn * number)
  19.     return n
  20. end
  21.  
  22. local function getDistance(x1, y1, x2, y2)
  23.     local dist = math.sqrt((math.abs(x1-x2)^2)+(math.abs(y1-y2)^2))
  24.     return math.ceil(dist-0.49)
  25. end
  26.  
  27. local function getClosest(p, points)
  28.     local mind = nil
  29.     local curi = nil
  30.     for i=1,#points do
  31.         local p2 = points[i]
  32.         local dist = getDistance(p[1], p[2], p2[1], p2[2])
  33.         if mind == nil then
  34.             mind = dist
  35.             curi = i
  36.         elseif dist < mind then
  37.             mind = dist
  38.             curi = i
  39.         end
  40.     end
  41.     return curi
  42. end
  43.  
  44. local stuff = {...}
  45. local cols = 6
  46. local rows = 4
  47. local noise = 2
  48. if stuff[1] ~= nil then noise = tonumber(stuff[1]) end
  49. if stuff[2] ~= nil then cols = tonumber(stuff[2]) end
  50. if stuff[3] ~= nil then rows = tonumber(stuff[3]) end
  51.  
  52. local points = {}
  53. local grad = {width/(cols+1), height/(rows+1)}
  54. for xb=1,cols do
  55.     for yb=1,rows do
  56.         local p = {addNoise(xb*grad[1], noise), addNoise(yb*grad[2], noise)}
  57.         points[#points + 1] = p
  58.     end
  59. end
  60.  
  61. local grid = {}
  62. grid.width = width
  63. grid.height = height
  64. for x=1,width do
  65.     grid[x] = {}
  66.     for y=1,height do
  67.         local i = getClosest({x, y}, points)
  68.         grid[x][y] = i
  69.     end
  70. end
  71.  
  72. local colorMap = {colors.red, colors.blue, colors.yellow, colors.green, colors.lightBlue, colors.lime, colors.pink, colors.white, colors.black}
  73. local function draw(grid)
  74.     for x=1,grid.width do
  75.         for y=1,grid.height do
  76.             local i = (grid[x][y])%(16)
  77.             local c = 2^i
  78.             term.setBackgroundColor(c)
  79.             term.setCursorPos(x,y)
  80.             term.write(" ")
  81.         end
  82.     end
  83. end
  84. draw(grid)
  85. sleep(4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement