Week 6 Assignment - Computational Forms
Lucas Longo
Problem 1. Create a function called smoothPath that takes as input an array of points (Vec2d*), the number of points (int), and a smoothing parameter (float) between 0.0 and 1.0. Apply this function to a stroke that has been drawn. Optionally, try applying it to the stroke as it's being drawn.
void smoothPathSticky(Vec2d* pointArray, int numPoints, float smooth){
newPoints[0] = pointArray[0];
newPoints[numPoints] = pointArray[numPoints];
for (int i = 1; i < numPoints - 1; i++){
Vec2d A = pointArray[i-1];
Vec2d B = pointArray[i];
Vec2d C = pointArray[i+1];
float w1 = (1.0-smooth) / 2;
float w2 = smooth;
float w3 = (1.0-smooth) / 2;
Vec2d D = A * w1 + B * w2 + C * w3;
newPoints[i] = D;
}
for (int i = numPoints - 10 ; i < numPoints - 1; i++){
pointList[i] = newPoints[i];
}
glColor3f(0,1,0);
glBegin(GL_LINE_STRIP);
for (int i = 1; i < numPoints - 1; i++){
glVertex2f(newPoints[i].x,newPoints[i].y);
}
glEnd();
}
void drawProblem1(){
smoothPathSticky(pointList, numPoints, 0.1);
}
Version 1
void smoothPathAnimated(Vec2d* pointArray, int numPoints, float smooth){
newPoints[0] = pointArray[0];
newPoints[numPoints] = pointArray[numPoints];
for (int i = 1; i < numPoints - 1; i++){
Vec2d A = pointArray[i-1];
Vec2d B = pointArray[i];
Vec2d C = pointArray[i+1];
float w1 = (1.0-smooth) / 2;
float w2 = smooth;
float w3 = (1.0-smooth) / 2;
Vec2d D = A * w1 + B * w2 + C * w3;
newPoints[i] = D;
}
for (int i = 0 ; i < numPoints - 1; i++){
pointList[i] = newPoints[i];
}
glColor3f(0,0,1);
glBegin(GL_LINE_STRIP);
for (int i = 1; i < numPoints - 1; i++){
glVertex2f(newPoints[i].x,newPoints[i].y);
}
glEnd();
}
void drawProblem1(){
smoothPathAnimated(pointList, numPoints, 0.1);
}
Problem 2. Create a function called distortPath that distorts a shape by pushing its vertices radially away from 10 pre-set points on the screen, each of which should have a different distortion strength. Apply this function to a circle composed of 360 points. Optionally, allow the circle to be dragged about the screen.
void drawProblem2(){
Vec2d dist[100];
float w[100];
dist[0] = Vec2d(50, 40);
dist[1] = Vec2d(150, 500);
dist[2] = Vec2d(300, 40);
dist[3] = Vec2d(450, 450);
dist[4] = Vec2d(550, 30);
dist[5] = Vec2d(650, 350);
dist[6] = Vec2d(750, 15);
dist[7] = Vec2d(790, 500);
w[0] = 1000;
w[1] = 2000;
w[2] = 1500;
w[3] = 3000;
w[4] = 500;
w[5] = 400;
w[6] = 200;
w[7] = 100;
glPointSize(2);
glColor3f(1,1,0);
glBegin(GL_POINTS);
for (int i = 0; i < 8; i++){
glVertex2f(dist[i].x,dist[i].y);
}
glEnd();
Vec2d center(mouseX, mouseY);
int numCirclePoints = 360;
Vec2d S(center);
float radius = 100;
glPointSize(1);
glColor3f(1,0,0);
glBegin(GL_POINTS);
for(int i = 0; i < numCirclePoints; i++){
float A = 2 * PI / numCirclePoints * i;
S.x = cos(A)*radius + center.x;
S.y = sin(A)*radius + center.y;
circlePoint = Vec2d(S.x, S.y);
for(int z = 0; z < 8; z++){
Vec2d N = circlePoint - dist[z];
float nLength = N.vLength();
N.normalize();
// FALL OF offset = - 2 ^ -(deltaDistPoint^2)
//float offset = pow(-2.0, -1.0 * (nLength * nLength));
float offset = 1/nLength * w[z];
circlePoint = circlePoint + N * offset;
}
glVertex2f(center.x, center.y);
glVertex2f(circlePoint.x, circlePoint.y);
}
glEnd();
}
Problem 3. Create a function that detects if a drawn stroke intersects itself. If it has, draw a finished shape that uses the intersection point as its start and end point, and is filled in solid red. Call this function continuously while drawing.
bool testIntersection(Vec2d P0a, Vec2d P0b, Vec2d P1a, Vec2d P1b){
float M0 = (P0b.y - P0a.y)/(P0b.x - P0a.x); //slope0 = dY0 / dX0
float M1 = (P1b.y - P1a.y)/(P1b.x - P1a.x); //slope1 = dY1 / dX1
float x = ((P1a.y - P0a.y) + P0a.x * M0 - P1a.x * M1);
if (M0 != M1){
x /= (M0-M1);
float y = P0a.y + (x - P0a.x) * M0;
float left0 = min (P0a.x, P0b.x);
float right0 = max (P0a.x, P0b.x);
float left1 = min (P1a.x, P1b.x);
float right1 = max (P1a.x, P1b.x);
if ( x > left0 && x < right0 && x > left1 && x < right1){
return true;
} else {
return false;
}
}
}
void drawProblem3(){
glColor3f(0,0,1);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < numPoints; i++){
glVertex2f(pointList[i].x, pointList[i].y);
}
glEnd();
for (int z = 0; z < numPoints - 2; z++){
bool test = testIntersection(pointList[z], pointList[z+1], pointList[numPoints-1], pointList[numPoints-2]);
if (test == true){
printf("true\n");
iPoint[iPointCounter] = Vec2d(pointList[z].x, pointList[z].y);
iPointStart = z + 1;
iPointEnd = numPoints - 1;
iPointCounter++;
}
glPointSize(10);
glColor3f(1,1,0);
glBegin(GL_POINTS);
for (int i = 0; i < iPointCounter; i++){
glVertex2f(iPoint[i].x, iPoint[i].y);
}
glEnd();
}
glPointSize(10);
glColor4f(1,1,1,0.5);
glBegin(GL_POLYGON);
for (int i = iPointStart; i < iPointEnd; i++){
glVertex2f(pointList[i].x, pointList[i].y);
//printf("i %i iPointStart %i iPointEnd %i \n", i, iPointStart, iPointEnd);
}
glEnd();
}
Problem 4. Work on your project. You have this week and next week to complete it. It is due on Friday, October 26.