// for Reid's 50th
// $Header: /Users/jch/zStuff/cvsstuff/cvsroot/trivo/spiral.c,v 1.2 2005/12/23 20:04:29 jch Exp $
// Copyright 2003 YON - Jan C. Hardenbergh. Permission to copy is
// granted as long as this copyright and this URL are maintained:
// http://www.jch.com/NURBS/
//
#include <GL/glut.h>
#include <stdlib.h>



double startAngle = 0;
double theAngle = 0;
double deltaAngle = 0.1;
double theRadius = 0;
double startRadius = .9;
double deltaRadius = 0.2;
double theNSteps = 72;
int thePhase = 0;


#define PIE 3.1415927

void drawArc(double center[2], double angles[2], double radii[2])
{
  GLfloat pt[2];
  double dAngle = 0.1;
  double angle;
  double radius;
  double dRadius;
  int steps, i;
  double startAngle;
  double endAngle;

  startAngle = angles[0];
  endAngle = angles[1];

  // while (startAngle > 2*PIE) startAngle -= 2*PIE;
  // while (startAngle > 2*PIE) startAngle -= 2*PIE;
  while (endAngle < startAngle) endAngle += 2*PIE;

  steps = (int)(fabs(endAngle - startAngle)/dAngle);
  dAngle = (endAngle - startAngle)/(double)(steps);
  dRadius = (radii[1] - radii[0])/(double)(steps);

  radius = radii[0];
  angle = startAngle;

  glColor3f(0.2,1.0,0.3);
  glLineWidth(2.0);
  glBegin(GL_LINE_STRIP);
	
  for(i = 0; i <= steps; i++)
    {
      pt[0] = (GLfloat)(center[0] + radius*cos(angle));
      pt[1] = (GLfloat)(center[1] + radius*sin(angle));
      glVertex2fv(pt);
      radius += dRadius;
      angle += dAngle;
    }
  glEnd();
}

void init(void) 
{
  glClearColor (1.0, 1.0, 1.0, 0.0);
  glShadeModel (GL_FLAT);
  // init 
}

void display(void)
{
  int i, j;
  double center[2] = {0,0};
  double fullCircle[2];
  double radii[2];
  int years = 50;
  double deltaRad;
  int ii;

  // Clear the window with current clearing color
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  fullCircle[0] = 0;
  fullCircle[1] = 2*PIE;

  deltaRad = .85/years;
  radii[0] = 0.9;
  radii[1] = radii[0] - deltaRad;

  for (ii = 0; ii < years; ii++)
    {
      drawArc(center, fullCircle, radii);
      radii[0] = radii[1];
      radii[1] -= deltaRad;
    }

  // Flush drawing commands
  glFlush();
}

void reshape (int w, int h)
{
  double wide, high;
  // Prevent a divide by zero
  if(h == 0)
    h = 1;

  glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  wide = (double)w;
  high = (double)h;
  if (w > h)
    {
      wide = wide/high;
      high = 1;
    }
  else
    {
      high = high/wide;
      wide = 1;
    }
  glOrtho(-wide, wide, -high, high, -1, 1);

  glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
   }
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (500, 500); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}
