CompuForm Assigment 6: smoothing · posted by vaibhav bhawsar Oct 21, 01:30

Problem 1 – Smoothing

void smoothPoints(Vec2d line[], int nPoints)
{	
	for( int i = 1 ; i < nPoints-1; i++ )
	 {
		Vec2d p = line[i];//current point to smooth
		Vec2d prev; //previous point
		Vec2d next; //next point		
		if(i!=0)
			prev = line[i-1];
		else
			prev = line[0];
		if(i!=nPoints)
			next = line[i+1];
		else
			next = line[nPoints];
		//each multiplied by a smooth value- should add to 1
		Vec2d r  = prev*0.20 + p*.60 + next*0.20; 
		pointList[i] = r;
	 }
}

Problem 2 – Distortion

void bezierDistortedShape(Vec2d pos, int radius)
{
	int numberOfPoints = 90;
	int r = radius;
	Vec2d distortOrigin( mouseX , mouseY );
	Vec2d p[numberOfPoints];
	for(int i = 0; i<numberOfPoints; i++)
	 {
		float angle = 2 * PI / numberOfPoints * i;
		float x = sin(angle) * r;
		float y = cos(angle) * r;
		Vec2d tp(x,y);
		Vec2d fp = tp+pos;
		p[i] = fp;
	}
	//draw the shape
	glBegin(GL_POLYGON);
	glColor3f(.9,.1,.4);
	for(int i = 0; i<numberOfPoints ; i++)
	 {
		Vec2d point	= p[i];
		Vec2d V = distortOrigin - point; //vector between target point and distort Origin
		float length = V.length();
		V.normalize();
		float amp = 50;
		float radius = 50;
		
		//draw the star shape
		//if(i % 1)
		length = length / radius;	
		//else length = length / (radius/2);	 
		float mag = amp * ( 1 / length );
		
		Vec2d dPoint = point + (V * mag);
		glVertex2f( dPoint.x , dPoint.y );
	 }
	glEnd();
}

Problem 3 – intersections

Vec2d newshape[100];
int shapeCount=0;
int s1 = 0;
int s2 = 0; // numberofpoints at a the moment of intersection
void intersectShapes()
{
	pen();
	//currentpoint p0a
	//prevpoint p0b
	//numpoint and numpoint-1
	//i and i+1
	for( int i = 0 ; i < numberOfpoints-2 ; i++ )
	 {
		Vec2d P1a = pointList[numberOfpoints-1]; //currentpoint
		Vec2d P1b = pointList[numberOfpoints-2]; //prevpoint
		Vec2d P0a = pointList[i]; // point currently on
		Vec2d P0b = pointList[i+1]; // point previous to the one currently on
		if( checkIntersectLines(P0a, P0b, P1a, P1b) )
		 {
			printf("intersect \n");
			glColor3f(0,0.75,0);
			newshape[shapeCount] = P1a;
			shapeCount++;
			s1 = i+1;
			s2 = numberOfpoints;
			//printf("s1 %d, s2 %d \n", s1,s2);
			glRectf(P1a.x-4,P1b.y-4,P1a.x+4,P1b.y+4);
		 }
	 }
	
	//draw the small rects
	for (int i =0 ; i < shapeCount ; i++ )
	 {
		Vec2d n = newshape[i];
		glRectf(n.x-4,n.y-4,n.x+4,n.y+4);
	 }
	
	//draw the shape
	glBegin(GL_POLYGON);
	glColor4f(0,0.75,0,.5);
	for (int i = s1 ; i < s2 ; i++ )
	 {
		Vec2d p = pointList[i];
		glVertex2f(p.x,p.y);
	 }
	glEnd();
}

checkIntersectLines function

bool checkIntersectLines(Vec2d P0a, Vec2d P0b, Vec2d P1a, Vec2d P1b)
{
	float M0 = ( P0b.y - P0a.y ) / ( P0b.x - P0a.x ); //slope0 = dy/dx
	float M1 = ( P1b.y - P1a.y ) / ( P1b.x - P1a.x ); //slope1 = dy/dx
	float X = P1a.y - P0a.y - P1a.x * M1 + P0a.x * M0 ;
	X /= ( M0 - M1 );
	
	float Y = P0a.y + ( X - P0a.x ) * M0;
	
	//to check if intersection is within line segment bounds
	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);
	
	//set colour to green if line segment intersect
	if( X > left0 && X < right0	&&	X > left1 && X < right1)
	 {
		return true;	
	 }
	else {
		return false;
	}
}

Commenting is closed for this article.