Bresenham's畫線算法作圖如下:
給定兩個點起點P1(x1, y1), P2(x2, y2),如何畫它們直連的直線呢,即是如何得到上圖所示的藍色的點。假設直線的斜率0<k>0,直線在第一象限,Bresenham算法的過程如下:
1.畫起點(x1, y1).
2.準備畫下一個點,X座標加1,判斷如果達到終點,則完成。否則找下一個點,由圖可知要畫的點要麼爲當前點的右鄰接點,要麼是當前點的右上鄰接點。
2.1.如果線段ax+by+c=0與x=x1+1的交點y座標大於(y+*y+1))/2則選右上那個點
2.2.否則選右下那個點。
3.畫點
4.跳回第2步
5.結束
主要代碼:
// ====== Computer Graphics Experiment #1 ======
// | Bresenham's Line Drawing Algorithm |
// | |
// =============================================
//
// Requirement:
// Implement a general Bresenham's Line Drawing Algorithm.
// Only use GL_POINTS drawing mode.
// Do not use OpenGL line drawing capability.
#include <windows.h>
#include <GL/glut.h>
#include <math.h>
#define PI 3.14159265359
int width, height;
void SetPixel(int x, int y) // 填充一個像素
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
// Bresenham line algorithm
void MyLine(int xs, int ys, int xe, int ye)
{
// Write your code here
//方法一
//glBegin(GL_LINES);
//glVertex2f(xs, ys);
//glVertex2f(xe, ye);
//glEnd();
//方法二
int x = xs;
int y = ys;
int dx = abs(xe - xs);
int dy = abs(ye - ys);
int s1 = xe > xs ? 1 : -1;
int s2 = ye > ys ? 1 : -1;
bool interchange = false; // 默認不互換 dx、dy
if (dy > dx) // 當斜率大於 1 時,dx、dy 互換
{
int temp = dx;
dx = dy;
dy = temp;
interchange = true;
}
int e = 2 * dy - dx;
for(int i = 0; i <= dx; i++)
{
SetPixel(x, y); // SetPixel
if (e >= 0)
{
if (!interchange) // 當斜率 < 1 時,選取上下象素點
y += s2;
else // 當斜率 > 1 時,選取左右象素點
x += s1;
e -= 2 * dx;
}
if (!interchange)
x += s1; // 當斜率 < 1 時,選取 x 爲步長
else
y += s2; // 當斜率 > 1 時,選取 y 爲步長
e += 2 * dy;
}
}
// 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(500, 500);
glutCreateWindow("Lines");
// Initialization
init();
// Define callback functions
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
// Main program loop. (Event loop)
glutMainLoop();
return 0;
}