Skip to content

Instantly share code, notes, and snippets.

@nicoptere
Created October 20, 2015 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicoptere/56af56910cf587bd5235 to your computer and use it in GitHub Desktop.
Save nicoptere/56af56910cf587bd5235 to your computer and use it in GitHub Desktop.
bresenham circle / disc
function bresenhamCircle( cx,cy,r )
{
var x = 0,y = r,p;
ctx.fillRect( cx+x,cy-y, 1, 1 );
p=3-(2*r);
for(x=0;x<=y;x++)
{
if (p<0)
{
p+=(4*x)+6;
}
else
{
y-=1;
p+=((4*(x-y)+10));
}
ctx.fillRect(cx+x,cy-y,1,1);
ctx.fillRect(cx-x,cy-y,1,1);
ctx.fillRect(cx+x,cy+y,1,1);
ctx.fillRect(cx-x,cy+y,1,1);
ctx.fillRect(cx+y,cy-x,1,1);
ctx.fillRect(cx-y,cy-x,1,1);
ctx.fillRect(cx+y,cy+x,1,1);
ctx.fillRect(cx-y,cy+x,1,1);
}
}
bresenhamCircle( 100,100, 50 );
function disc( cx, cy, r )
{
var d = 3 - ( 2 * r );
var x = 0;
var y = r;
while( y >= x )
{
ctx.fillRect( cx - x, cy - y, ( 2 * x + 1 ), 1);
ctx.fillRect( cx - x, cy + y, ( 2 * x + 1 ), 1);
ctx.fillRect( cx - y, cy - x, ( 2 * y + 1 ), 1);
ctx.fillRect( cx - y, cy + x, ( 2 * y + 1 ), 1);
if (d < 0)
d += (4 * x) + 6;
else {
d += 4 * (x - y) + 10;
y--;
}
x++;
}
}
disc( 250,100, 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment