Skip to content

Instantly share code, notes, and snippets.

@iCHAIT
Created December 18, 2015 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iCHAIT/ae3a519ae1817dc88eec to your computer and use it in GitHub Desktop.
Save iCHAIT/ae3a519ae1817dc88eec to your computer and use it in GitHub Desktop.
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
#include <math.h>
#include <stdio.h>
float x1=-2.0, x2=2.0;
float x = 0.7, y = 0.7;
void keyPressed(int key, int x1, int y1)
{
if(key==GLUT_KEY_UP)
{
x *= 1.35;
y *= 1.35;
glutPostRedisplay();
}
if(key==GLUT_KEY_DOWN)
{
x *= 0.95;
y *= 0.95;
glutPostRedisplay();
}
}
void drawSquare(float x, float y) {
glColor3f(0.8, 0.7, 0.7);
glBegin(GL_LINES);
glVertex2f(x, 0.0);
glVertex2f(0.0, -y);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.0, -y);
glVertex2f(-x, 0.0);
glEnd();
glBegin(GL_LINES);
glVertex2f(-x, 0.0);
glVertex2f(0.0, y);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.0, y);
glVertex2f(x, 0.0);
glEnd();
}
void drawRhombus(float x, float y) {
glColor3f(0.2, 0.6, 0.3);
glBegin(GL_LINES);
glVertex2f(x, y);
glVertex2f(x, -y);
glEnd();
glBegin(GL_LINES);
glVertex2f(x, -y);
glVertex2f(-x, -y);
glEnd();
glBegin(GL_LINES);
glVertex2f(-x, -y);
glVertex2f(-x, y);
glEnd();
glBegin(GL_LINES);
glVertex2f(-x, y);
glVertex2f(x, y);
glEnd();
}
void drawShape(float x, float y){
int k = 1;
while(x >= (0.0125)) {
if(k % 2)
{
drawRhombus(x, y);
}
else
{
drawSquare(x, y);
x = x/2;
y = y/2;
}
k++;
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawShape(x, y);
glutSwapBuffers();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("Animation of Squares");
glClearColor(0.25, 0.25, 0.25, 0.3);
glutDisplayFunc(display);
glutIdleFunc(display);
glutSpecialFunc(keyPressed);
glutMainLoop();
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment