Week 2 Assignment - Computational Forms

Lucas Longo

 

Problem 1. Complete the Vec2d class. Add the following methods to the class:
a. Subtract one vector from another and return the resulting vector.
b. Divide a vector by a float and return the resulting vector.

Vec2d operator-(Vec2d B);
Vec2d operator/(float Scalar);

In Vec2d.h

Vec2d Vec2d::operator-(Vec2d B)
{
Vec2d C;
C.x = x - B.x;
C.y = y - B.y;
return C;
}

Vec2d Vec2d::operator*(float M)
{
Vec2d C;
C.x = x * M;
C.y = y * M;
return C;
}

Vec2d Vec2d::operator/(float M)
{
Vec2d C;
C.x = x / M;
C.y = y / M;
return C;
}

In Vec2d.cpp
c. Calculate the length of a vector using Pythagorean theorem and return the resulting float.
float vLength(Vec2d A){
float C = sqrt(pow(A.x,2)+pow(A.y,2));
return C;
}
In main.cpp

Problem 2. Create a function called drawCircle that takes as arguments a radius (float), a number of points (int) and a center point (Vec2d) and draws a circle as specified. Demonstrate that this function works by using it in the following ways:

a. Draw 5 circles in a row, each with increasing radii (10, 20, 30...), just touching each other, each composed of 30 points.

//Draw circles
void drawCircle(float radius, int numPoints, Vec2d center)
{
Vec2d S(center);

glBegin(GL_LINE_LOOP);
for(int i = 0; i < numPoints; i++){
float A = 2 * PI / numPoints * i;

S.x = cos(A)*radius + center.x;
S.y = sin(A)*radius + center.y;

glVertex2f(S.x, S.y);
}

S = S + center;

glEnd();

}

void displayFunc ( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glColor3f( 1,0,0 );

int numPoints = 30;
float radius;
Vec2d center(20,windowH/2);

for (int i = 0; i < 6; i++){
radius = 10*i;
center.x = i*radius + 10;
drawCircle(radius, numPoints, center);
}
glutPostRedisplay();
glutSwapBuffers();
}

b. Draw 5 regular polygons in a row, each with an increasing number of points (3, 4, 5...), each with a radius of 50 pixels.

//Returns point on a circle with numPoints
Vec2d pointOnCircle(float radius, int numPoints, Vec2d center, int i)
{
Vec2d S(center);

float A = 2 * PI / numPoints * i;

S.x = cos(A)*radius;
S.y = sin(A)*radius;

S = S + center;

return S;
}

void displayFunc ( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3f( 1,0,0 );

int numPoints;
float radius;
float radiusPosition = 200;
Vec2d center;

//n Polygons arranged in circle
int numShapes = mouseX;

for (int i = 0; i < numShapes; i++){
numPoints = i + 3;
radius = 50;
center = pointOnCircle(radiusPosition, numShapes, Vec2d(windowW/2,windowH/2), i);
drawCircle(radius, numPoints, center);
}

glutPostRedisplay();
glutSwapBuffers();
}

Problem 3. Create a function called drawOval that takes as arguments a horizontal radius (float), a vertical radius, a number of points (int) and a center point (Vec2d) and draws an oval as specified. Demonstrate that this function works by drawing 10 ovals in a row, each with an increasing horizontal radius (10, 20, 30...) and decreasing vertical radius (100, 90, 80...).

//10 ovals with varying height and width
void drawOval(float hor, float ver, Vec2d center)
{
Vec2d S(center);

glBegin(GL_LINE_LOOP);
for(int i = 0; i < 40; i++){
float A = 2 * PI / 40 * i;

S.x = cos(A)*hor + center.x;
S.y = sin(A)*ver + center.y;

glVertex2f(S.x, S.y);
}

S = S + center;

glEnd();
}

void displayFunc ( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3f( 1,0,0 );

float radiusH;
float radiusW;
Vec2d center(100,windowH/2);

for (int h = 1; h < 11; h++){
radiusW = 5*h;
radiusH = 100 - h*5;
Vec2d(windowW/2,windowH/2), h);
center.x += radiusW * 2;
drawOval(radiusW, radiusH, center);
}

glutPostRedisplay();
glutSwapBuffers();
}

Problem 4. Create a function called drawStar that takes as arguments an inner radius (float), an outer radius (float), a number of points (int), and a center point (Vec2d) and draws a star as specified. Demonstrate that this function works by drawing 100 stars with a varying number of points placed randomly about the screen. Feel free to change the background color for a more dramatic effect.

//Draw stars with random number of points randomly on the screen
void drawStar(float radiusIn, float radiusOut, int tips, Vec2d center)
{
Vec2d P;
glBegin(GL_LINE_LOOP);
int numPoints = tips*2;

for(int i = 0; i < numPoints; i++){
float A = 2 * PI / numPoints * i;

if (i%2==0){
P.x = cos(A)*radiusIn;
P.y = sin(A)*radiusIn;
}
else{
P.x = cos(A)*radiusOut;
P.y = sin(A)*radiusOut;
}

P = P + center;

glVertex2f(P.x,P.y);
}
glEnd();

}

