6 Program to draw Ring Pattern using Bresenham Circle Drawing Algorithm using OpenGL

Program 1:

#include<GL/glut.h>
#include<iostream>
#include <math.h>

using namespace std;

void  myInit()
{
    gluOrtho2D(0,600,0,600);
    //glOrtho(0, 600, 600, 0, 0, 1);
    glClearColor(1,1,1,1);
    glColor3f(1,0,1);
}

void plotpixel(int x,int y)
{
    glPointSize(4);
    glBegin(GL_POINTS);
        glVertex2i(x,y);
    glEnd();
    glFlush();
}

void octant(int xc,int yc,int x,int y)
{
    plotpixel(xc+x,yc+y);
    plotpixel(xc+y,yc+x);

    plotpixel(xc+x,yc-y);
    plotpixel(xc+y,yc-x);
    
    plotpixel(xc-x,yc+y);
    plotpixel(xc-y,yc+x);
    
    plotpixel(xc-x,yc-y);
    plotpixel(xc-y,yc-x);
}


//convert the rad to deg
double ang(int q)
{
   return (double)q * 3.142 / 180;
}

// To draw the center point
void Point(int x, int y)
{
   glPointSize(5);
   glBegin(GL_POINTS);
   glVertex2i(x, y);
   glEnd();
}


void circleMP(int xc, int yc, int r)
{
   int p = 1 - r, x = 0, y = r;
   //loop til the x become y equal to radius (r,r)
   Point(xc,yc);

   while (x < y)
   {
      octant(xc, yc, x, y);
      x++;
      //if p>0 decrement the y and2(x-y)+1
      if (p > 0)
      {
         y--,
             p += 2 * (x - y) + 1;
      }
      //if p<=0 add 2x+1 to p
      else
      {
         p += 2 * x + 1;
      }
   }
}


void drawcircles(int x, int y, int r)
{
   // First 90 degree 
   int i,j = 1;
   circleMP(x,y,r); 
   for( i=0;i<10;i+=2)
   {
      circleMP(x + (2+i) * r * cos(ang(60)), y - (1+j) * r * sin(ang(60)), r);
      circleMP(x - (2+i) * r * cos(ang(60)), y - (1+j) * r * sin(ang(60)), r);
      j+=2 ;     
   }
 
    
   // Bottom 90 degree
   cout<<i;
   for( i=i;i>0;i-=2)
   {
      circleMP(x + (i-2) * r * cos(ang(60)), y - (j+1) * r * sin(ang(60)), r);
      circleMP(x - (i-2) * r * cos(ang(60)), y - (j+1) * r * sin(ang(60)), r);
      j+=2;     
   }
}

void drawShape()
{
   myInit();
   drawcircles(300, 500, 20);
}


int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(300,150);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

    glutCreateWindow("Ring Ring Pattern");

    glutDisplayFunc(drawShape);

    glutMainLoop();
    return 0;
}
Output:

Ring Pattern using Bresenham Circle Drawing Algorithm


Program 2:

#include<GL/glut.h>
#include<iostream>
#include <math.h>

using namespace std;

void  myInit()
{
    gluOrtho2D(0,600,0,600);
    //glOrtho(0, 600, 600, 0, 0, 1);
    glClearColor(1,1,1,1);
    glColor3f(1,0,1);
}

void plotpixel(int x,int y)
{
    glPointSize(4);
    glBegin(GL_POINTS);
        glVertex2i(x,y);
    glEnd();
    glFlush();
}

void octant(int xc,int yc,int x,int y)
{
    plotpixel(xc+x,yc+y);
    plotpixel(xc+y,yc+x);

    plotpixel(xc+x,yc-y);
    plotpixel(xc+y,yc-x);
    
    plotpixel(xc-x,yc+y);
    plotpixel(xc-y,yc+x);
    
    plotpixel(xc-x,yc-y);
    plotpixel(xc-y,yc-x);
}


//convert the rad to deg
double ang(int q)
{
   return (double)q * 3.142 / 180;
}

// To draw the center point
void Point(int x, int y)
{
   glPointSize(5);
   glBegin(GL_POINTS);
   glVertex2i(x, y);
   glEnd();
}


void circleMP(int xc, int yc, int r)
{
   int p = 1 - r, x = 0, y = r;
   //loop til the x become y equal to radius (r,r)
   Point(xc,yc);

   while (x < y)
   {
      octant(xc, yc, x, y);
      x++;
      //if p>0 decrement the y and2(x-y)+1
      if (p > 0)
      {
         y--,
             p += 2 * (x - y) + 1;
      }
      //if p<=0 add 2x+1 to p
      else
      {
         p += 2 * x + 1;
      }
   }
}



void drawcircles(int x, int y, int r)
{
    circleMP(x + 2 * r * cos(ang(60)), y - 1 * r * sin(ang(60)), r);
    circleMP(x + 6  * r * cos(ang(60)), y - 2 * r * sin(ang(60)), r);
    circleMP(x + 8  * r *  cos(ang(60)), y - 4 * r * sin(ang(60)), r);
    circleMP(x + 6  * r * cos(ang(60)), y - 6 * r * sin(ang(60)), r);
    circleMP(x + 2  * r * cos(ang(60)), y - 7 * r * sin(ang(60)), r);

    circleMP(x - 2 * r * cos(ang(60)), y - 1 * r * sin(ang(60)), r);
    circleMP(x - 6  * r * cos(ang(60)), y - 2 * r * sin(ang(60)), r);
    circleMP(x - 8  * r *  cos(ang(60)), y - 4 * r * sin(ang(60)), r);
    circleMP(x - 6  * r * cos(ang(60)), y - 6 * r * sin(ang(60)), r);
    circleMP(x - 2  * r * cos(ang(60)), y - 7 * r * sin(ang(60)), r);
}

void drawShape()
{
   myInit();
   drawcircles(300, 400, 20);
}


int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(300,150);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

    glutCreateWindow("Ring Ring Pattern");

    glutDisplayFunc(drawShape);

    glutMainLoop();
    return 0;
}
Output:

Ring Ring Pattern OpenGL

Previous
Next Post »