Week 4 Assignment
Yasmin Elayat
Problem 1. Create a function that takes as input a number of steps, and draws that many points, evenly spaced between the last two mouse clicks.
|
void stepsW2points( int steps )
{
Vec2d A(prevMouseX,prevMouseY);
Vec2d B(currMouseX,currMouseY);
glPointSize(5);
glColor3f(1.0, 0, 0);
steps = (steps < 1) ? 1: steps;
float dP = 1.0 / steps;
glBegin(GL_POINTS);
for(float p = 0; p <=1.000; p+=dP) {
Vec2d C = LERP(A,B,p);
glVertex2f(C.x,C.y);
}
glEnd();
}
|
 |
Problem 2. Create a function that takes as input a number of steps, and draws that many intermediate shapes between a start shape and an end shape. The start shape and end shape should be defined by an array of vectors (i.e points), and should have the same number of points. Extra credit for those who can make this work for two shapes with an unequal number of points.
|
Vec2d shape1[] = { Vec2d(50,50), Vec2d(50,200), Vec2d(100,200), Vec2d(200,50)};
Vec2d shape2[] = { Vec2d(550,450), Vec2d(450,600), Vec2d(600,600), Vec2d(800,450)};
Vec2d shape3[] = { Vec2d(150,150), Vec2d(150,200), Vec2d(100,200), Vec2d(200,50)};
void drawShape(Vec2d shape[])
{
glBegin(GL_LINE_LOOP);
for (int i=0; i <4; i++) {
glVertex2f(shape[i].x,shape[i].y);
}
glEnd();
}
void stepsWshapes(float steps)
{
drawShape(shape1);
drawShape(shape2);
for (int j=0; j < steps; j++) {
glBegin(GL_LINE_LOOP);
Vec2d tempShape[4];
for (int i=0; i<4; i++) {
tempShape[i] = LERP(shape1[i],shape2[i],j/steps);
glVertex2f(tempShape[i].x,tempShape[i].y);
}
glEnd();
}
}
|
 |
Problem 3.Create a function that takes four points (Vec2d) as input and draws a single Bezier curve based upon those four points.
|
void drawBezier(Vec2d pt1, Vec2d pt2, Vec2d pt3, Vec2d pt4)
{
int n = 3;
glBegin(GL_LINE_STRIP);
for(float p=0; p<=1.0;p+=0.01) {
float b = 1.0-p;
Vec2d R = pt1*pow(b,n) + pt2*(n*pow(p,(n-1))*b)
+ pt3*(n*pow(b,(n-1))*p) + pt4*pow(p,n);
glVertex2f(R.x, R.y);
}
glEnd();
}
|
 |
Problem 4. Create a function that takes as input an array of points (Vec2d *) and the number of points in that array (int), and draws a shape composed of several consecutive Bezier curves.
|
void drawBezierShapes(Vec2d points[], int len)
{
//assumption2: we are drawing consecutive quadratic bezier curves.
for(int i = 0; i < len; i=i+4) {
drawBezier(points[i], points[i+1], points[i+2], points[i+3]);
}
}
|
 |
Problem 5. Create a function that draws a head of hair composed of 500 bezier curves. Extra credit for highlights.
|
void drawHair(Vec2d points[])
{
for(int i=0; i < 500; i++){
Vec2d follicle [] = {Vec2d(points[0].x - i,points[0].y + i),
Vec2d(points[1].x - i,points[1].y + i),
Vec2d(points[2].x - i,points[2].y + i),
Vec2d(points[3].x - i,points[3].y + i),
Vec2d(points[4].x + i,points[4].y + i),
Vec2d(points[5].x + i,points[5].y + i),
Vec2d(points[6].x + i,points[6].y + i),
Vec2d(points[7].x + i,points[7].y + i)};
drawBezierShapes(follicle,8);
}
}
|
 |
|