#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
#include<math.h>
const int screenWidth = 480;
const int screenHeight = 480;
const float superEllipseWidth = 100;
const float superEllipseHeight = 50;
const float superEllipseBulge = 1.5; // OK for (0,2] only!

GLdouble xFcn(float t);
GLdouble yFcn(float t);

void myInit(void)
{
  glClearColor(1.0,1.0,1.0,0.0); // background
  glColor3f(0.0, 0.0, 0.0);   // drawing color
  glPointSize(1.0);              // pointsize
  glMatrixMode(GL_PROJECTION);   // random necessary wierdness
  glLoadIdentity();                    
  gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}


GLdouble xFcn(float t) 
{
  float cost = cos(t);
  if (cost == 0.0)
    return cost;
  else
    return ( superEllipseWidth * cost
	     * (pow
		(fabs (cost), (2/superEllipseBulge)-1)));
}

GLdouble yFcn(float t) 
{
  float sint = sin(t);
  if (sint == 0.0)
    return sint;
  else
    return ( superEllipseHeight * sint
	     * (pow
		(fabs (sint), (2/superEllipseBulge)-1)));
}

void myDisplay(void)
{
  int N = 100;
  int xOffset = 300;
  int yOffset = 200;
  float timePointArray[N];
  for (int i=0; i<N; i++)
    {
      timePointArray[i] = (2* M_PI * i)/N;
    }
  glClear(GL_COLOR_BUFFER_BIT);  // clear the screen
  glBegin(GL_LINE_LOOP);
  for (int i=0; i<N; i++)
    glVertex2f(xOffset + xFcn(timePointArray[i]), 
	       yOffset + yFcn(timePointArray[i]));
  glEnd();

  glFlush();
}

void main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(screenWidth, screenHeight);
  glutInitWindowPosition(100, 150);
  glutCreateWindow("parametric superellipse");
  glutDisplayFunc(myDisplay);
  myInit();
  glutMainLoop();
}

