Posted 04 January 2013 - 07:29 AM
does anyone know how to generate a circle…i have been thinking of this for hours and for some reason cant think of how to :l i would like to make a circle function for my graphics library
function distance(x1,y1,x2,y2)
return math.sqrt((x2-x1)^2+(y2-y1)^2)
end
function Circle(x,y,r)
for tx=x-r,x+r do
for ty=y-r,y+r do
if (distance(tx,ty,x,y) >= r and distance(tx,ty,x,y) <= r+0.5) then
term.setCursorPos(tx,ty)
term.write("X")
end
end
end
end
function distance(x1,y1,x2,y2)
return math.sqrt((x2-x1)^2+(y2-y1)^2)
end
function Circle(x,y,r)
for tx=x-r,x+r do
for ty=y-r,y+r do
if (tx == x and ty == y) then -- Midpoint drawing
term.setCursorPos(tx,ty)
term.write("O")
elseif (distance(tx,ty,x,y) >= r and distance(tx,ty,x,y) <= r+0.5) then -- Outside drawing
term.setCursorPos(tx,ty)
term.write("X")
elseif (distance(tx,ty,x,y) <= r+0.5) then -- Inside drawing
term.setCursorPos(tx,ty)
term.write("-")
end
end
end
end
term.clear()
Circle(15,8,5)
term.setCursorPos(1,1)
function sky.graphics.circle(x,y,rad,fill,colour)
for tx=x-rad,x+rad do
for ty=y-rad,y+rad do
if (math.sqrt((tx-ty)^2+(x-y)^2) >= rad and math.sqrt((tx-ty)^2+(x-y)^2) <= rad+0.5) then
sky.graphics.pixel(tx,ty,colour,2," ")
end
end
end
end
(might be because i mixed the variables up) but thanks anyway i got something working
function sky.graphics.circle(x,y,rad,fill,colour)
for tx=x-rad,x+rad do
for ty=y-rad,y+rad do
if (math.sqrt((tx-x)^2+(ty-y)^2) >= rad and math.sqrt((tx-x)^2+(ty-y)^2) <= rad+0.5) then
sky.graphics.pixel(tx,ty,colour,2," ")
end
end
end
end
i put the variables in the wrong order but it still doesnt quite work (it misses some corners)
function distance(x1,y1,x2,y2)
return math.sqrt((x2-x1)^2+(y2-y1)^2)
end
function Circle(x,y,r)
for tx=x-r,x+r do
for ty=y-r,y+r do
if (tx == x and ty == y) then -- Midpoint drawing
term.setCursorPos(tx,ty)
term.write("O")
elseif (distance(tx,ty,x,y) >= r and distance(tx,ty,x,y) <= r+(r/distance(tx,ty,x,y))) then -- Outside drawing
term.setCursorPos(tx,ty)
term.write("X")
elseif (distance(tx,ty,x,y) <= r+(r/distance(tx,ty,x,y))) then -- Inside drawing
term.setCursorPos(tx,ty)
term.write("-")
end
end
end
end
term.clear()
Circle(15,8,2)
term.setCursorPos(1,1)
function sky.graphics.circle(x,y,r,colour,fill)
for tx=x-r,x+r do
for ty=y-r,y+r do
if (math.sqrt((x-tx)^2+(y-ty)^2) >= r and math.sqrt((x-tx)^2+(y-ty)^2) <= r+(r/math.sqrt((x-tx)^2+(y-ty)^2))) then
sky.graphics.pixel(tx,ty,colour,2," ")
elseif (math.sqrt((x-tx)^2+(y-ty)^2) <= r+(r/math.sqrt((x-tx)^2+(y-ty)^2))) and fill then
sky.graphics.pixel(tx,ty,colour,2," ")
end
end
end
end
function sky.graphics.circle(x,y,rad,fill,color)
for tx=x-r,x+r do
for ty=y-r,y+r do
if (math.sqrt((tx-x)^2+(ty-y)^2) >= r and math.sqrt((tx-x)^2+(ty-y)^2) <= r+(r/math.sqrt((tx-x)^2+(ty-y)^2))) then -- Outside drawing
sky.graphics.pixel(tx,ty,color,2," ")
elseif (math.sqrt((tx-x)^2+(ty-y)^2) <= r+(r/math.sqrt((tx-x)^2+(ty-y)^2))) then -- Inside drawing
sky.graphics.pixel(tx,ty,color,2," ")
end
end
end
end