Circle Generation Algorithm using Parametric Equation

This is a simple OpenGL Program to generate a circle using the parametric equation. The GLUT toolkit is used to make things easier.

Parametric equation for circle is

     \[\mathbf{x}=\mathbf{r}.cos(\theta) \quad , \quad \mathbf{y}=\mathbf{r}.sin(\theta)\]

The algorithm can be written as

 for \( \:\theta \in[0,2\pi] \) \\ x=x_{c}+r.cos(\theta) \\ y=y_{c}+r.sin(\theta) \\

where x_c and y_c are coordinates of the center of the circle.
The point P(x,y) is plotted. After the loop terminates, a circle is generated.

The code for this is shown below

// Parametric Circle Algorithm in OpenGL
//Written by Shreyak Chakraborty

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

using namespace std;

void display(void)  //Draw circle
{
  float r,x,y,cx,cy;
  float pi=3.1415,dtheta=0.01,theta;
  
  r=100.0;
  
  //coordinates of center of circle
  cx=200;
  cy=100;
  
  glColor3f(1.0,1.0,0.0);        /* set current color */

  glBegin(GL_POINTS); 
  
  theta=0.0;
  
  while(theta<=2*pi)  //vary angle from 0 to 2pi radians
  {
     glVertex2f(cx+r*cos(theta),cx+r*sin(theta));
	 theta+=dtheta;
  }

  glEnd();
  glFlush();    /* Complete any pending operations */

  std::cout<<"done";
}


int main(int argc, char *argv[])
{
  int win;

  glutInit(&argc, argv);        /* initialize GLUT system */

  glutInitDisplayMode(GLUT_RGB);
  glutInitWindowSize(500,500);        /* width=400pixels height=500pixels */
  win = glutCreateWindow("MCS-051 Study");    /* create window */

  /* from this point on the current window is win */

  glClearColor(0.0,0.0,0.0,0.0);    /* set background to black */
  gluOrtho2D(0,500,0,500);        /* how object is mapped to window */
  glClear(GL_COLOR_BUFFER_BIT);        /* clear the display */



  glutDisplayFunc(display);        /* set window's display callback */

  glutMainLoop();            /* start processing events... */

  /* execution never reaches this point */

  return 0;
}

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *