# Bresenham Circle Algorithm

• 04-30-2002
cozman
Bresenham Circle Algorithm
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++)?

Code:

```         PHP Code:             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.
• 04-30-2002
taylorguitarman
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.
• 04-30-2002
taylorguitarman
Here's the algorithm I used: (it's almost actual code so you should be able to modify it to what you need)
Code:

```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 ); }```
• 04-30-2002
Leeman_s
does this help?

Code:

```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);   } }```
• 04-30-2002
cozman
thanks for all the help
thanks for all the help :) i got it working now