fatboychummy

Bresenham Circles in CC

Apr 10th, 2020
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.38 KB | None | 0 0
  1. -- Adapted from https://iq.opengenus.org/bresenhams-circle-drawing-algorithm/ to lua
  2. -- going to use this for a few things soon:tm:
  3.  
  4. local function putpixel(x, y, color)
  5.   term.setCursorPos(x, y)
  6.   term.setBackgroundColor(color)
  7.   io.write(' ')
  8. end
  9.  
  10. local function displayBresenhamCircle(xc_, yc_, x, y)
  11.   putpixel(xc_+x, yc_+y, colors.white);
  12.   putpixel(xc_-x, yc_+y, colors.white);
  13.   putpixel(xc_+x, yc_-y, colors.white);
  14.   putpixel(xc_-x, yc_-y, colors.white);
  15.   putpixel(xc_+y, yc_+x, colors.white);
  16.   putpixel(xc_-y, yc_+x, colors.white);
  17.   putpixel(xc_+y, yc_-x, colors.white);
  18.   putpixel(xc_-y, yc_-x, colors.white);
  19. end
  20.  
  21. local function drawBresenhamCircle(radius_, xc, yc)
  22.   term.clear()
  23.   local x = 0
  24.   local y = radius_
  25.   local decisionParameter = 3 - 2 * radius_
  26.   displayBresenhamCircle(xc, yc, x, y)
  27.  
  28.   while y >= x do
  29.     x = x + 1
  30.     if decisionParameter > 0 then
  31.       y = y - 1
  32.       decisionParameter = decisionParameter + 4 * (x - y) + 10
  33.     else
  34.       decisionParameter = decisionParameter + 4 * x + 6
  35.     end
  36.     displayBresenhamCircle(xc, yc, x, y)
  37.     os.sleep(0.1)
  38.   end
  39. end
  40.  
  41. local function readNum(out)
  42.   io.write(out .. ": ")
  43.   local got = tonumber(io.read())
  44.   return type(got) == "number" and got or readNum(out)
  45. end
  46.  
  47. drawBresenhamCircle(readNum("Radius of circle"), readNum("X pos of circle"), readNum("Y pos of circle"))
  48. os.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment