Click here to Skip to main content
15,885,087 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Iam trying to draw an ellipse that don't move in my project and i use animation in this project but using function 'myellipse' makes the animation very slow
this is the code to draw ellipse

What I have tried:

This is the code to draw an ellipse
void myEllipse(int x,int y,float StAngle,float EndAngle,int RX, int RY)
{
	double i;
	glBegin(GL_LINE_STRIP);
	glColor3f(1,1,0);
	i=StAngle;
	while(i<=EndAngle)
	{
		glVertex2f(int((RX*cos(i)+y)+.5),int((RY*sin(i)+x)+.5));
		i=i+.001;
	}
		glEnd();
}

and this is my code to animation
C++
void animation(void)
{
	if(angle>=0 && angle<10)
		angle = angle+0.5;
	else angle = 0;
	glutPostRedisplay();
}

void Auto(void) //Control the movment of the fish 
{
	if(Autorun<=300 && Autorun>-350)
		Autorun = Autorun-0.05;
	else Autorun = 300;
	glutPostRedisplay();
}
Posted
Updated 15-Apr-18 0:19am

1 solution

Quote:
but using function 'myellipse' makes the animation very slow

You want to improve speed, that process is named 'optimization', the tool that help you to spot bottle necks is the profiler.
Profiling (computer programming) - Wikipedia[^]

Optimization is about being critical about your code, it translate at answering the questions:
Why do I it that way, is it necessary ?
Can I get same result with more efficient code ?
Your code is using a fixed increment i=i+.001 which imply that for any elipse, you loop 6200 times, no matter the size of the elipse.
Critics: if my elipse is 10 pixels across, do I need to loop 6200 times ?
Answer is no, I can reduce the number of loops.
The trick is to adapt the increment to the size of elipse.
Something like this sould improve speed:
C++
void myEllipse(int x,int y,float StAngle,float EndAngle,int RX, int RY)
{
	double i, inc;
	glBegin(GL_LINE_STRIP);
	glColor3f(1,1,0);
	inc=3.14/max(RX,RY)/2;
	i=StAngle;
	while(i<=EndAngle)
	{
		glVertex2f(int((RX*cos(i)+y)+.5),int((RY*sin(i)+x)+.5));
		i=i+inc;
	}
	glEnd();
}


If you want real faster elipses and circles, you have to study this article:
Circles and the Digital Differential Analyzer | Dr Dobb's[^]
 
Share this answer
 
v2
Comments
KarstenK 15-Apr-18 13:47pm    
Great Stuff from good old DDJ ;-)
Patrice T 15-Apr-18 14:05pm    
Always worth checking. :)
Member 15046718 21-Jan-21 0:50am    
Can you send full structure of the program . because i cant figure out what parameter to send.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900