Week 2 Assignment
Yasmin Elayat
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.
c. Calculate the length of a vector using Pythagorean theorem and return the resulting float.
|
header file cpp file
|
|
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.
|
void drawCircle(float rad, int numPoints, Vec2d center)
{
Vec2d P;
glBegin( GL_LINE_LOOP);
for(int i = 0; i < numPoints; i++) {
// 360 = 2*PI radians
float A = 2*PI / numPoints * i; //cos, sin takes radians
P.x = cos(A) * rad;
P.y = sin(A) * rad;
P = P + center; //vector math to perform a translation
glVertex2f(P.x,P.y);
}
glEnd();
}
.
.
.
int x = 200;
int y = 300;
for(int i = 1; i <= 5; i++) {
drawCircle((float)(10*i), 30, Vec2d(x, y)); //radius, num of points, center pt
x = x + (10*i) + (10*(i + 1)); //adjacent perimeters
}
|
 |
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.
|
int a = 50;
int b = 100;
for( int i = 3; i < 8; i++) {
drawCircle(50.0, i, Vec2d(a,b));
a+=150;
}
|
 |
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...).
|
void drawOval(float radH, float radV, int numPoints, Vec2d center)
{
Vec2d P;
glBegin( GL_LINE_LOOP);
for(int i = 0; i < numPoints; i++) {
// 360 = 2*PI radians
float A = 2*PI / numPoints * i; //cos, sin takes radians
P.x = cos(A) * radH;
P.y = sin(A) * radV;
P = P + center; //vector math to perform a translation
glVertex2f(P.x,P.y);
}
glEnd();
}
.
.
.
int x = 50;
int y = 200;
for( int i = 0; i < 10; i++) {
drawOval((float)(10.0*(i+1)), (float)(100.0 - (i*10.0)), 30, Vec2d(x,y));
x = x + (10*(i + 1)) + (10*(i + 2));
}
|
 |
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.
|
void drawStar(float radI, float radO, int numPoints, Vec2d center)
{
Vec2d P;
glBegin( GL_TRIANGLE_FAN);
for(int i = 0; i < numPoints; i++) {
// 360 = 2*PI radians
float A = 2*PI / numPoints * i;
if( i%2 == 0) {
P.x = cos(A) * radI;
P.y = sin(A) * radI;
} else {
P.x = cos(A) * radO;
P.y = sin(A) * radO;
}
P = P + center; //vector math to perform a translation
glVertex2f(P.x,P.y);
}
glEnd();
}
.
.
.
for(int i=0; i<100; i++) {
glColor3f( 0.3, 0.5, 0.6 );
drawStar(10.0, 15.0, 10 + rand() % 100, Vec2d( rand() % windowW , rand() % windowH));
}
|
 |
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 rad, int numPoints, Vec2d center)
{
Vec2d P;
glBegin( GL_TRIANGLE_FAN);
for(int i = 0; i < numPoints; i++) {
// 360 = 2*PI radians
float A = 2*PI / numPoints * i; //cos, sin takes radians
P.x = cos(A) * rad;
P.y = sin(A) * rad;
P = P + center; //vector math to perform a translation
//mouse will control opacity for a shimmer effect
if( i <= 5 ) glColor4f( 1.0, 1.0, 1.0, mouseX/windowW);
else glColor4f( 0.6, 0.6, 0.6, 1.0);
glVertex2f(P.x,P.y);
}
glEnd();
}
void drawNecklace()
{
int numPoints = 80;
int Rad1 = 250;
int Rad2 = 300;
Vec2d P;
Vec2d center = Vec2d(windowW/2,400);
for(int i = 1; i <= numPoints; i++) {
float A = 2*PI / numPoints * i;
P.x = cos(A) * Rad1;
P.y = sin(A) * Rad2;
P = P + center;
//draw pearls along an oval drawn using vector P
drawPearl(10.0, numPoints, P);
}
}
|
mouseX in the left side of screen:
mouseX in the right side of screen. |
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(float hr, float min, float sec)
{
Vec2d center(windowW/2, windowH/2);
drawCircle(200.0, 30, center);
drawPointCircle(180.0, 12, center);
float mlen = 180.0 - 10; //make longer
float hlen = 2*mlen/3;
float slen = mlen - 10; //make thinner
//for example:
//3:25:03
Vec2d hourHand, minHand, secHand;
glBegin( GL_LINES);
float A = 2*PI / 12 * convertTimeToDegree(hr); //hour
float B = 2*PI / 60 * convertSeconds(min); //min
float C = 2*PI / 60 * convertSeconds(sec); //sec
hourHand.x = cos(A) * hlen;
hourHand.y = sin(A) * hlen;
minHand.x = cos(B) * mlen;
minHand.y = sin(B) * mlen;
secHand.x = cos(C) * slen;
secHand.y = sin(C) * slen;
hourHand = hourHand + center;
glVertex2f(hourHand.x,hourHand.y);
glVertex2f(center.x, center.y);
minHand = minHand + center;
glVertex2f(minHand.x,minHand.y);
glVertex2f(center.x, center.y);
glColor3f(1.0,0,0);
secHand = secHand + center;
glVertex2f(secHand.x,secHand.y);
glVertex2f(center.x, center.y);
glEnd();
}
.
.
.
//test time 7:56:55
drawClock(7.0, 56.0, 55.0); //float hr, float min, float sec
|
 |
by Yasmin Elayat
|