计算机图形学 - 实验: Bresenham’s Line Algorithm
作者:互联网
本博客基于课程"计算机图形学",教材使用为计算机图形学(第4版) [Computer Graphics with OpenGL, Fourth Edition],部分代码模板便来自于此教材,并且有所改动。大部分内容来自本人实验报告,有错误是难以避免的,若有表述错误或bug欢迎指出。
实验思路
此代码通过一些判断将会出现的需要进行判断的八种情况进行合并成两种,大大减少了代码量
实现代码
核心代码及关键步骤注释
void MyLine(int xs, int ys, int xe, int ye) {//起点坐标为(xs,ys),终点坐标(xe,ye)
int dx = xe - xs, dy = ye - ys;//x和y方向上,起点终点的距离
int x = xs, y = ys, p = 0;//p就是教材上的重点,决策参数
int addX = dx < 0 ? -1 : 1;//对于x轴每次是增加单位长度还是减少单位长度进行判断
int addY = dy < 0 ? -1 : 1;//同上条
dx = fabs(dx);//计算出x轴距离的绝对值,用于下面判断斜率与1的大小关系
dy = fabs(dy);//同上一条
glBegin(GL_POINTS);
//不能单独定义变量作为斜率
//例如: m=dy/dx;
//因为会出现dx==0的情况,这样会出现异常
if (dx>dy) {
for (int i = 0; i < dx; i++) {
glVertex2i(x, y);//放置像素点
x += addX;
if (p < 0) {
p += 2 * dy;
} else {
y += addY;
p += 2 * dy - 2 * dx;//根据斜率情况计算决策参数
}
}
} else {
for (int i = 0; i < fabs(dy); i++) {
glVertex2i(x, y);
y += addY;
if (p < 0)
p += 2 * dx;
else {
x += addX;
p += 2 * dx - 2 * dy;
}
}
}
glEnd();
}
全部代码
#include <GL/glut.h>
#include <cmath>
#include <windows.h>
const float PI = 3.14159;
int width, height;
// Bresenham line algorithm
void MyLine(int xs, int ys, int xe, int ye) {
int dx = xe - xs, dy = ye - ys;
int x = xs, y = ys, p = 0;
int addX = dx < 0 ? -1 : 1;
int addY = dy < 0 ? -1 : 1;
dx = fabs(dx);
dy = fabs(dy);
glBegin(GL_POINTS);
if (dx>dy) {
for (int i = 0; i < dx; i++) {
glVertex2i(x, y);
x += addX;
if (p < 0) {
p += 2 * dy;
} else {
y += addY;
p += 2 * dy - 2 * dx;
}
}
} else {
for (int i = 0; i < fabs(dy); i++) {
glVertex2i(x, y);
y += addY;
if (p < 0)
p += 2 * dx;
else {
x += addX;
p += 2 * dx - 2 * dy;
}
}
}
glEnd();
}
// Initialization function
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
}
// Display callback function
void display(void) {
int i, x0, y0, x, y;
double a;
glClear(GL_COLOR_BUFFER_BIT);
x0 = width / 2;
y0 = height / 2;
glColor3f(1.0, 1.0, 1.0);
// Draw lines
for (i = 0; i < 360; i += 15) {
a = (double)i / 180.0 * PI;
x = 0.45 * width * cos(a);
y = 0.45 * height * sin(a);
if (i == 0 || i == 180)
y = 0;
if (i == 90 || i == 270)
x = 0;
MyLine(x0, y0, x0 + x, y0 + y);
}
glFlush();
}
// Reshape callback function
void reshape(int w, int h) {
// Record width and height of program window
width = w;
height = h;
// Set clipping window and viewport equal to
// view area of program window
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, w, 0.0, h);
}
// Keyboard callback function
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27:
exit(0);
}
}
// Main program entrance
int main(int argc, char* argv[]) {
// Create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(800, 800);
glutCreateWindow("Lines");
// Initialization
init();
// Define callback functions
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
// Main program loop. (Event loop)
glutMainLoop();
return 0;
}
标签:Algorithm,int,void,Bresenham,图形学,dx,dy,xs,ys 来源: https://www.cnblogs.com/arcolaus/p/15784439.html