Week 5 Assignment - Computational Forms

Lucas Longo

 

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 drawProblem1(int spacing){


glColor3f(1,0,0);
glLineWidth(1);
glBegin(GL_LINE_STRIP);
for (int i = 1; i < numPoints; i++){
float deltaX = pointList[i-1].x - pointList[i].x;
float deltaY = pointList[i-1].y - pointList[i].y;
float len = sqrt(pow(deltaX,2) + pow(deltaY,2));

if (len < spacing){
pointList[i] = pointList[i-1];
}

glVertex2f(pointList[i].x, pointList[i].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 drawProblem2(int strokeWidth, float spacing){

glColor3f(1,0,0);
glLineWidth(5);
glBegin(GL_TRIANGLE_STRIP);
for (int i = 1; i < numPoints - 1; i++){

Vec2d currentPoint = pointList[i];

Vec2d A = pointList[i-1];
Vec2d B = pointList[i+1];

Vec2d V = B - A;
V.normalize();

Vec2d Vp;
Vp.x = V.y * -1.0;
Vp.y = V.x;

Vec2d C = currentPoint + Vp*strokeWidth;
Vec2d D = currentPoint - Vp*strokeWidth;

glVertex2f(C.x, C.y);
glVertex2f(D.x, D.y);
//glVertex2f(currentPoint.x, currentPoint.y);

}
glEnd();
}

}
glEnd();
}

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 drawProblem3(int strokeWidth){

glColor3f(1,0,0);
glLineWidth(1);
glBegin(GL_LINE_STRIP);
for (int i = 1; i < numPoints - 1; i++){

Vec2d currentPoint = pointList[i];

Vec2d A = pointList[i-1];
Vec2d B = pointList[i+1];

Vec2d V = B - A;
V.normalize();

Vec2d Vp;
Vp.x = V.y * -1.0;
Vp.y = V.x;

Vec2d C = currentPoint + Vp*strokeWidth*2;
Vec2d D = C + V*strokeWidth;
Vec2d E = D - Vp*(strokeWidth-strokeWidth/3);
Vec2d F = E + V*strokeWidth;
Vec2d G = F - Vp*(strokeWidth-strokeWidth/3);
Vec2d H = G + V*strokeWidth;
Vec2d I = H - Vp*(strokeWidth-strokeWidth/3);

glVertex2f(currentPoint.x, currentPoint.y);
glVertex2f(C.x, C.y);
glVertex2f(D.x, D.y);
glVertex2f(E.x, E.y);
glVertex2f(F.x, F.y);
glVertex2f(G.x, G.y);
glVertex2f(H.x, H.y);
glVertex2f(I.x, I.y);
}
glEnd();
}

Version 1

void drawProblem3(float strokeWidth){

glColor3f(1,0,0);
glLineWidth(1);
glBegin(GL_TRIANGLE_STRIP);
for (int i = 1; i < numPoints - 1; i++){

Vec2d cP = pointList[i];
Vec2d pP = pointList[i-1];
//Vec2d nP = pointList[i+1];

//Parallel Vectpr
Vec2d vPar = cP - pP;
vPar.normalize();

//Perpendicular Vector
Vec2d vPer;
vPer.x = vPar.y * -1.0;
vPer.y = vPar.x;

printf("% i %i\n", i, i%2);

if (i%40 < 10)
cP = cP + vPer * (strokeWidth * 1);
else if (i%40 < 20)
cP = cP + vPer * (strokeWidth * 2);
else if (i%40 < 30)
cP = cP + vPer * (strokeWidth * 3);
else
cP = cP + vPer * (strokeWidth * 4);

glVertex2f(cP.x,cP.y);
glVertex2f(pointList[i].x, pointList[i].y);

}
glEnd();
}

Version 2

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 drawProblem4(float amplitude, float frequency){

glColor3f(1,0,0);
glLineWidth(1);
glBegin(GL_LINE_STRIP);
for (int i = 1; i < numPoints - 1; i++){

Vec2d currentPoint = pointList[i];
Vec2d previousPoint = pointList[i-1];

Vec2d P = currentPoint - previousPoint;
P.normalize();

Vec2d N;
N.x = P.y * -1.0;
N.y = P.x;

float angle = 2 * PI / 20.0 * i;
float offset = amplitude * sin(angle*frequency);
currentPoint = currentPoint + N * offset;
glVertex2f(currentPoint.x, currentPoint.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 drawProblem5(){

float strokeWidth = 10;

glLineWidth(1);
glBegin(GL_LINES);

for (int i = 1; i < numPoints - 1; i++){

Vec2d currentPoint = pointList[i];

Vec2d A = pointList[i-1];
Vec2d B = pointList[i+1];

Vec2d V = B - A;
V.normalize();

Vec2d Vp;
Vp.x = V.y * -1.0;
Vp.y = V.x;

Vec2d C = currentPoint + Vp*strokeWidth - V*strokeWidth * 4;
Vec2d D = currentPoint - Vp*strokeWidth - V*strokeWidth * 4;

glVertex2f(C.x, C.y);
glVertex2f(currentPoint.x, currentPoint.y);
glVertex2f(D.x, D.y);
glVertex2f(currentPoint.x, currentPoint.y);
}
glEnd();
}

Problem 6. Create a function that draws the stroke in a creative method of your choosing.

void drawProblem6(float amplitude, float frequency){

glColor3f(1,0,0);
glLineWidth(1);
glBegin(GL_POINTS);
for (int i = 1; i < numPoints - 1; i++){

Vec2d currentPoint = pointList[i];
Vec2d A = pointList[i-1];
Vec2d B = pointList[i+1];

Vec2d P = B - A;
P.normalize();

Vec2d N;
N.x = P.y * -1.0;
N.y = P.x;

for (int z = numPoints; z > 0; z--){
float angle = 2 * PI / numPoints * z;
float offset = 500 * d_sin(angle*2);
Vec2d C = pointList[z-1] + N * offset;
glVertex2f(C.x, C.y);
}
//glVertex2f(pointList[i].x, pointList[i].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 drawProblem7(float angle){

glColor3f(1,0,0);
glLineWidth(1);

angle = mouseX;

Vec2d A = Vec2d(0,0);
Vec2d B = Vec2d(0, 100);
Vec2d C = Vec2d(100, 100);
Vec2d D = Vec2d(100, 0);

Vec2d Xprime(cos(angle),sin(angle));
Vec2d Yprime(sin(angle)*-1,cos(angle));

A = Xprime*A.x + Yprime*A.y;
B = Xprime*B.x + Yprime*B.y;
C = Xprime*C.x + Yprime*C.y;
D = Xprime*D.x + Yprime*D.y;

Vec2d center(windowW/2,windowH/2);

A = A + center;
B = B + center;
C = C + center;
D = D + center;

glBegin(GL_LINE_LOOP);
glVertex2f(A.x, A.y);
glVertex2f(B.x, B.y);
glVertex2f(C.x, C.y);
glVertex2f(D.x, D.y);
glEnd();
}

Version 1

void drawProblem7(){

int teeth = 30;
float innerRadius = 100;
float outterRadius = innerRadius + 10;
int radius = 100;

Vec2d center(windowW/2,windowH/2);
float angle = mouseX;

Vec2d xPrime(cos(angle),sin(angle));
Vec2d yPrime(sin(angle)*-1,cos(angle));

int nP = 360;

glBegin(GL_LINE_LOOP);
for (int i= 0; i < nP; i++){
float A = 360 / nP * i;
int position = 360 / nP * i;
int repeat = 360/teeth;
Vec2d gear;

if (position%repeat < repeat/2){
radius = innerRadius;
}
else {
radius = outterRadius;
}
gear = Vec2d(d_cos(A)*radius, d_sin(A)*radius);

//Rotate
gear = xPrime * gear.x + yPrime * gear.y;
//Translate
gear = gear + center;

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

}

Version 2

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 drawProblem8(){

int teeth = 30;
float innerRadius = 100;
float outterRadius = innerRadius + 10;
int radius = 100;

Vec2d center(windowW/2,windowH/2);

int nP = 360;

glBegin(GL_LINE_LOOP);
for (int i= 0; i < nP; i++){
float A = 360 / nP * i;
int position = 360 / nP * i;
int repeat = 360/teeth;
Vec2d gear;

if (position%repeat < repeat/2){
radius = innerRadius;
}
else {
radius = outterRadius;
}
gear = Vec2d(d_cos(A)*radius, d_sin(A)*radius);

//Translate
gear = gear + center;

//Twist
Vec2d dist = gear - center;
Vec2d perp(dist.y * -1, dist.x);
perp.normalize();

float len = sqrt( dist.x * dist.x + dist.y * dist.y);

gear = gear + perp * (len + mouseX);

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