Week 5 Assignment
Yasmin Elayat
Problem 1. Write a program that allows the user to draw a stroke of up to 1000 points. Optionally, implement a feature that prevents the addition of points that are closer than 5 pixels from the last point added.
|
void drawAStroke(float strokeWidth=5){
glLineWidth(1);
//float strokeWidth=5;
glBegin( GL_TRIANGLE_STRIP );
for(int i =0; i < numPoints - 1; i++)
{
Vec2d currentPoint = pointList[i];
Vec2d A = pointList[i];
Vec2d B = pointList[i+1];
//get vector between the previous and next points
Vec2d V = B - A;
V.normalize();
Vec2d Vp;
//get perpindicular vector
Vp.x = V.y * -1;
Vp.y = V.x;
Vec2d C = currentPoint + Vp*(strokeWidth);
Vec2d D = currentPoint - Vp*(strokeWidth);
//regular stroke
glVertex2f(C.x, C.y);
glVertex2f(D.x, D.y);
}
glEnd();
}
|
 |
Problem 2. Create a function that draws the stroke (from Problem 1) using the special wide-stroke method. The function should take as input the desired with of the stroke as a float.
|
void drawstrokes(float strokeWidth) {
drawAStroke(strokeWidth);
}
drawstrokes(30);
|
 |
Problem 3. Create a function that draws the stroke with a three-step stair-step pattern along it on just one side of the stroke.
|
void drawStepStroke() {
glLineWidth(1); //3);
float strokeWidth=20;
glBegin( GL_LINE_STRIP );
for(int i =1; i < numPoints - 1; i++)
{
Vec2d currentPoint = pointList[i];
Vec2d A = pointList[i-1];//[i];
Vec2d B = pointList[i+1];
//get vector between the previous and next points
Vec2d V = B - A;
Vec2d V2 = V/3;
V.normalize();
Vec2d Vp;
Vec2d Vp2;
Vec2d Vp3;
//get perpindicular vector
Vp.x = V.y * -1;
Vp.y = V.x;
Vp2.x = V2.y * -1;
Vp2.y = V2.x;
Vec2d C = currentPoint + Vp*(strokeWidth);
Vec2d E = currentPoint + Vp2*(strokeWidth);
//regular stroke
glVertex2f(C.x, C.y);
glVertex2f(E.x, E.y);
}
glEnd();
}
|
 |
Problem 4. Create a function that the draws the stroke with a wave that runs perpendicular to the direction of the stroke. The wave should affect the path of the stroke, not its width. The function should take an amplitude and frequency as input
|
void drawWaveStroke(float amp=10, float freq=20)
{
glLineWidth(1);
float strokeWidth=5;
glBegin( GL_TRIANGLE_STRIP );
for(int i =0; i < numPoints - 1; i++)
{
Vec2d currentPoint = pointList[i];
Vec2d A = pointList[i];
Vec2d B = pointList[i+1];
//get vector between the previous and next points
Vec2d V = B - A;
V.normalize();
Vec2d Vp;
//get perpindicular vector
Vp.x = V.y * -1;
Vp.y = V.x;
Vec2d C;
C.x = currentPoint.x + Vp.x*(strokeWidth);
C.y = currentPoint.y + Vp.y*(strokeWidth) * amp * dsin(i*freq);
glVertex2f(C.x, C.y);
}
glEnd();
}
|
 |
Problem 5. Create a function that the draws the stroke with a giant arrowhead at its tip. The arrowhead should be in alignment the last stroke segment. Optionally, draw a series of similar but smaller arrows along the length of the whole stroke.
|
void drawArrowStroke() {
glLineWidth(1); //3);
float strokeWidth=50;
glBegin( GL_LINES );
for(int i =2; i < numPoints - 2; i++)
{
Vec2d currentPoint = pointList[i];
Vec2d A = pointList[i-2];
Vec2d B = pointList[i+2];
//get vector between the previous and next points
Vec2d V = B - A;
V.normalize();
Vec2d Vp;
//get perpindicular vector
Vp.x = V.y * -1;
Vp.y = V.x;
Vec2d C = currentPoint + Vp*(strokeWidth);
Vec2d D = currentPoint - Vp*(strokeWidth);
//current point + perp comp + parallel comp
Vec2d E = currentPoint + Vp*(strokeWidth) - V*(strokeWidth);
Vec2d F = currentPoint - Vp*(strokeWidth) - V*(strokeWidth);
//drawing an arrow bristle brush stroke
glVertex2f(E.x, E.y);
glVertex2f(currentPoint.x, currentPoint.y);
glVertex2f(F.x, F.y);
glVertex2f(currentPoint.x, currentPoint.y);
}
glEnd();
}
|
 |
