【问题标题】:represent a stacked bar chart graphically with openGL using C++使用 C++ 使用 openGL 以图形方式表示堆积条形图
【发布时间】:2017-04-01 13:12:11
【问题描述】:

我想出了如何制作条块,但我的问题是每个条的底部边缘的定位,我不知道如何使它们完美地堆叠在一起,所以它们中的大多数最终都在顶部另一个,使酒吧看起来不对。 这是我的功能:

void StackedBar(void)
{
// Clear the Screen
glClear(GL_COLOR_BUFFER_BIT);

float colors[6][3] = { { 1,0,0 },{ 1,1,0 },{ 0,1,0 },{ 0,1,1 },{ 1,0,1 },{0,0,1} };
float data[6] = { 20,66,42,28,71,23 };
float x = 0, y = 0;
float dy;

glBegin(GL_QUADS);
for (int i = 0; i < 6; i++) {
    glColor3f(colors[i][0], colors[i][1], colors[i][2]);
    dy = data[i];
    glVertex2f(x, y);
    glVertex2f(x + 5, y);
    glVertex2f(x + 5, y + dy);
    glVertex2f(x, y + dy);
    y  = dy;
    //x +=5; incremented x just to see how the pieces are positioned
}
glEnd();

// force execution of GL commands
glFlush();
}

输出:

result

result with x incremented with 5 just to see the pieces are positioned

【问题讨论】:

    标签: c++ opengl graphics opengl-4 stacked-chart


    【解决方案1】:

    如果您想将条形堆叠在一起,只需在每次迭代中将 y 递增 dy。

    for (int i = 0; i < 6; i++) {
        glColor3f(colors[i][0], colors[i][1], colors[i][2]);
        dy = data[i];
        glVertex2f(x, y);
        glVertex2f(x + 5, y);
        glVertex2f(x + 5, y + dy);
        glVertex2f(x, y + dy);
        y += dy;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-02
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 2014-08-08
      • 2022-06-13
      相关资源
      最近更新 更多