Home | | Syllabus | | Assignments | | Documentation

    Solution to Assignment 3, Problem 1


    /*
    Graphics Assignment 3
    Flower Transformations.
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <GL/glut.h>
    
    #define PI 3.14159
    
    
    void myinit(void)
    {
    	glClearColor(1.0, 1.0, 1.0, 1.0);
    	glColor3f(1.0, 0.0, 0.0);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluOrtho2D(-250.0, 250.0, -250.0, 250.0);
    	glMatrixMode(GL_MODELVIEW);
    }
    
    
    /*draw the flower */
    void display(void){
    	typedef GLfloat point2[2];
    	point2 squareVerts[4];
    	int i, j;
    	
    	GLfloat x=-1.0, y=-1.0;
    	GLfloat side = 2.0;
    	
    	glClear(GL_COLOR_BUFFER_BIT);
    	
    	/*Specify vertices for original square*/
    	squareVerts[0][0] = x;
    	squareVerts[0][1] = y;
    	squareVerts[1][0] = x + side;
    	squareVerts[1][1] = y;
    	squareVerts[2][0] = x + side;
    	squareVerts[2][1] = y + side;
    	squareVerts[3][0] = x;
    	squareVerts[3][1] = y + side;
    	
    	/*Draw small petals*/
    	for( j = 0; j < 12; j ++){
    		glPushMatrix();
    		glRotatef(30.0*j, 0.0, 0.0, 1.0);
    		glTranslatef(30*sqrt(2), 0.0, 0.0);
    		glScalef(30.0, 5.0, 1.0);
    		glRotatef(45.0, 0.0, 0.0, 1.0);	
    		glBegin(GL_LINE_LOOP);
    			for (i=0; i<4; i++){
    				glVertex2fv(squareVerts[i]);
    			}
    		glEnd();
    		glPopMatrix();
    	}
    	
    	/*Draw Big petals*/
    	for (j = 0; j < 12; j++){
    		glPushMatrix();
    		glRotatef(30.0*j, 0.0, 0.0, 1.0);
    		glTranslatef(40*sqrt(2), 0.0, 0.0);
    		glScalef(40.0, 10.0, 1.0);
    		glRotatef(45.0, 0.0, 0.0, 1.0);	
    		glBegin(GL_LINE_LOOP);
    		for (i=0; i<4; i++){
    				glVertex2fv(squareVerts[i]);
    		}
    		glEnd();
    		glPopMatrix();
    	}
    	
    	/*Draw boxes between petals*/
    	for (j = 0; j < 12; j++){
    		glPushMatrix();
    		glRotatef(30.0*j + 15.0, 0.0, 0.0, 1.0);
    		glTranslatef(100, 0.0, 0.0);
    		glScalef(10.0, 10.0, 1.0);
    		glBegin(GL_LINE_LOOP);
    		for (i=0; i<4; i++){
    				glVertex2fv(squareVerts[i]);
    		}
    		glEnd();
    		glPopMatrix();
    	}
    	
    	/*Draw tilted boxes between petals*/
    	for (j = 0; j < 12; j++){
    		glPushMatrix();
    		glRotatef(30.0*j + 15.0, 0.0, 0.0, 1.0);
    		glTranslatef(100, 0.0, 0.0);
    		glRotatef(45.0, 0.0, 0.0, 1.0);
    		glScalef(10.0/sqrt(2.0), 10.0/sqrt(2.0), 1.0);
    		glBegin(GL_LINE_LOOP);
    		for (i=0; i<4; i++){
    				glVertex2fv(squareVerts[i]);
    		}
    		glEnd();
    		glPopMatrix();
    	}
    	glFlush();
    
    }
    
    
    int main(int argc, char** argv)
    {	
    	glutInit(&argc, argv);
    	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    	glutInitWindowSize(500,500);
    	glutInitWindowPosition(50,50);
    	glutCreateWindow("Flower Transform"); 
    	glutDisplayFunc(display);
    	myinit();
    	glutMainLoop();
    	
    	return 0;
    }
    	
    	
    
    	
    
    	
    

    Home | | Syllabus | | Assignments | | Documentation


    Constance Royden--croyden@mathcs.holycross.edu
    Computer Science 384
    Date Created: August 17, 1999
    Last Modified: September 24, 2001
    Page Expires: August 17, 2002