Problem 6. Create a function that draws the stroke in a creative method of your choosing.
|
void drawPrettyStroke(){
glLineWidth(1);
float strokeWidth=40;
glBegin( GL_TRIANGLE_STRIP );
for(int i =0; i < numPoints - 1; i++)
{
Vec2d currentPoint = pointList[i];
Vec2d A = pointList[i];
Vec2d B = pointList[i+1];
//get vector between the previous and next points
Vec2d V = B - A;
V.normalize();
Vec2d Vp;
//get perpindicular vector
Vp.x = V.y * -1;
Vp.y = V.x;
Vec2d C = currentPoint + Vp*(strokeWidth);
Vec2d D = currentPoint - Vp*(strokeWidth);
if(i%5) strokeWidth = i%10; //pretty effects
else strokeWidth = i%20;
glColor4f(1.0,1.0,1.0,0.2);
//regular stroke
glVertex2f(C.x, C.y);
glVertex2f(D.x, D.y);
}
glEnd();
}
|
 |
Problem 7. Create a function that rotates a shape by a given angle. The function should take as input an array of points (Vec2d *), the number of points in the array (int), and an angle of rotation in degrees. You may want to calculate the center point and then rotate the points around the center, rather than about the window origin.
|
void drawStrokeRotated(Vec2d points[], int size, int rot=180)
{
float strokeWidth=1;
glBegin(GL_TRIANGLE_FAN );
for(int i =0; i < size; i++)
{
Vec2d currentPoint = points[i];
glVertex2f(currentPoint.x, currentPoint.y);
}
glEnd();
//draw rotated stroke
float A = rot;
Vec2d center( windowW/2, windowH/2);
Vec2d Xp, Yp;
Xp.x = dcos(A);//to skew *2;
Xp.y = dsin(A);//to skew *2;
Yp.x = -1*dsin(A);
Yp.y = dcos(A);//end draw kaleidoscope
glColor3f(0.0,0,1.0);
glBegin(GL_TRIANGLE_FAN );
for(int i =0; i < size; i++)
{
Vec2d currentPoint = points[i];
currentPoint = currentPoint - center;
Vec2d rotatedPoint = Xp*currentPoint.x + Yp*currentPoint.y;
rotatedPoint = rotatedPoint + center;
glVertex2f(rotatedPoint.x, rotatedPoint.y);
}
glEnd();
}
|
 |
Problem 8. Create a function that twists a shape around its center. The further a point is from the center, the more it should be rotated.
|
void drawTwistedStroke() {
float strokeWidth=1;
glBegin(GL_LINE_STRIP );
for(int i =0; i < numPoints - 1; i++)
{
Vec2d currentPoint = pointList[i];
glVertex2f(currentPoint.x, currentPoint.y);
}
glEnd();
Vec2d center( windowW/2, windowH/2);
// Vec2d dist = currentPoint - center;
for(float A=0; A<360; A+=20) { //to draw kaleidoscope
Vec2d Xp, Yp;
Xp.x = dcos(A);
Xp.y = dsin(A);
Yp.x = -1*dsin(A);
Yp.y = dcos(A);//end draw kaleidoscope
glColor3f(1.0,0,1.0);
glBegin(GL_LINE_STRIP );
for(int i =0; i < numPoints - 1; i++)
{
Vec2d currentPoint = pointList[i];
currentPoint = currentPoint - center;
Vec2d rotatedPoint = Xp*currentPoint.x + Yp*currentPoint.y;
rotatedPoint = rotatedPoint + center;
glVertex2f(rotatedPoint.x, rotatedPoint.y);
}
glEnd();
}
}
|
 |
|