Friday 15 May 2015

OpenGL : Making Spirals


What is a spiral ? 
A curve on a plane that winds around a fixed center point at a continuously increasing or decreasing distance from the point.


two-dimensional spiral may be described most easily using polar coordinates, where the radius r is a monotonic continuous function of angle θ. The circle would be regarded as a degenerate case (the function not being strictly monotonic, but rather constant).
Some of the more important sorts of two-dimensional spirals include:

How to draw it using OpenGL?
Prerequisites : Running a basic OpenGL code.
Making spiral in OpenGL is quite simple because we have the mathematical equations for both the x and y coordinates of the Spiral.
In this example we will go with the logarithmic and the archimedean spiral.

Snapshot of completed code :


Coordinates for logarithmic spiral 


Code-Snippet in C++ (OpenGL) 
Here a and b are the constants of the equation. You can call the function inside you display in you OpenGL code. Download the complete code from this link.


void DrawSpiral(double a,double b)
{
 double x, y;
 
 theta = 0;
 x = a*pow(2.718281,(double)b*theta)*cos(theta);
 y = a*pow(2.718281,(double)b*theta)*sin(theta);
 for (int i = 0; i < 1000; i++)
 {
  glBegin(GL_LINES);
  theta = 0.025*i;
  glVertex2f((GLfloat)x, (GLfloat)y);
  glVertex2f(a*pow(2.718281, (double)b*theta)*cos(theta), a*pow(2.718281, (double)b*theta)*sin(theta));
 
  //glColor3f(r, g, b);
  x = a*pow(2.718281, (double)b*theta)*cos(theta);
  y = a*pow(2.718281, (double)b*theta)*sin(theta);
  
  glEnd();
 }
 
}



1 comment: