c++ - 使用 OpenGL 或 D3D 绘制椭圆的有效方法

标签 c++ opengl vector geometry

像这样画圆有一种快速的方法

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

我想知道是否有类似的方法来绘制椭圆,其中它的长轴/短轴 vector 和大小都是已知的。

最佳答案

如果我们以您的示例为例,我们可以使用 1 的内半径并分别应用水平/垂直半径以获得椭圆:

void DrawEllipse(float cx, float cy, float rx, float ry, 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 = 1;//we start at angle = 0 
    float y = 0; 

    glBegin(GL_LINE_LOOP); 
    for(int ii = 0; ii < num_segments; ii++) 
    { 
        //apply radius and offset
        glVertex2f(x * rx + cx, y * ry + cy);//output vertex 

        //apply the rotation matrix
        t = x;
        x = c * x - s * y;
        y = s * t + c * y;
    } 
    glEnd(); 
}

关于c++ - 使用 OpenGL 或 D3D 绘制椭圆的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5886628/

相关文章:

java - 50 MB 图像使用 1 GB RAM 作为纹理

c++ - 我不断收到错误 "no match for call ' (std::vector<int>) (int)”

java - 为什么我的旋转矩阵不起作用?

c++ - 类静态函数中变量的非静态成员引用

c++ - CMake错误-使用柯南

c++ - 在 64 位可执行文件中执行 64 位计算

c++ - 与指针相关的 UE4 崩溃。我的指点哪里错了?

c# - 使用 Yaw 和 Pitch 创建旋转矩阵并模拟 FPS 相机

c++ - 将 operator != 与 std::vector 迭代器一起使用时出错

c++ - Windows 8 下 Metro 风格应用程序的类似 Fraps 功能