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

Problem 2 Touching Ovals Necklace Clock

Problem 4. Create a function called drawStar that takes as arguments an inner radius (float), an outer radius (float), a number of points (int), and a center point (Vec2d) and draws a star as specified. Demonstrate that this function works by drawing 100 stars with a varying number of points placed randomly about the screen. Feel free to change the background color for a more dramatic effect.

void drawStar(Vec2d c)
{
	int innerRadius		=	70;
	int outerRadius		=	90;
	int numberOfPoints	=	30;
	Vec2d center(c.x,c.y);
	Vec2d p;
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(center.x,center.y);
	for(int i=0; i < numberOfPoints+1 ; i++)
	{
		float angle = 2 * PI / numberOfPoints * i;
		if ( i % 2 ) {
			p.x = cos(angle) * innerRadius;
			p.y = sin(angle) * innerRadius;
		} else {
			p.x = cos(angle) * outerRadius;
			p.y = sin(angle) * outerRadius;
		}
		p = center + p;
		glVertex2f(p.x,p.y);
	}
	glEnd();
}
void drawStarField()
{
	for( int i=0 ; i < 10 ; i++ )
	{
		int w = rand() % windowW; //printf("%d \n",w);
		int h = rand() % windowH; //printf("%d \n",w);
		drawStar(Vec2d(w,h));
	}
}

Commenting is closed for this article.