Week 4 Assignment - Computational Forms

Lucas Longo

 

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.

Vec2d LERP(Vec2d A, Vec2d B, float p){
Vec2d C = A*(1-p)+B*p;
return C;
}

void drawProblem1(int numSteps){
glColor3f(1,0,0);
glPointSize(5);

glBegin(GL_POINTS);
glVertex2f(m1.x,m1.y);
glEnd();

glBegin(GL_POINTS);
glVertex2f(m2.x,m2.y);
glEnd();

if (m2.x > 0){
glBegin(GL_POINTS);
for (int i = 0; i < numSteps; i++){
float p = (float)i / numSteps;
Vec2d points = LERP(m1,m2,p);
glVertex2f(points.x,points.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.

void drawPolygon(Vec2d start){

glBegin(GL_LINE_LOOP);
glVertex2f(start.x,start.y);
glVertex2f(start.x+10,start.y+60);
glVertex2f(start.x+60,start.y+200);
glVertex2f(start.x+100,start.y+50);
glVertex2f(start.x+60,start.y+20);
glVertex2f(start.x+10,start.y+200);

glEnd();

}

void drawProblem2(int numSteps){
glColor3f(1,0,0);

drawPolygon(m1);
if (m2.x > 0){
drawPolygon(m2);
for (int i = 0; i < numSteps; i++){
float p = (float)i / numSteps;
Vec2d points = LERP(m1,m2,p);
drawPolygon(points);
}
}
}

Version 1

void drawPolygon(Vec2d start){

glBegin(GL_TRIANGLES);
glVertex2f(start.x,start.y);
glVertex2f(start.x+10,start.y+60);
glVertex2f(start.x+60,start.y+200);
glVertex2f(start.x+100,start.y+50);
glVertex2f(start.x+60,start.y+20);
glVertex2f(start.x+10,start.y+200);

glEnd();

}

void drawProblem2(int numSteps){
glColor3f(1,0,0);

drawPolygon(m1);
if (m2.x > 0){
drawPolygon(m2);
for (int i = 0; i < numSteps; i++){
float p = (float)i / numSteps;
Vec2d points = LERP(m1,m2,p);
drawPolygon(points);
}
}
}

Version 2

void drawPolygon(Vec2d start){

glBegin(GL_TRIANGLES);
for (int i= 0; i < 1000; i++){
float A = i;

float radius = 20 + 10*d_sin(A);
radius += 10*d_sin(A*3+40);

float x = radius * d_cos(A);
float y = radius * d_sin(A);

glColor3f( 1,mouseX/windowW,0 );
glVertex2f(x+start.x,y+start.y);
glColor3f( 0,mouseY/windowH,1 );
glVertex2f(start.x,start.y);
}
glEnd();

}

void drawProblem2(int numSteps){
glColor3f(1,0,0);

drawPolygon(m1);
if (m2.x > 0){
drawPolygon(m2);
for (int i = 0; i < numSteps; i++){
float p = (float)i / numSteps;
Vec2d points = LERP(m1,m2,p);
drawPolygon(points);
}
}
}

Version 3

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 A, Vec2d B, Vec2d C, Vec2d D){
glColor3f(1,1,1);
glBegin(GL_LINE_STRIP);
glVertex2f(A.x,A.y);
glVertex2f(B.x,B.y);
glVertex2f(C.x,C.y);
glVertex2f(D.x,D.y);
glEnd();

glPointSize(1);
glBegin(GL_POINTS);
for (float p = 0; p <= 1.0; p+=0.001){
float b = 1.0-p;
Vec2d R = A*(b*b*b) + B*(3*p*b*b) + C*(3*p*p*b) + D*(p*p*p);
glColor3f(1,0,0);
glVertex2f(R.x,R.y);
}
glEnd();

}

void drawProblem3(Vec2d A, Vec2d B, Vec2d C, Vec2d D){
if (m1.y > 0)
drawBezier(A,B,C,D);
}

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 manyBeziers(Vec2d pArray[], int points){
for (int i = 0; i < points - 3; i = i + 3){
drawBezier(pArray[i],pArray[i+1],pArray[i+2],pArray[i+3]);
}
}

void drawProblem4(){
int points = 10;
Vec2d pArray[points];

pArray[0] = Vec2d(0,0);
pArray[1] = Vec2d(20,500);
pArray[2] = Vec2d(99,80);
pArray[3] = Vec2d(100,300);
pArray[4] = Vec2d(200,200);
pArray[5] = Vec2d(300,500);
pArray[6] = Vec2d(400,100);
pArray[7] = Vec2d(520,300);
pArray[8] = Vec2d(700,150);
pArray[9] = Vec2d(800,100);

manyBeziers(pArray, points);
}

Version 1

Problem 5. Create a function that draws a head of hair composed of 500 bezier curves. Extra credit for highlights.

void drawCircle(float radius, int numPoints, Vec2d center)
{
Vec2d S(center);

glBegin(GL_TRIANGLES);
for(int i = 0; i < numPoints; i++){
float A = 2 * PI / numPoints * i;

S.x = cos(A)*radius + center.x;
S.y = sin(A)*radius + center.y;

glVertex2f(center.x, center.y);
glVertex2f(S.x, S.y);
}

S = S + center;

glEnd();

}

void drawHairs(Vec2d* hArray, int points){

int hairs = 50;
Vec2d center(windowW/2, windowH/2);

for (int i = 0; i < hairs; i++){
float A = 360 / hairs * i;
int x = 10*d_sin(A) + 1;
int y = d_cos(A) + 1;

for (int z = 0; z < points; z++){
hArray[z] = Vec2d(hArray[z].x + x ,hArray[z].y + y);
}

glLoadIdentity();
glTranslatef(rand()%30, rand()%5 - 50,0);
glRotatef(rand()%30,0,0,1);
glColor3f(1,0,0.2);
manyBeziers(hArray, points);
}

glLoadIdentity();
glColor3f(0.0,1.0,0.9);
drawCircle(150, 500,Vec2d(windowW/2-50,windowH/2-75));

}

void drawProblem5(){

int points = 25;
Vec2d hArray[points];

hArray[0] = Vec2d(512,278);
hArray[1] = Vec2d(483,280);
hArray[2] = Vec2d(467,257);
hArray[3] = Vec2d(496,258);
hArray[4] = Vec2d(503,265);
hArray[5] = Vec2d(496,275);
hArray[6] = Vec2d(461,271);
hArray[7] = Vec2d(444,270);
hArray[8] = Vec2d(465,261);
hArray[9] = Vec2d(471,270);
hArray[10] = Vec2d(474,286);
hArray[11] = Vec2d(432,291);
hArray[12] = Vec2d(432,268);
hArray[13] = Vec2d(437,262);
hArray[14] = Vec2d(449,281);
hArray[15] = Vec2d(431,298);
hArray[16] = Vec2d(404,295);
hArray[17] = Vec2d(412,284);
hArray[18] = Vec2d(423,295);
hArray[19] = Vec2d(412,326);
hArray[20] = Vec2d(381,307);
hArray[21] = Vec2d(400,301);
hArray[22] = Vec2d(407,331);
hArray[23] = Vec2d(423,313);
hArray[24] = Vec2d(390,325);


drawHairs(hArray, points);

}

Problem 6. Extra credit: make a bezier drawing tool that works just like the one in Adobe Illustrator.

void manyBeziers2(Vec2d pArray[], int points){
for (int i = 0; i < points - 1; i += 3){
drawBezier(pArray[i],pArray[i+1],pArray[i+2],pArray[i+3]);
printf("%i\n",i);
}
}

void drawProblem6(){
manyBeziers2(mArray, pointCount);
}

void mouseMoveFunc ( int x, int y )
{
mouseX = x;
mouseY = windowH - y;

mArray[pointCount] = Vec2d(mouseX,mouseY);
mArray[pointCount+1] = Vec2d(mouseX,mouseY);
mArray[pointCount+2] = Vec2d(mouseX,mouseY);

}

Not quite the pen tool but a start :)

 

Problem 7. Describe and skech (on paper is fine) a computation form that you would like to create for your first class project. Consider all the methods that you know thus far and how they can be combined. Also, feel free to make it interactive or animated or both.

I want to create a sphere that wobbles according to data I feed into it for x, y, and z deformations... the globe should ripple following the variations on the x, y, and z equators