Comp Form: Week 4

interpolation, beziers, hair. . .

//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.
points1_wk4.png

void drawPts(Vec2d a, Vec2d b, int n){
Vec2d c;
glBegin(GL_POINTS);
for(float i=0;i<1;i=i+1.0/n){
c=a*(1.0-i)+b*i;
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.

blend_wk4.png

Vec2d LERP(Vec2d a, Vec2d b, float p){  //(vec, vec, percentage)
	Vec2d c;
	c=a*(1.0-p)+b*p;
	return c;
}
void drawShape(Vec2d shape[], int length){
	glBegin(GL_LINE_LOOP);

	for (int i=0;i<length;i++){>
		glVertex2f(shape[i].x,shape[i].y);
	}
	glEnd();

}
void drawBlend(Vec2d shapeA[], Vec2d shapeB[], float steps, int length){
	for (float j=0;j<steps;j++){>
		Vec2d d[length];
		glBegin(GL_LINE_LOOP);
		for (int i=0;i<length;i++){>
			d[i]=LERP(shapeA[i],shapeB[i],j/steps);
			glVertex2f(d[i].x,d[i].y);
		}
		glEnd();
	}
}
</length;i++){></steps;j++){></length;i++){>
<length;i++){><steps;j++){><length;i++){>
	Vec2d shapeA[]={Vec2d(50,100),Vec2d(100,200),Vec2d(150,250),Vec2d(100,150)};
	Vec2d shapeB[]={Vec2d(300,400),Vec2d(400,500),Vec2d(700,400),Vec2d(400,300)};
	drawShape(shapeA,4);
	drawShape(shapeB,4);
	drawBlend(shapeA,shapeB,10,4);</length;i++){></steps;j++){></length;i++){>

//Problem 3. Create a function that takes four points (Vec2d) as input and draws a single Bezier curve based upon those four points.
bezier_wk4_1.png

void drawBezier(Vec2d A, Vec2d B, Vec2d C, Vec2d D){
	glBegin(GL_LINE_STRIP);
	for (float p=0;p&lt;1.0;p=p+.01){
		float b=1.0-p;
		Vec2d J=A*b*b*b+B*b*b*p+C*p*p*b+D*p*p*p;
		glVertex2f(J.x,J.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.
beziershapewk4.png

void drawBezierShape(Vec2d shape[], int length){
for (int i=0;i<length;i=i+3){>
drawBezier(shape[i],shape[i+1],shape[i+2],shape[i+3]);

}
}

Vec2d shapeA[]={Vec2d(50,100),Vec2d(100,200),Vec2d(150,250),Vec2d(100,150),Vec2d(450,650),Vec2d(300,100),Vec2d(700,400),Vec2d(400,300)};
drawBezierShape(shapeA,8);
</length;i=i+3){>

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

void drawHair(){
	for (float i=0.0;i<windoww;i=i+20){>
		for (float j=0.0;j<windowh;j=j+20){>
			float angle=atan2(mouseX-i,mouseY-j);
			float distance=dist(i,mouseX,j,mouseY);
			//drawBezier(Vec2d(i,j),Vec2d(i,j),Vec2d(i,j),Vec2d(i,j));
			Vec2d a=Vec2d(10.0+i,10.0+j);
			Vec2d b=Vec2d((i*3+60.0-100*cos(angle)),(j*3+100+100*sin(angle)));
			Vec2d c=Vec2d((i*3+60.0+150*cos(angle)),(j*3+100-150*sin(angle)));
			Vec2d d=Vec2d((40.0+i),(40.0+j));

			glColor3f(distance/windowW,0,0);

			drawBezier(a,b,c,a);

		}
	}
}