CompuForm Week 4 · posted by vaibhav bhawsar Oct 15, 03:51

Problem 1

Vec2d mousepos;
Vec2d prevpos;
void mouseSteps ( int n )
{
	//set under glut callback for mousereleased
	if(mouseReleased)
	{
		prevpos = mousepos;
		mousepos.x = mouseX;
		mousepos.y = mouseY;
		mouseReleased = false;
	}	
	int nPoints = n+1;
	float dP = 1.0 / nPoints;
	Vec2d A = mousepos;
	Vec2d B = prevpos;
	glBegin( GL_LINE_STRIP);
	for (float p = 0 ; p <= 1.001 ; p+=dP )
	{
	Vec2d C =  A * ( 1.0 - p ) + B * p;
	float Y = fmod(p,.1000f);
	if(Y){
	glColor3f(1,1,0);
	}else{
	glColor3f(1,1,1);
	}
	drawCircle(C,4,8);
	}
}

Problem 2

void blendShape(int numberOfSteps)
{
	int nPoints = 20;
	Vec2d shapeA[nPoints];
	Vec2d shapeB[nPoints];
	int rA = 100;
	int y = windowH/2;
	Vec2d posA(150,y);
	int rB = 50;
	Vec2d posB(1300,y);
	
	//generate shapeA
	for(int i = 0 ; i < nPoints ; i++){
		float angle = 2*PI / nPoints * i;
		float x = cos(angle) * rA;
		float y = sin(angle) * rA;
		Vec2d p(x,y);
		Vec2d c = p + posA;
		shapeA[i] = c;
	}
	
	//generate shapeB	STAR
	for(int i = 0 ; i < nPoints ; i++){
		float angle = 2*PI / nPoints * i;
		Vec2d p;
		if ( i % 2 ) {
			p.x = cos(angle) * (rB);
			p.y = sin(angle) * (rB);
		} else {
			p.x = cos(angle) * (rB+20);
			p.y = sin(angle) * (rB+20);
		}
		Vec2d c = p + posB;
		shapeB[i] = c;
	}
	
	//generate blend shape
	for(int i = 0 ; i < nPoints ; i++ ) 
	{
		Vec2d A = shapeA[i];
		Vec2d B = shapeB[i];
		float dP = 1.0 / (numberOfSteps+1);
		
		glPointSize( 2 );
		glBegin( GL_POINTS);
		glColor3f( .8,1,1 );			
		for ( float p = 0 ; p <= 1.001 ; p+=dP )
		{
			if(i%2) glColor3f( .8,1,1 );
			else glColor3f( c*256,c*64,0 );
			Vec2d C =  A * ( 1.0 - p ) + B * p;
			glVertex2f(C.x,C.y);
		}
		glEnd();
	}
	for(int i = 0; i < nPoints ; i++ ) 
	{
		Vec2d a = shapeA[i];
		glBegin(GL_LINE_LOOP);
		glColor3f(0,0,0);
		glVertex2f(a.x,a.y);
	}
	glEnd();
	for(int i = 0 ; i < nPoints ; i++ ) 
	{
		Vec2d b = shapeB[i];
		glColor3f(0,0,0);
		glBegin(GL_LINE_LOOP);
		glColor3f(0,0,0);
		glVertex2f(b.x,b.y);
	}
	glEnd();
}

Problem 3

void bezier(Vec2d A, Vec2d B, Vec2d c1, Vec2d c2)
{
	int nPoints = 122;
	float dP = 1.0 / nPoints;
	for ( float p = 0; p < 1.001 ; p+=dP )
	{
		float b = 1.0 - p;
		Vec2d v = A*b*b*b + c1*3*b*b*p + c2*3*b*p*p + B*p*p*p;
		glBegin(GL_LINE_STRIP);
		glColor3f(1,0,1);
		glVertex2f(v.x,v.y);
	}
	glEnd();
	bool drawHandles = true;
	if (drawHandles)
	{
	glColor4f(1,0,1,.5);
	glRectf( A.x-5, A.y-5, A.x + 5, A.y + 5 );
	glRectf( B.x-5, B.y-5, B.x + 5, B.y + 5 );	
	glColor4f(1,1,0,.5);
	glRectf( c1.x-5, c1.y-5, c1.x + 5, c1.y + 5 );
	glRectf( c2.x-5, c2.y-5, c2.x + 5, c2.y + 5 );
	}
}

Problem 4

//generating shapes out of beziers//
bool generateArray = true;
Vec2d* bShape1;
Vec2d* bShape2;
void bezierShape()
{
	int numberOfPoints = 90;
	
	//this is one time only
	if(generateArray)
	{
		bShape1 = generateDaisy(10,50,10,numberOfPoints);
		bShape2 = generateDaisy(10,200,50,numberOfPoints);
		generateArray = false;
		printf("arrayGenerated");	
	}
	//draw the beziers
	for(int i = 0; i<numberOfPoints ; i++)
	{
		Vec2d pos(windowW/2,windowH/2);
		Vec2d a = bShape1[i] + pos;
		Vec2d b = bShape2[i] + pos;
		bezier(a,b,Vec2d(mouseX,mouseY),Vec2d(mouseX,mouseY));
	}
}

Comment

Commenting is closed for this article.