1: #include<stdio.h>
2: #include<GL/glut.h>
3: #include<math.h>
4: int ww=400,wh=400;
5: int first=0;
6: int xi,yi,xf,yf;
7: void putpixel(int x,int y)
8: {
9: glBegin(GL_POINTS);
10: glVertex2i(x,y);
11: glEnd();
12: glFlush();
13: }
14: int round(double n)
15: {
16: return (n>=0)?(int)(n+0.5):(int)(n-0.5);
17: }
18: void Bresenham_circle(int r)
19: {
20: int x=0,y=r,d=3-2*r;
21: while(x<=y){
22: //Here Transform each x,y coordinates by 250 pixels
23: putpixel(250+x, 250+y);
24: putpixel(250+y, 250+x);
25: putpixel(250-x, 250+y);
26: putpixel(250-x, 250-y);
27: putpixel(250-y, 250+x);
28: putpixel(250-y, 250-x);
29: putpixel(250+y, 250-x);
30: putpixel(250+x, 250-y);
31: if(d<0)
32: d=d+(4*x)+6;
33: else{
34: d=d+(4*(x-y))+10;
35: y--;
36: }
37: x++;
38: }
39: }
40: void display()
41: {
42: glClearColor(0.4,0.7,0.2,1.0);
43: glColor3f(0.5,0.3,0.0);
44: glClear(GL_COLOR_BUFFER_BIT);
45: glutSwapBuffers();
46: glFlush();
47: }
48: void mouse(int btn,int state,int y,int x)
49: {
50: if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
51: {
52: xi=0;
53: yi=y;
54: Bresenham_circle(yi);
55: }
56: }
57: void myinit()
58: {
59: glViewport(0,0,ww,wh);
60: glMatrixMode(GL_PROJECTION);
61: glLoadIdentity();
62: gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
63: glMatrixMode(GL_MODELVIEW);
64: }
65: int main(int argc,char** argv)
66: {
67: glutInit(&argc,argv);
68: glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
69: glutInitWindowSize(ww,wh);
70: glutCreateWindow("Bresenham-Circle");
71: glutDisplayFunc(display);
72: myinit();
73: glutMouseFunc(mouse);
74: glutMainLoop();
75: return 0;
76: }
Thursday, May 15, 2014
Opengl,C++ : Draw Circle With Bresenham’s Circle Algorithm
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment