Created
October 20, 2015 10:52
-
-
Save nicoptere/56af56910cf587bd5235 to your computer and use it in GitHub Desktop.
bresenham circle / disc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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