Bresenham Circle Algorithm [Archive] - C Board

PDA

View Full Version : Bresenham Circle Algorithm


cozman
04-30-2002, 03:37 PM
Does anyone have experience with this algo.. i can't seem to get it working right (i converted the code i found on gamedev from pascal to c++)?


int d,mx,my;

d = 3 - (2*radius);
mx = 0;
my = radius;

while(my != mx)
{
DrawPixel(surface,x+mx,y+my,color);
DrawPixel(surface,x+mx,y-my,color);
DrawPixel(surface,x-mx,y+my,color);
DrawPixel(surface,x-mx,y-my,color);
DrawPixel(surface,x+my,y+mx,color);
DrawPixel(surface,x+my,y-mx,color);
DrawPixel(surface,x-my,y+mx,color);
DrawPixel(surface,x-my,y-mx,color);

if(d < 0)
d += (4*mx)+6;
else
{
d += 4*(mx-my) + 10;
my--;
}
}


edit: It is drawing a + sign rather than an O.. I'm trying to figure this one out but so far no luck.

taylorguitarman
04-30-2002, 04:24 PM
I don't remember if it was Bresenham's or another one, but I got it to work with DirectDraw. I'll try and find it.

taylorguitarman
04-30-2002, 05:45 PM
Here's the algorithm I used: (it's almost actual code so you should be able to modify it to what you need)

int x_center; // the center of the circle
int y_center;

int x; // used for the algorithm to draw points on the border
int y;

int radius;

int r2 = radius * radius; // the radius sqaured

PutPixel( x_center, y_center + radius );
PutPixel( x_center, y_center - radius );
PutPixel( x_center + radius, y_center );
PutPixel( x_center - radius, y_center );

x = 1;
y = (int)( sqrt( r2 - 1 ) + 0.5 );

while( x < y ) {

PutPixel( x_center + x, y_center + y );
PutPixel( x_center + x, y_center - y );
PutPixel( x_center - x, y_center + y );
PutPixel( x_center - x, y_center - y );
PutPixel( x_center + y, y_center + x );
PutPixel( x_center + y, y_center - x );
PutPixel( x_center - y, y_center + x );
PutPixel( x_center - y, y_center - x );

++x;
y = (int)( sqrt( r2 - x * x ) + 0.5 );
}
if (x == y) {

PutPixel( x_center + x, y_center + y );
PutPixel( x_center + x, y_center - y );
PutPixel( x_center - x, y_center + y );
PutPixel( x_center - x, y_center - y );
}

Leeman_s
04-30-2002, 08:21 PM
does this help?


void retro_circle(int xc, int yc, int r)
{
int x,
y,
d,

y = r;
d = 1 - r;

for (x = 0; x < y; x++)
{
if (d < 0)
{
d += x * 2 + 3;
}
else
{
d += x * 2 - y * 2 + 5;
y--;
}

vga_drawpixel(xc + x, yc + y);
vga_drawpixel(xc - x, yc + y);
vga_drawpixel(xc + x, yc - y);
vga_drawpixel(xc - x, yc - y);
vga_drawpixel(xc + y, yc + x);
vga_drawpixel(xc - y, yc + x);
vga_drawpixel(xc + y, yc - x);
vga_drawpixel(xc - y, yc - x);
}
}

cozman
04-30-2002, 09:21 PM
thanks for all the help :) i got it working now