void displayFunc ( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3f( 1,0,0 );

for (int i = 0; i < 101; i++){
drawStar(radiusIn,radiusOut,numPoints,center);
}

glutPostRedisplay();
glutSwapBuffers();
}

void init ( GLvoid )
{
glShadeModel( GL_SMOOTH );
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glEnable ( GL_COLOR_MATERIAL );
glEnable( GL_BLEND );
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

for (int i = 0; i < 100; i++){
center[i] = Vec2d(rand()%windowW,rand()%windowH);
numPoints [i] = 5+rand()%10;
}
}

Problem 5. Create a function called drawNecklace that draws a pearl necklace. Feel free to shade the pearls or make them sparkle in the light of the moving mouse.

void drawPearl(float radius, int numPoints, Vec2d center)
{
Vec2d S(center);

float cX = mouseX/windowW;
float cY = mouseY/windowH;

glColor4f(cX, 1, cY, 1);

glBegin(GL_TRIANGLE_FAN);
glVertex2f(center.x, center.y);

for(int i = 0; i < numPoints+1; i++){
float A = 2 * PI / numPoints * i;

if (i%50 == 0){
glColor4f(cX, 1, cY, 1);
}
else {
glColor4f(cY, cX, 1, 1);
}

S.x = cos(A)*radius + center.x;
S.y = sin(A)*radius + center.y;

glVertex2f(S.x, S.y);
}
glVertex2f(center.x, center.y);

S = S + center;

glEnd();

}

void displayFunc ( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3f( 1,1,0 );

float radius = 50;
int numShapes = 10;
float radiusPosition = numShapes*radius/PI;

float radiusH;
float radiusW;

Vec2d center;

float numPoints = 100;

for (int i = 0; i < numShapes; i++){
center = pointOnCircle(radiusPosition, numShapes, Vec2d(windowW/2,windowH/2), i);
drawPearl(radius, numPoints, center);
}

glutPostRedisplay();
glutSwapBuffers();
}



Problem 6. Create a function called drawClock that takes as arguments an hour (float), a minute (float) and a number of seconds (float), and draws an analog clock. Feel free to add a pendulum or any other fancy clock accoutrements.

void drawClock(int hour, int min, int sec)
{
Vec2d center(windowW/2,windowH/2);
Vec2d hour_tip;
Vec2d min_tip;
Vec2d sec_tip;

float radius_clock = 200;
float hour_len = radius_clock*(1-0.45);
float min_len = radius_clock*(1-0.20);
float sec_len = radius_clock*(1-0.05);

//Clock
for (int i = 0; i < 5; i+=4){
glColor4f(1, 1, 1, 1);
drawCircle(radius_clock+i, 300, center);
glColor4f(0, 0, 1, 1);
drawCircle(radius_clock+i+1, 300, center);
}

//Hour/min lines
glColor4f(1, 1, 1, 0.5);
glBegin(GL_LINES);
glColor4f(1, 0, 0, 1);

for (int i = 0; i < 60; i += 5){
if (i%15==0){
min_tip = pointOnCircle(sec_len-10, 60, Vec2d(windowW/2,windowH/2),i+1);
glVertex2f(min_tip.x, min_tip.y);
min_tip = pointOnCircle(sec_len-10, 60, Vec2d(windowW/2,windowH/2),i-1);
glVertex2f(min_tip.x, min_tip.y);
}
min_tip = pointOnCircle(sec_len-10, 60, Vec2d(windowW/2,windowH/2),i);
glVertex2f(min_tip.x, min_tip.y);
sec_tip = pointOnCircle(sec_len, 60, Vec2d(windowW/2,windowH/2),i);
glVertex2f(sec_tip.x, sec_tip.y);
}
glEnd();

//Hour hand
glColor4f(1, 1, 1, 1);
glBegin(GL_LINES);
glVertex2f(windowW/2,windowH/2);
hour_tip = pointOnCircle(hour_len, 12, Vec2d(windowW/2,windowH/2), 12-hour+15);
glVertex2f(hour_tip.x, hour_tip.y);
glEnd();
drawCircle(radius_clock/40,300,hour_tip);


//Min hand
glBegin(GL_LINES);
glVertex2f(windowW/2,windowH/2);
min_tip = pointOnCircle(min_len, 60, Vec2d(windowW/2,windowH/2), 60-min+15);
glVertex2f(min_tip.x, min_tip.y);
glEnd();

//Sec hand
glColor4f(0, 0, 1, 1);
drawCircle(radius_clock/30,300,center);
drawCircle(radius_clock/15,300,center);
glBegin(GL_LINES);
glVertex2f(windowW/2,windowH/2);
sec_tip = pointOnCircle(sec_len, 60, Vec2d(windowW/2,windowH/2), 60-sec+15);
glVertex2f(sec_tip.x, sec_tip.y);
glEnd();

}

void displayFunc ( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3f( 1,1,0 );

float radius = 50;
int numShapes = 10;

time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );

mktime(timeinfo);

int hour = timeinfo->tm_hour;
int min = timeinfo->tm_min;
int sec = timeinfo->tm_sec;

drawClock(hour, min, sec);

glutPostRedisplay();
glutSwapBuffers();
}