Bresenham Circle Algorithm

This is a discussion on Bresenham Circle Algorithm within the Game Programming forums, part of the General Programming Boards category; Does anyone have experience with this algo.. i can't seem to get it working right (i converted the code i ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    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;     - (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(0)             += (4*mx)+6;             else         {             += 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.
    Last edited by cozman; 04-30-2002 at 04:50 PM.

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    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.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  3. #3
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    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 );
    }
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    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);
       }
    }

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    thanks for all the help

    thanks for all the help i got it working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 04:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21