Draw Midpoint Circle Algorithm with Keyboard Function

Midpoint algo reference: http://www.eazynotes.com/notes/computer-graphics/algorithms/mid-point-circle-algorithm.pdf
http://suraj1693.blogspot.com/2013/11/c-program-for-drawing-circle-using.html
https://birunthag.blogspot.com/2014/05/openglc-draw-circle-with-midpoint.html
https://www.opengl.org/discussion_boards/showthread.php/169955-using-glutKeyboardFunc

Code:

#include <windows.h>
#include <GL\glut.h>
#include <stdlib.h>
#include<iostream>
#include<stdio.h>
using namespace std;

int r,p,flags=0;

void myInit (void)

{

    glClearColor(0.0,0.0,0.0,0.0); // sets background color to white

    // sets a point to be 4x4 pixels

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluOrtho2D(0.0, 500.0, 0.0, 400.0); // the display area in world coordinates.

}
void midpoint()
{
    r=20;
    p=1-r;
    int xk=0;
    int yk=r;
    int xc=200;
    int yc=200;
    while(xk<=yk)
    {
        if(p<0)
        {
            xk++;
            p=p+2*xk+3;
        }
        else if(p>0)
        {
            xk++;
            yk=yk-1;
            p=p+2*xk-2*yk+5;
        }
        glVertex2i(xc+xk,yc+yk);
        glVertex2i(xc-xk,yc+yk);
        glVertex2i(xc+xk,yc-yk);
        glVertex2i(xc-xk,yc-yk);
        glVertex2i(xc+yk,yc+xk);
        glVertex2i(xc-yk,yc+xk);
        glVertex2i(xc+yk,yc-xk);
        glVertex2i(xc-yk,yc-xk);
    }

}

void processKey(unsigned char key, int x, int y)
{
	switch(key){
		case '1':
			flags=1;
			printf("Hello world!");
			break;

		//default:
        // break;
	}
}

void myDisplay(void)
{
    //int nol = rand() % 100;

    glClear(GL_COLOR_BUFFER_BIT); // clears the screen
    // setsthe drawing colour

    glPointSize(4.0);

    glColor3f(1.0f,0.0f,1.0f) ;
    glBegin(GL_LINES);

    glVertex2i(0,200);
    glVertex2i(500,200);

    glVertex2i(255,0);
    glVertex2i(255,500);

    glEnd();


glBegin(GL_POINTS);
//midpoint();
if(flags==1){
    midpoint();
}

glEnd();


    glFlush(); // sends all output to display;

}


int main (int argc, char **argv)
{

//scanf("%d,%d,&d,&d",&x,&y,&a,&b);
    glutInit (&argc, argv); // to initialize the toolkit;

    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // sets the display mode

    glutInitWindowSize (640, 480); // sets the window size

    glutInitWindowPosition (10, 10); // sets the starting position for the window

    glutCreateWindow ("My first OpenGL program!"); // creates the window and sets the title
    glutKeyboardFunc(processKey);
    glutDisplayFunc (myDisplay);
    myInit(); // additional initializations as necessary

    glutMainLoop(); // go into a loop until event occurs
    return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.