//linda macphee-cobb fall 99 //demonstrate midpoint line drawing algorithm for a circle //input r //output x, y each iteration and draw in a window //www.timestocome.com //to compile on OSX //gcc circle.c -o circle -framework GLUT -framework OpenGL #include #include #include #include //globals //opengl rountines are predefined so data that is passed to them must //be declared globably int r; //user input radius //prototypes void display(void); void myinit(void); int main(int argc, char **argv) { //get user input system("clear"); printf("\n Input the radius => "); scanf("%d", &r); //calculate and plot points glutInit(&argc, argv); glutInitWindowSize(500, 250); glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); (void)glutCreateWindow("mid-point circle algorithm"); glutDisplayFunc(display); myinit(); glutMainLoop(); return 0; } //calculate and draw points void display (void){ typedef GLfloat point2[2]; //define a point array point2 p = {0.0, 0.0}; //temp for calcluations point2 p1, p2, p3, p4, p5, p6, p7; int x = 0; int y = r; int d = 1 - r; int deltaE = 3; int deltaSE = -2*r + 5; int i; glClear(GL_COLOR_BUFFER_BIT); //clear window while(y > x){ //calculate points if(d < 0){ d += deltaE; deltaE += 2; deltaSE += 2; }else{ d += deltaSE; deltaE += 2; deltaSE += 4; y--; } x++; //plot points (*points are cast to floats before plotting...) p[0] = x; p[1] = y; p1[0] = y; p1[1] = x; p2[0] = y; p2[1] = -x; p3[0] = x; p3[1] = -y; p4[0] = -x; p4[1] = y; p5[0] = -y; p5[1] = -x; p6[0] = -y; p6[1] = x; p7[0] = -x; p7[1] = -y; glBegin(GL_POINTS); glVertex2fv(p); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); //dump points to terminal window // printf(" x = %d; y = %d\t", x, y); //if(i > 1){ //printf("\n"); //i=0; //}else{ //i++; //} } glFlush(); //plot quickly.. only makes a difference over a network } //set up window void myinit (void){ //attributes glClearColor(1.0, 1.0, 1.0, 0.0); //backround (r, g, b, alpha) glColor3f(1.0, 0.0, 0.0); //draw color (r, g, b) //set up viewing glMatrixMode(GL_PROJECTION); gluOrtho2D(-250.0, 250.0, -125.0, 125.0); //viewing area glMatrixMode(GL_MODELVIEW); }