为了平滑起见,我想迭代地在N点之间绘制线段,其中N至少为100。
我希望这些片段形成一个圆圈。
如何计算这些积分? OpenGL示例很受欢迎。
答案 0 :(得分:0)
始终是SiegeLord way:
void DrawCircle(float cx, float cy, float r, int num_segments)
{
float theta = 2 * 3.1415926 / float(num_segments);
float c = cosf(theta);//precalculate the sine and cosine
float s = sinf(theta);
float t;
float x = r;//we start at angle = 0
float y = 0;
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//apply the rotation matrix
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
}
所有在一起:
#include <GL/glut.h>
#include <cmath>
void DrawCircle( float radius, float error )
{
const float theta = acos( 1 - ( error / radius ) );
const unsigned int num_segments = ( 2 * 3.1415926 ) / theta;
//precalculate the sine and cosine
const float c = cos( theta );
const float s = sin( theta );
//we start at angle = 0
float x = radius;
float y = 0;
glBegin(GL_LINE_LOOP);
for( unsigned int i = 0; i < num_segments; i++ )
{
glVertex2f( x, y );
//apply the rotation matrix
const float t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
}
float radius = 50;
float direction = 1;
void timer( int extra )
{
if( radius < 50 && direction == -1 )
direction = 1;
if( radius > 250 && direction == 1 )
direction = -1;
radius += 1 * direction;
glutPostRedisplay();
glutTimerFunc( 16, timer, 0 );
}
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const double w = glutGet( GLUT_WINDOW_WIDTH );
const double h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( -w/2, w/2, -h/2, h/2, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3ub( 255, 255, 255 );
DrawCircle( radius, 1 );
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 0;
}