gpt4 book ai didi

c++ - Bresenham line drawer 在渲染任何东西之前在某个循环后崩溃?

转载 作者:行者123 更新时间:2023-11-28 04:16:15 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用 CodeBlocks 中的 OpenGL 使用 Bresenham 线图算法绘制直方图。当更改 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 窗口上呈现一条直线。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com