CompForm Assignment 2 - Problem 3 · posted by vaibhav bhawsar Sep 21, 05:29

Problem 2 Touching Ovals Necklace Clock

Problem 3. Create a function called drawOval that takes as arguments a horizontal radius (float), a vertical radius, a number of points (int) and a center point (Vec2d) and draws an oval as specified. Demonstrate that this function works by drawing 10 ovals in a row, each with an increasing horizontal radius (10, 20, 30…) and decreasing vertical radius (100, 90, 80…).

void drawOval ( Vec2d c, float hr, float vr, int numberOfPoints ) {
	glBegin(GL_POLYGON);
	glColor3f(0,0,0);
	for(int i = 0; i < numberOfPoints; i++){
		float angle = 2*PI / numberOfPoints * i;
		float x = sin(angle) * hr;
		float y = cos(angle) * vr;
		Vec2d p(x,y);
		p = p+c;
		glVertex2f(p.x,p.y);
	}
	glEnd();
}
void drawTouchingOvals()
{
	int numberOfOvals = 10;
	int nPoints = 30;
	Vec2d origin(0,400);
	for(int i=1; i<=numberOfOvals; i++){
		float hr = i*10;	//hor radius
		float vr = 110-hr;	//ver radius	
		//printf("horizontal %f , vertical %f \n",hr,vr);
		float dx = (hr*2)-10;//offset
		Vec2d shift(dx,0);
		origin = origin+shift;
		drawOval(origin,hr,vr,nPoints);
	}
}

Commenting is closed for this article.