布雷森汉姆抽屉抽屉在渲染任何东西后一定循环后会崩溃吗?

时间:2019-06-11 08:52:18

标签: c++ opengl glut bresenham

我正在尝试在CodeBlocks中使用OpenGL使用Bresenham Line绘制算法绘制直方图。当drawLine的参数更改(手动/硬编码)时,程序崩溃。代码不完整,我只是想尝试一下。为什么程序在经过某些循环之后在呈现任何内容之前崩溃?

我在drawHistogram函数内的drawLine函数中尝试了不同的参数。循环本身也似乎有问题。我不能在那附近工作。尽管逻辑对我来说似乎还可以,但结果并不完全符合预期。

#include <iostream>
#include <GL/glut.h>

using namespace std;

void drawLine();
void inputData();
void CoordinateAxes();
void plot(float x, float y);

void drawBar(float );
void drawHistogram();

float x, y, dx, dy, D1, D2, xInc, yInc;

//int frequency[5] = {100, 200, 150, 300, 500} ;

int main(int argc, char** argv)
{
    //inputData();

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(500,500);

    glutCreateWindow("DDA Algorithm");
    glutDisplayFunc(drawHistogram);

    glutMainLoop();
    return 0;
}

void drawLine(float x1, float y1, float x2, float y2)
{
    glClear(GL_COLOR_BUFFER_BIT);
    CoordinateAxes();
    glColor3ub(255 ,255,255);
    glBegin(GL_POINTS);

        dx = x2 - x1;
        dy = y2 - y1;

        float P[int(dx)];

        if (dy >= dx)
        {
            D1 = dx;
            D2 = dy;
            xInc = 0;
            yInc = 1;
        }
        else
        {
            D1 = dy;
            D2 = dx;
            xInc = 1;
            yInc = 0;
        }

        P[0] = 2*D1 - D2;


        x = x1;
        y = y1;
        //plot(x, y);
        //cout << "(" << x << "," << y << ") ";
        //cout << D2;



        for(int i = 0; i < D2 ; i++)
        {
            if (P[i] <= 0)
            {
                plot(x , y);
                P[i+1] = P[i] + 2*D1;
                x += xInc;
                y += yInc;
                //cout << "(" << x << "," << y << ")";
            }
            else
            {
                plot(x , y );
                P[i+1] = P[i] + 2*D1 - 2*D2;
                x += 1;
                y += 1;
                //cout << "(" << x << "," << y << ")";
            }
        }
    glEnd();
    glFlush();
}

//void inputData()
//{
//    cout << "Enter frequencies of 5 data: ";
//
//    for (int i = 0; i < 5; i++)
//    {
//        cin >> freq[i];
//    }
//}

void CoordinateAxes()
{
    glColor3ub(0,0,255);
    glBegin(GL_LINES);
        glVertex2f(0,1);
        glVertex2f(0,-1);
        glVertex2f(-1,0);
        glVertex2f(1,0);
    glEnd();
}

void plot(float x, float y)
{
    glVertex2f(x/500, y/500);
    cout << "(" << x << "," << y << ")";
}

//void drawBar(float freq)
//{
//    drawLine(0, 0, 0, freq );
//    drawLine(0, freq, 25, freq);
//    drawLine(25, freq, 25, 0);
//}

void drawHistogram()
{
    drawLine(10, 10, 10, 100 );
}

我希望在OpenGL窗口上呈现一条直线。

1 个答案:

答案 0 :(得分:1)

问题始于

drawLine(10, 10, 10, 100 )   

此调用导致x1=10x2=10,因此dx=0由于dx = x2 - x1;
最后,由于P

,数组float P[int(dx)];的大小为0
void drawLine(float x1, float y1, float x2, float y2)
{
    // [...]

    dx = x2 - x1;
    dy = y2 - y1;

    float P[int(dx)]; 

您必须创建一个数组,其绝对最大差值为x2-x1y2-y1

#include <algorithm>
float P[int(std::max(abs(dx), abs(dy))+0.5)+1];

在c ++中,我建议使用std::vector

#include <vector>
std::vector<float> P(int(std::max(abs(dx), abs(dy))+0.5)+1, 0.0f);