使用带有opengl的C ++绘制椭圆? [英] Drawing an ellipse using C++ with opengl?

查看:474
本文介绍了使用带有opengl的C ++绘制椭圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam试图绘制一个不在我的项目中移动的椭圆,我在这个项目中使用动画,但使用函数'myellipse'使动画非常慢

这是绘制椭圆的代码



我尝试了什么:



这是绘制的代码椭圆

 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();
}



这是我的动画代码

  void 动画( void 
{
if (angle> = 0 && angle< 10)
angle = angle + 0。 5 ;
else angle = 0 ;
glutPostRedisplay();
}

void 自动( void // 控制鱼的运动
{
if (Autorun< = 300 &&& Autorun> - 350
Autorun = Autorun- 0 05 ;
else Autorun = 300 ;
glutPostRedisplay();
}

解决方案

Quote:

但是使用'myellipse'功能会使动画变得非常慢



你想提高速度,这个过程被命名为'优化',帮助你发现瓶颈的工具是探测器。

分析(计算机编程) - 维基百科 [ ^ ]



优化是关于你的代码的关键,它在回答问题时翻译:

为什么我这样做,是否有必要?

我可以得到更多相同的结果高效代码?

您的代码使用固定增量 i = i + .001 这意味着对于任何elipse,您循环6200次,无论如何elipse的大小。

评论家:如果我的话elipse是10像素,我需要循环6200次吗?

答案是否定的,我可以减少循环次数。

诀窍是调整增量到elipse的大小。

这样的东西可以提高速度:

  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 ((R​​X * cos(i)+ y)+。 5 ), int ((RY * sin(i)+ x)+。 5 ));
i = i + inc ;
}
glEnd();
}





如果你想要更快的elipses和圈子,你必须学习这篇文章:

圈子和数字差分分析仪| Dobb博士 [ ^ ]


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

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();
}

解决方案

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:

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[^]


这篇关于使用带有opengl的C ++绘制椭圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