January 17, 2014

Rainbow - part 4

Improved on the previous by modifying my code to draw ellipses instead of circles.

#define PI 3.1415926535897932384626433832795

float colors[] =
{1.0, 0.0, 0.0,
1.0, 0.5, 0.0,
1.0, 1.0, 0.0,
 0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 1.0};

typedef struct
{
  float x;
  float y;
} ELLIPSE;

int RED = 0, ORANGE = 3, YELLOW = 6, GREEN = 9, BLUE = 12, PURPLE = 15;

void createEllipse(float a, float b, int color, float yPos, float thickness) {
  ELLIPSE ellipse;
  glLineWidth(thickness);

  glBegin(GL_LINES);
  glColor3f(colors[color], colors[color + 1], colors[color + 2]);

    for (float i = 0; i < PI; i+=.1) {
      ellipse.x = a * cos(i);
      ellipse.y = b * sin(i);
      glVertex3f(ellipse.x, ellipse.y + yPos, 0);

      ellipse.x = a * cos(i + 0.1);
      ellipse.y = b * sin(i + 0.1);
      glVertex3f(ellipse.x, ellipse.y + yPos, 0);
    }
    glEnd();
}

void display()
{
  glClearColor(1,1,1,1);
  glClear(GL_COLOR_BUFFER_BIT);

  float thickness = 7, b = 1, a = 1.33, y = -0.1;
  float yEpsilon = .05, aEpsilon = .01;

  createEllipse(a, b, RED, y, thickness);
  createEllipse(a - aEpsilon, b, ORANGE, y - yEpsilon, thickness);
  createEllipse(a - 2*aEpsilon, b, YELLOW, y - 2*yEpsilon, thickness);
  createEllipse(a - 3*aEpsilon, b, GREEN, y - 3*yEpsilon, thickness);
  createEllipse(a - 4*aEpsilon, b, BLUE, y - 4*yEpsilon, thickness);
  createEllipse(a - 5*aEpsilon, b, PURPLE, y - 5*yEpsilon, thickness);

  glutSwapBuffers();
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
  glutInitWindowSize(600,600);

  glutCreateWindow("not a rainbow but i can still pretend");
  glutDisplayFunc(display);

  glutMainLoop();
}

Looking more like a rainbow now!



Yay!