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

Problem 2 Touching Ovals Necklace Clock

Problem 2. Create a function called drawCircle that takes as arguments a radius (float), a number of points (int) and a center point (Vec2d) and draws a circle as specified. Demonstrate that this function works by using it in the following ways:

a. Draw 5 circles in a row, each with increasing radii (10, 20, 30…), just touching each other, each composed of 30 points.

b. Draw 5 regular polygons in a row, each with an increasing number of points (3, 4, 5…), each with a radius of 50 pixels.

void drawCircle ( Vec2d c, int r, 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) * r;
		float y = cos(angle) * r;
		Vec2d p(x,y);
		p = p+c;
		glVertex2f(p.x,p.y);
	}
	glEnd();
}
//also draws the regular polygons
void drawTouchingCircles()
{
	int numberOfCircles = 5;
	int nPoints = 29;
	Vec2d origin(100,100);
	for(int i=0; i<=numberOfCircles; i++)
	{
		
		float r = i*20;//radius	
		float dx = (r*2)-20;//offset
		Vec2d shift(dx,0);
		origin = origin+shift;
		origin.print();
		//draw circles touching each other
		drawCircle(origin,r,nPoints);
		//draw polygons with varying points
		drawCircle(Vec2d(i*100,300),50,i+2);
	}
}

Commenting is closed for this article.