Week 2 Assignment
Matt Parker
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.
|
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;
}
float Vec2d::length()
{
float a = x*x + y*y;
return sqrt(a);
}
|
|
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.
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.
|
|
Full Source:
a2p2.cpp
Shape.h
Shape.cpp
|
void drawCircle(float radius, int numOfPts, Vec2d center){
Circle circ(radius, numOfPts, center);
circ.draw();
}
void displayFunc ( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLineWidth(3);
glColor3f( 1,0,0 );
glRectf( mouseX-5, mouseY-5, mouseX+5, mouseY+5 );
// place your drawing code here
Vec2d center(0,250);
int vector = 0;
for(int i = 10; i <= 50; i = i + 10){
center.x = i + vector;
drawCircle(i,30,center);
vector = i*2 + vector;
}
for(int i = 3; i <= 8; i++){
center.x += 50 * 2;
drawCircle(50, i, center);
vector = 50 + vector;
}
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...).
|
|
Full Source:
a2p3.h
a2p3.cpp
|
void drawCircle(float radius, int numOfPts, Vec2d center){
Circle circ(radius, numOfPts, center);
circ.draw();
}
void drawOval(float vRadius, float hRadius, int numOfPts, Vec2d center){
Oval ov(vRadius, hRadius, numOfPts, center);
ov.draw();
}
void displayFunc ( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLineWidth(3);
glColor3f( 1,0,0 );
glRectf( mouseX-5, mouseY-5, mouseX+5, mouseY+5 );
// place your drawing code here
int ovalVert = 100;
int incr = 10;
Vec2d center(10,400);
for(int vert = ovalVert; vert > 0; vert -= incr){
int horz = ovalVert + incr - vert;
drawOval(horz, vert, 30, center);
printf("x:%i y:%i\n", horz, vert);
center.x += horz * 2 + incr;
}
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.
|
|
Full Source:
a2p4.h
a2p4.cpp
|
void drawCircle(float radius, int numOfPts, Vec2d center){
Circle circ(radius, numOfPts, center);
circ.draw();
}
void drawOval(float vRadius, float hRadius, int numOfPts, Vec2d center){
Oval ov(vRadius, hRadius, numOfPts, center);
ov.draw();
}
void drawStar(float innerRadius, float outerRadius, int numOfPts, Vec2d center){
Star star(innerRadius, outerRadius, numOfPts, center);
star.draw(GL_LINE_LOOP);
}
void drawScreen() {
if (mouseClick) {
int numOfStars = 100;
Vec2d center(10, 400);
for (int starNum = 0; starNum < numOfStars; starNum++) {
glColor3f(((float)(rand() % 10)) * .1, ((float)(rand() % 10)) * .1,
((float)(rand() % 10)) * .1);
int inner = rand() % 50;
int outer = rand() % 50;
center.x = rand() % windowW;
center.y = rand() % windowW;
drawStar(inner, outer, rand() % 30+ 10, center);
}
}
}
|
 |
|
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.
|
|
Full Source:
a2p5.cpp
|
void drawNecklace(){
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// place your drawing code here
Vec2d center(450, 350);
int numOfPts = 50;
if (vecArray == NULL) {
Oval oval(235, 200, numOfPts, center);
vecArray = oval.getVecs();
}
for (int i = 0; i < numOfPts; i++) {
for (float c = 4; c < 20; c = c + .1) {
glColor3f((float)c/11.5, (float)c/12.5, (float)c/12.5);
Vec2d vec(vecArray[i].x, vecArray[i].y - c);
Circle circ(20 - c, 20, vec);//vecArray[i]);
circ.draw();
}
}
for (int i = 0; i < numOfPts; i++) {
for (float c = 4; c < 5; c = c + .1) {
glColor3f((float)c/4.5, (float)c/4.5, (float)c/12.5);
Vec2d vec(vecArray[i].x, vecArray[i].y - 15);
Star star(6 - c + mouseX/75, 2 - c, 10, vec);
star.draw(GL_LINE_LOOP);
}
}
redraw = false;
}
|
 |
|
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. |
|
Full Source:
a2p6.cpp
|
int shiftHour(float min){
int result = 15 - (int)min;
if(result > 12){
result -= 12;
}
return result;
}
int shiftMin(float min){
int result = 75 - (int)min;
if(result >= 60){
result -= 60;
}
return result;
}
float eyeDir = -0.25;
float eyePos = 0;
float shiftEye(float eyePos){
if((eyePos > 15) || (eyePos < -15)){
eyeDir = eyeDir * -1;
}
return eyePos + eyeDir;
}
void drawClock(float hour, float min, float second){
eyePos = shiftEye(eyePos);
float minScale = 40;
float hourScale = 30;
float secScale = 50;
Vec2d center(440, 400);
glColor3f(1,0.75,0.5);
Circle head(150, 90, center);
head.draw(GL_TRIANGLE_FAN, true, true);
center.x = 398;
center.y = 450;
glColor3f(1,1,1);
Circle leftEye(50, 50, center);
leftEye.draw(GL_TRIANGLE_FAN, true, true);
glColor3f(0,0,0);
center.x = center.x + eyePos;
Oval leftEyeBall(15, 20, 50, center);
leftEyeBall.draw(GL_TRIANGLE_FAN, true, true);
center.x = 477;
glColor3f(1,1,1);
Circle rightEye(50, 50, center);
rightEye.draw(GL_TRIANGLE_FAN, true, true);
glColor3f(0,0,0);
center.x = center.x + eyePos;
Oval rightEyeBall(15, 20, 50, center);
rightEyeBall.draw(GL_TRIANGLE_FAN, true, true);
glColor3f(1,0.5,0.5);
center.x = 440;
center.y = 400;
Oval nose(30, 20, 30, center);
nose.draw(GL_TRIANGLE_FAN, true, true);
glColor3f(1,1,1);
center.x = 440;
center.y = 320;
Circle mouth(60, 60, center);
mouth.draw(GL_TRIANGLE_FAN, true, true);
glColor3f(1,0,0);
Star star(60, minScale, 23, center);
star.draw(GL_TRIANGLE_FAN, true, true);
glColor3f(0,1,0);
Circle minCircle(minScale, 60, center);
vecArray = minCircle.getVecs();
glLineWidth(3);
glBegin(GL_LINE_STRIP);
glVertex2f(center.x, center.y);
glVertex2f(vecArray[shiftMin(min)].x, vecArray[shiftMin(min)].y);
glEnd();
Circle secCircle(secScale, 60, center);
vecArray = secCircle.getVecs();
glBegin(GL_LINE_STRIP);
glVertex2f(center.x, center.y);
glVertex2f(vecArray[shiftMin(second)].x, vecArray[shiftMin(second)].y);
glEnd();
Circle hourCircle(hourScale, 12, center);
vecArray = hourCircle.getVecs();
glBegin(GL_LINE_STRIP);
glVertex2f(center.x, center.y);
glVertex2f(vecArray[shiftHour(hour)].x, vecArray[shiftHour(hour)].y);
glEnd();
}
float hour, min, sec = 0;
void displayFunc(void) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// place your drawing code here
if(sec > 60){
min = min + 1;
sec = 0;
}
if(min > 60){
hour = hour + 1;
min = 0;
}
if(hour > 12){
hour = 1;
}
drawClock(hour, min, sec);
sec = sec + 0.01;
glutPostRedisplay();
glutSwapBuffers();
redraw = false;
}
|
 |
|