Saturday, September 17, 2016

3. Write a program to implement Midpoint circle generation algorithm.

#include <windows.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;

double Xc, Yc, R;

/* Handler for window-repaint event. Call back when the window first appears and
   whenever the window needs to be re-painted. */
void Init()
{
    glClearColor(0.0,0.0,0.0,0);     // Set background color to black and opaque
    glClear(GL_COLOR_BUFFER_BIT);    // Clear the color buffer (background)

    glColor3f(1.0,1.0,1.0);

    glViewport(0 , 0 , 640 , 480);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0 , 640 , 0 , 480);
}

void Circle8Points(int x, int y)
{
    glVertex2i(Xc + x, Yc + y);
    glVertex2i(Xc - x, Yc + y);
    glVertex2i(Xc + x, Yc - y);
    glVertex2i(Xc - x, Yc - y);
    glVertex2i(Xc + y, Yc + x);
    glVertex2i(Xc - y, Yc + x);
    glVertex2i(Xc + y, Yc - x);
    glVertex2i(Xc - y, Yc - x);
}

//Implement the Midpoint algorithm
void CircleMidPoint(void)
{
    int x=0;
    int y=R;
    int p=1-R; //initial value of the decision parameter
    Init();
   
glBegin(GL_POINTS);
    {
            glVertex2i(Xc,Yc); //Draw circle center for illustration
            Circle8Points(x,y);/* Plot the First point */

            while(x<y)
            {
                x++;
                if(p<0)
                    p +=2 * x + 1;
                else
                {
                    y--;
                    p += 2 * (x -y) + 1;
                }
                Circle8Points(x,y);
    }
    }glEnd();
    glFlush();
}
//Main function
int main(int argc, char** argv) {
    cout<<"Enter Center point for the circle:\n";
    cin>>Xc >>Yc;
    cout<<"\nEnter Radius : ";
    cin>>R;


   glutInit(&argc, argv);                 // Initialize GLUT
   glutCreateWindow("OpenGL MidPoint Circle Algorithm");
   glutInitWindowSize(320, 320);   // Set the window's initial width & height
   glutInitWindowPosition(50, 50); // Position the window's initial top-left corner

   glutDisplayFunc(CircleMidPoint); // Register display callback handler for window re-paint

   glutMainLoop();           // Enter the event-processing loop
   return 0;
}

No comments:

Post a Comment