# Bresenham Circle Algorithm Using OpenGL and C++

Here is the Bresenham Circle Algorithm, generally accepted as the most optimized circle algorithm.

// bresenham_circle_algorithm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "GL/glut.h";
#include <math.h>
#define window_wide 1300
#define window_height 700

float ax,ay,bx,by,check=0;
void setPixel(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glFlush();
}

void breselham_func()
{
int r = sqrt((bx-ax)*(bx-ax)+(by-ay)*(by-ay));
int x = 0, y = r;
int d = 3-2*r;
while(x<=y)
{
setPixel(ax+x,ay+y);
setPixel(ax+x,ay-y);
setPixel(ax-x,ay+y);
setPixel(ax-x,ay-y);
setPixel(ax+y,ay+x);
setPixel(ax+y,ay-x);
setPixel(ax-y,ay+x);
setPixel(ax-y,ay-x);
if(d<0)
{
d = d+4*x+6;
}
else
{
d = d+4*(x-y)+10;
y–;
}
x++;
}
}

void myMouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if(check==0)
{
ax = x;
ay = window_height-y;
check = 1;
setPixel(ax,ay);
}
else if(check==1)
{
bx = x;
by = window_height-y;
check = 0;
breselham_func();
//glFlush();
}
}
}

void myDisplay(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(1.0,0.0,0.0);
gluOrtho2D(0.0,window_wide,0.0,window_height);
glClear(GL_COLOR_BUFFER_BIT);
glutMouseFunc(myMouse);
}

int _tmain(int argc, char** argv)
{
glutInitWindowSize(window_wide,window_height);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham Center Algorithm");
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}

To get complete code and project contact with me through email , mobile. You may commented below as your requirement.