Home | | Syllabus | | Assignments | | Documentation

    Solution to Assignment 1, Problem 3


    /*
     * CSCI 384, Problem set 1
     * Solution to Spiral problem
     * September 25, 2003
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <GL/glut.h>
    
    
    #define PI 3.14159
    
    /*Maximum vertices allowed*/
    #define MAX_VERTICES 100
    
    void myinit(void)
    {
    	glClearColor(1.0, 1.0, 1.0, 1.0);
    	glColor3f(1.0, 0.0, 0.0);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluOrtho2D(-500.0, 500.0, -500.0, 500.0);
    	glMatrixMode(GL_MODELVIEW);
    }
    
    void display(void)
    {
    	typedef GLfloat point2[2];
    	
    	point2 vertices[MAX_VERTICES];  /*Array of vertices*/
    	int numVertices = 100;  /* Number of vertices in spiral*/
    	int i,k;
    	
    	/*Initialize spiral parameters*/
    	double xcenter = 0.0, ycenter = 0.0;
    	double initRad = 20.0;
    	double alphaDeg = 35.0;
    	double initAlpha = 0.0;
    	double rInc = 2.0;
    	
    	/*Set up array of vertices*/
    	for (i = 0; i < numVertices; i ++){
    		vertices[i][0] = xcenter + 
    			(initRad + i * rInc) * cos((initAlpha +alphaDeg*i) * PI/180.0);
    		vertices[i][1] = ycenter + 
    			(initRad + i * rInc) * sin((initAlpha +alphaDeg*i) * PI/180.0);
    	}
    	
    	glClear(GL_COLOR_BUFFER_BIT);
    	
    	
    	/*Draw vertices*/	
    	glBegin(GL_LINES);
    		for( k= 0; k < numVertices; k++){
    			glVertex2fv(vertices[k]);
    		}
    	glEnd();
    	
    	
    	glFlush();
    }
    
    int main(int argc, char** argv)
    {	
    	glutInit(&argc, argv);
    	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    	glutInitWindowSize(500,500);
    	glutInitWindowPosition(50,50);
    	glutCreateWindow("Spirals"); 
    	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 17, 2001
    Page Expires: August 17, 2002