【问题标题】:How to adjust this OpenGL codes of drawing a 2d circle into an ellipse如何调整将二维圆绘制成椭圆的 OpenGL 代码
【发布时间】:2021-12-27 15:50:52
【问题描述】:

我有这几行用来画圆的代码,但我想知道如何调整代码以便得到椭圆而不是圆。

void circle(int n, int x, int y, int r) {
double inc = 2 * PI / (double)n;
glBegin(GL_POLYGON);
for (double theta = 0.0; theta <= 2 * PI; theta += inc) {
    glVertex2i(r * cos(theta) + x, r * sin(theta) + y);
}
glEnd(); }

【问题讨论】:

  • 例如:glVertex2i(2 * r * cos(theta) + x, r * sin(theta) + y);
  • 非常感谢!

标签: c++ opengl


【解决方案1】:

椭圆和圆的区别只是一个方向比另一个方向长。

因此,除了计算 r*cos(theta)r*sin(theta) 之外,您还可以在每个中使用不同的 r。椭圆在每个方向上都有不同的“半径”。

请注意,这将产生一个轴对齐的(即水平或垂直椭圆)。要创建一个非轴对齐的椭圆(例如 45 度),从轴对齐的椭圆开始并使用旋转矩阵旋转它可能更容易。

【讨论】:

  • 噢,我明白了。谢谢,真的很有帮助!
【解决方案2】:

这是我所拥有的:

void circle(int n, int x, int y, int rx, int ry) {
double inc = 2 * PI / (double)n;
glBegin(GL_POLYGON);
for (double theta = 0.0; theta <= 2 * PI; theta += inc) {
    glVertex2i(rx * cos(theta) + x, ry * sin(theta) + y);
}
glEnd(); 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-10
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 2011-10-08
    相关资源
    最近更新 更多