Comp Form: Week 6

smooth lines, distortion, intersection . . . //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.
wk6screenshot_1.png

//int numPoints=0;
//Vec2d pointList[1000];
Vec2d S[1000];
void smoothPath(float smooth){
	glColor3f(1,0,0);
	//glLineWidth(20);
	glBegin(GL_LINE_STRIP);
	for ( int i=0; i<numpoints;i++){>
		if(i&gt;2){
			Vec2d A=Vec2d(pointList[i-2].x, pointList[i-2].y);
			Vec2d B=Vec2d(pointList[i-1].x, pointList[i-1].y);
			Vec2d C=Vec2d(pointList[i].x, pointList[i].y);

			S[i]=A*(1-smooth)/2+B*smooth+C*(1-smooth)/2;

			glVertex2f(S[i].x,S[i].y);
		}
	}
	glEnd();
}</numpoints;i++){>

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

void distortPath(Vec2d shape[], int shapeSize, Vec2d distort[], int distortSize){
	glColor3f(1,0,0);
	//glLineWidth(20);

	for ( int k=0; k<distortsize;k++){>
		glRectf(distort[k].x,distort[k].y,distort[k].x+2,distort[k].y+2);
	}

	float distortX[shapeSize];
	float distortY[shapeSize];
	float dist[distortSize];
	Vec2d sd1[distortSize];

	glBegin(GL_LINE_LOOP);

	for ( int i=0; i<shapesize;i++){>
		distortX[i]=0;
		distortY[i]=0;
		for ( int l=0; l<distortsize;l++){>
			sd1[l]=shape[i]-distort[l];
			dist[l]=sd1[l].length()/500;
			sd1[l].normalize();
			distortX[i]+=sd1[l].x/dist[l];
			distortY[i]+=sd1[l].y/dist[l];
		}
		glVertex2f(shape[i].x+distortX[i],shape[i].y+distortY[i]);
	}

	glEnd();
}
</distortsize;l++){></shapesize;i++){></distortsize;k++){>

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

bool checkIntersection (Vec2d P0a, Vec2d P0b, Vec2d P1a, Vec2d P1b){
	bool checkIntersection;
	float M0=(P0b.y-P0a.y)/(P0b.x-P0a.x); //slope
	float M1=(P1b.y-P1a.y)/(P1b.x-P1a.x);

	float X=P1a.y-P0a.y-P1a.x*M1+P0a.x*M0;
	X/=(M0-M1);
	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&gt;left0 &amp;&amp; X<right0>left1 &amp;&amp; X<right1){>
		checkIntersection= true;
	}
	else {
		checkIntersection=false;
	}
	return checkIntersection;
}

void drawStrokeFill(float smooth){
	glColor3f(1,0,0);
	//glLineWidth(20);
	bool intersect;

	glBegin(GL_LINE_STRIP);
	for ( int i=0; i<numpoints;i++){>
		if(i&gt;2){
			Vec2d A=Vec2d(pointList[i-2].x, pointList[i-2].y);
			Vec2d B=Vec2d(pointList[i-1].x, pointList[i-1].y);
			Vec2d C=Vec2d(pointList[i].x, pointList[i].y);

			S[i]=A*(1-smooth)/2+B*smooth+C*(1-smooth)/2;

			glVertex2f(S[i].x,S[i].y);
		}
	}
	glEnd();

	for (int i=1;i<numpoints;i++){>
		for (int j=1;j<numpoints;j++){>
			intersect=	checkIntersection(S[i],S[i-1],S[j],S[j-1]);
			if (intersect==true) {

				glBegin(GL_TRIANGLE_FAN);
				for ( int i=0; i<numpoints;i++){>
					if(i&gt;2){
						Vec2d A=Vec2d(pointList[i-2].x, pointList[i-2].y);
						Vec2d B=Vec2d(pointList[i-1].x, pointList[i-1].y);
						Vec2d C=Vec2d(pointList[i].x, pointList[i].y);

						S[i]=A*(1-smooth)/2+B*smooth+C*(1-smooth)/2;

						glVertex2f(S[i].x,S[i].y);
					}
				}
				glEnd();
			}
		}
	}
}
</numpoints;i++){></numpoints;j++){></numpoints;i++){></numpoints;i++){></right1){></right0>