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

Making a circle with lua?

Started by Thib0704, 21 February 2014 - 09:43 AM
Thib0704 #1
Posted 21 February 2014 - 10:43 AM
Hi,
So today I was trying to make a circle in CC.
I googled a bit and understood, I had to use math.cos and math.sin.
Well I'm still lost :P/>
If someone could explain me, that would be awsome!

Thanks!
Alice #2
Posted 21 February 2014 - 10:48 AM
I'm not sure how it's done, but I know it has something to do with the pythagorean thereom, so yes you will need math.cos and math.sin probably somewhere in there.
H4X0RZ #3
Posted 21 February 2014 - 12:47 PM
I'm not entirely sure…

--# sX = x position, sY = y position, sR = radius
function circle(sX,sY,sR)

  local radius = math.pi * sR
  for i = 1, 360 do
    local x = math.sin(radius*i)
    local y = math.cos(radius*i)
    term.setCursorPos(sX+x,sY+y)
    term.write(" ")
  end

end
Maybe it's wrong, idk
Bomb Bloke #4
Posted 21 February 2014 - 07:32 PM
Do you mean "draw a circle on the screen" or "make a turtle move around in a circle"? Are you talking about a basic circle, or a filled in circle?
Thib0704 #5
Posted 21 February 2014 - 08:59 PM
basic, filled or not filled.
Edited on 21 February 2014 - 08:00 PM
Spexiono #6
Posted 21 February 2014 - 09:51 PM
I'd be interested in seeing how this is done.. might make some sort of API if i can figure it out… Still a bit of a Lua noob though.
Warumdk #7
Posted 23 February 2014 - 08:36 AM
What you are looking for is the Midpoint circle algorithm, making voxel circles requires a bit of math. I might give it a try tho, since I have been slacking on my math usage lately.
CometWolf #8
Posted 09 March 2014 - 12:19 AM
Here's a very simple hollow circle function i just made for my own program, with the program specific parts stripped and some comments added. This can draw ellipsoids aswell.

function circleRender(x1,x2,y1,y2,color)
  if not (x1 and x2 and y1 and y2) then
    return
  end
  local x = {
    max = math.max(x1,x2), --right most x
    min = math.min(x1,x2) --left most x
  }
  x.rad = math.floor(x.max-x.min)/2 --radius
  x.center = x.rad+x.min --circle x-axis center
  local y = {
    max = math.max(y1,y2), --bottom y
    min = math.min(y1,y2) --top y
  }
  y.rad = (y.max-y.min)/2
  y.center = y.rad+y.min --circle y-axis center
  local thetha = 0
  local tPoints = {}
  while thetha <= 360 do
    local xP = math.floor(x.center+x.rad*math.cos(thetha)) --calculate x point
    local yP = math.floor(y.center+y.rad*math.sin(thetha)) --calculate y point
    tPoints[xP] = tPoints[xP] or {} --countine existing x-axis or create new one
    tPoints[xP][yP] = true -- mark point on y axis along x axis
    thetha = thetha+1
  end
  for x,yT in pairs(tPoints) do
    for y in pairs(yT) do
	  if y <= canvasEdge then
	    paintutils.drawPixel(x,y,color)
	  end
    end
  end
end
theoriginalbit #9
Posted 09 March 2014 - 01:03 AM
here's one I've thrown together in the past (slightly different version is also in my thread)


function drawCircle( centerX, centerY, radius, color )
	local isAdvanced = term.isColor and term.isColor()
	local char = isAdvanced and " " or "|"
	if isAdvanced then term.setBackgroundColor( color ) end
	local radStep = 1/(1.5*radius)
	for angle = 1, math.pi+radStep, radStep do
		local pX = math.cos( angle ) * radius * 1.5
		local pY = math.sin( angle ) * radius

		for i=-1,1,2 do
			for j=-1,1,2 do
				term.setCursorPos( centerX + i*pX, centerY + j*pY )
				write( char )
			end
		end
	end
end

it also supports non-advanced computers by having a char that it can print instead of colours.
Edited on 09 March 2014 - 12:04 AM
cdel #10
Posted 10 December 2014 - 11:52 AM
here's one I've thrown together in the past (slightly different version is also in my thread)


function drawCircle( centerX, centerY, radius, color )
	local isAdvanced = term.isColor and term.isColor()
	local char = isAdvanced and " " or "|"
	if isAdvanced then term.setBackgroundColor( color ) end
	local radStep = 1/(1.5*radius)
	for angle = 1, math.pi+radStep, radStep do
		local pX = math.cos( angle ) * radius * 1.5
		local pY = math.sin( angle ) * radius

		for i=-1,1,2 do
			for j=-1,1,2 do
				term.setCursorPos( centerX + i*pX, centerY + j*pY )
				write( char )
			end
		end
	end
end

it also supports non-advanced computers by having a char that it can print instead of colours.

do you mind if I use this in my engine?
LtKillPuppy #11
Posted 10 December 2014 - 12:07 PM
Hint: Search for "how to rasterize a circle" to get more useful hints on writing a program on how to do this. This is an 2D rasterizing problem that's well explored.

If you are drawing a hollow circle, the methods given above will work. If you want a filled circle, then it's best to think of this problem as drawing a series of horizontal lines instead of individual pixels.

If you are drawing circles and not ellipses, you can mirror the top and bottom halves of the circle.