Wednesday, 4 June 2014

Bresenham's Circle algorithm in text mode



#include<stdio.h>

#define wsize 50
int buffer[wsize][wsize];

main()
{
 int i, j;
                                                                                               
 for(i = 0; i < wsize; i++)
   for(j = 0; j < wsize; j++)
      buffer[i][j] = 1;

 MidpointCircle(25, 25, 20);
                                                                                                
 printf("\n");
 for(i = 0; i < wsize; i++)
   {
    for(j = 0; j < wsize; j++)
      printf("%d ",buffer[i][j]);
    printf("\n");
   }

}

MidpointCircle( int cen_x, int cen_y, int rad)
{
 int x, y, d;

 x = 0; y = rad; d = 3 - 2 * rad;
 CirclePoints(cen_x, cen_y, x, y);
 while(x < y)
  {
   if (d < 0) d = d + 4 * x + 6;
   else
    {
     d = d + 4 * (x - y) + 10;
     y--;
    }
   x++;
   CirclePoints(cen_x, cen_y, x, y);
  }
}

CirclePoints(int Center_x, int Center_y, int x, int y)
{

 buffer[Center_x + x][Center_y + y] = 0;
 buffer[Center_x - x][Center_y + y] = 0;
 buffer[Center_x + x][Center_y - y] = 0;
 buffer[Center_x - x][Center_y - y] = 0;
 buffer[Center_x + y][Center_y + x] = 0;
 buffer[Center_x - y][Center_y + x] = 0;
 buffer[Center_x + y][Center_y - x] = 0;
 buffer[Center_x - y][Center_y - x] = 0;

}

No comments:

Post a Comment