【问题标题】:How do I calculate points to draw a circle?如何计算点以绘制圆?
【发布时间】:2014-12-17 04:19:14
【问题描述】:

为了平滑,我想在 N 至少为 100 的 N 点之间迭代绘制线段。

我希望这些段形成一个圆圈。

我如何计算这些分数? OpenGL 示例赞赏。

【问题讨论】:

标签: opengl drawing trigonometry


【解决方案1】:

总是有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;
}

【讨论】:

  • 有人在 2006 年声称拥有这种方法的所有权?那很好笑。这个解决方案要老得多。这是我在 1994 年首次使用它编写的代码(至少那是版权日期,它很可能是较旧的),它看起来如此明显,以至于我非常怀疑我是第一个:sourceforge.net/p/molmol/code/ci/Macintosh/tree/src/sg/src/def/…
  • @RetoKoradi:优雅,不是首先想到的东西,但回想起来,显而易见的事情总是被重新发现和重新发明。也发生在我身上,当我重新发现一种方法可以将一个测试数字的 2 次幂的函数重写为不需要编译器特定扩展的类型不可知 C 宏。我非常自豪,却发现几十年前已经发布了相同的宏。尽管如此,我还是自己解决了这个问题。
  • @datenwolf 真的。我认为标准的 2 次方检验归因于 Donald Knuth,但搜索表明它甚至更早(1960 年)。我对图形的第一次尝试是我在高中时实现的装配线绘制算法。后来发现我重新发明了 Bresenham 算法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多