计算机图形学の三种经典画直线算法

三种画直线:

DDA:

image-20201215104520980 image-20201215104606617 image-20201215104554682 image-20201215104619375
void lineDDA (int x0, int y0, int xEnd, int yEnd){
	int dx=xEnd-x0,dy=yEnd-y0,steps,k;
	float xIncrement,yIncrement,x=x0,y=y0;
	if(fabs(dx)> fabs(dy)){
		steps = fabs(dx);
	}else {
		steps = fabs(dy);
	}
	xIncrement = float(dx)/float(steps);
	yIncrement = float(dy)/ float(steps);
	setPixel(round(x),round(y));
	for(k = 0;k< steps;k++){
		x += xIncrement;
		y += yIncrement;
		setPixel(round(x),round(y));
	}
	return ;
}

中点画线法:

image-20201215105747075 image-20201215105754982

这里就是只取当前点右上方或右方的像素点, 看下头的例子会更清楚一点

后头这部分就是对程序的优化了

image-20201215105803136 image-20201215105812579
void lineMiddle(int x0, int y0, int x1, int y1){
	int a,b,d1,d2,d,x,y;
	a=y0-y1,b=x1-x0,d=2*a+b;
	d1=2*a,d2=2*(a+b);
	x=x0,y=y0;
	setPixel (x,y);
	while (x<x1){
		if (d<0){
			x++;
			y++;
			d+=d2;
		}
		else{
			x++;
			d+=d1;
		}
		setPixel(x,y);
	}
	return ;
}

Bresenham画线法:

image-20201215111332934

基本思想就这么简单, 后头都是对程序的优化

image-20201215111616680 image-20201215111747888 image-20201215112029646
void lineBres(int x0,int y0,int xEnd,int yEnd){
	int dx=fabs(xEnd-x0),dy=fabs(yEnd-y0);
	int p=2*dy-dx;
	int twoDy=2*dy,twoDyMinusDx=2*(dy-dx);
	int x,y;

	if(x0>xEnd){
		x=xEnd;
		y=yEnd;
		xEnd=x0;
	}
	else{
		x=x0;
		y=y0;
	}
	setPixel(x,y);
	while(x<xEnd){
		x++;
		if(p<0)
			p+=twoDy;
		else{
			y++;
			p+=twoDyMinusDx;
		}
		setPixel(x,y);
	}
}

Bresenham 画圆:

image-20201215112109829 image-20201215112119788 image-20201215112123090
class screenPt {
   private:
	GLint x, y;

   public:
	screenPt() { x = y = 0; }
	void setCoords(GLint xCoordValue, GLint yCoordValue) {
		x = xCoordValue;
		y = yCoordValue;
	}
	GLint getx() const { return x; }
	GLint gety() const { return y; }
	void incremetx() { x++; }
	void decrementy() { y--; }
};


void circleMidp(GLint xc, GLint yc, GLint radius) {
	screenPt circPt;
	GLint p = 1 - radius;
	circPt.setCoords(0, radius);
	void circlePlotPoints(GLint, GLint, screenPt);
	circlePlotPoints(xc, yc, circPt);
	while (circPt.getx() < circPt.gety()) {
		circPt.incremetx();
		if (p < 0)
			p += 2 * circPt.getx() + 1;
		else {
			circPt.decrementy();
			p += 2 * (circPt.getx() - circPt.gety()) + 1;
		}
		circlePlotPoints(xc, yc, circPt);
	}
}
void circlePlotPoints(GLint xc, GLint yc, screenPt circPt) {
	setPixel(xc + circPt.getx(), yc + circPt.gety());
	setPixel(xc - circPt.getx(), yc + circPt.gety());
	setPixel(xc + circPt.getx(), yc - circPt.gety());
	setPixel(xc - circPt.getx(), yc - circPt.gety());
	setPixel(xc + circPt.gety(), yc + circPt.getx());
	setPixel(xc - circPt.gety(), yc + circPt.getx());
	setPixel(xc + circPt.gety(), yc - circPt.getx());
	setPixel(xc - circPt.gety(), yc - circPt.getx());
}

完整程序:

运行环境: Qt

main.cpp

#define NDEBUG
#ifndef GLUT_DISABLE_ATEXIT_HACK
#define GLUT_DISABLE_ATEXIT_HACK
#endif
#include <windows.h>
#include <gl/glut.h>
#include <cmath>

inline int round (const float a){
	return int(a+0.5) ;
}

void setPixel(int xCoord, int yCoord){
	glBegin (GL_POINTS);
	glVertex2i (xCoord, yCoord);
	glEnd();
}

void lineDDA (int x0, int y0, int xEnd, int yEnd){
	int dx=xEnd-x0,dy=yEnd-y0,steps,k;
	float xIncrement,yIncrement,x=x0,y=y0;
	if(fabs(dx)> fabs(dy)){
		steps = fabs(dx);
	}else {
		steps = fabs(dy);
	}
	xIncrement = float(dx)/float(steps);
	yIncrement = float(dy)/ float(steps);
	setPixel(round(x),round(y));
	for(k = 0;k< steps;k++){
		x += xIncrement;
		y += yIncrement;
		setPixel(round(x),round(y));
	}
	return ;
}

void lineMiddle(int x0, int y0, int x1, int y1){
	int a,b,d1,d2,d,x,y;
	a=y0-y1,b=x1-x0,d=2*a+b;
	d1=2*a,d2=2*(a+b);
	x=x0,y=y0;
	setPixel (x,y);
	while (x<x1){
		if (d<0){
			x++;
			y++;
			d+=d2;
		}
		else{
			x++;
			d+=d1;
		}
		setPixel(x,y);
	}
	return ;
}


void lineBres(int x0,int y0,int xEnd,int yEnd){
	int dx=fabs(xEnd-x0),dy=fabs(yEnd-y0);
	int p=2*dy-dx;
	int twoDy=2*dy,twoDyMinusDx=2*(dy-dx);
	int x,y;

	if(x0>xEnd){
		x=xEnd;
		y=yEnd;
		xEnd=x0;
	}
	else{
		x=x0;
		y=y0;
	}
	setPixel(x,y);
	while(x<xEnd){
		x++;
		if(p<0)
			p+=twoDy;
		else{
			y++;
			p+=twoDyMinusDx;
		}
		setPixel(x,y);
	}
}

class screenPt {
   private:
	GLint x, y;

   public:
	screenPt() { x = y = 0; }
	void setCoords(GLint xCoordValue, GLint yCoordValue) {
		x = xCoordValue;
		y = yCoordValue;
	}
	GLint getx() const { return x; }
	GLint gety() const { return y; }
	void incremetx() { x++; }
	void decrementy() { y--; }
};


void circleMidp(GLint xc, GLint yc, GLint radius) {
	screenPt circPt;
	GLint p = 1 - radius;
	circPt.setCoords(0, radius);
	void circlePlotPoints(GLint, GLint, screenPt);
	circlePlotPoints(xc, yc, circPt);
	while (circPt.getx() < circPt.gety()) {
		circPt.incremetx();
		if (p < 0)
			p += 2 * circPt.getx() + 1;
		else {
			circPt.decrementy();
			p += 2 * (circPt.getx() - circPt.gety()) + 1;
		}
		circlePlotPoints(xc, yc, circPt);
	}
}
void circlePlotPoints(GLint xc, GLint yc, screenPt circPt) {
	setPixel(xc + circPt.getx(), yc + circPt.gety());
	setPixel(xc - circPt.getx(), yc + circPt.gety());
	setPixel(xc + circPt.getx(), yc - circPt.gety());
	setPixel(xc - circPt.getx(), yc - circPt.gety());
	setPixel(xc + circPt.gety(), yc + circPt.getx());
	setPixel(xc - circPt.gety(), yc + circPt.getx());
	setPixel(xc + circPt.gety(), yc - circPt.getx());
	setPixel(xc - circPt.gety(), yc - circPt.getx());
}


//绘制程序
void display(){
	glClear(GL_COLOR_BUFFER_BIT);//将屏幕设置为黑色
	glColor3f(0.0,0.4,0.2);//设置当前颜色状态为白色
	lineDDA (0,400,200,300);
	lineMiddle (0,300,350, 400);
	lineBres (0,0,150,400);
	circleMidp(200,200,100);
	glFlush();//发送缓冲区
	return ;
}

//初始化绘制
void init(){
	glClearColor(1.0,1.0,1.0,0.0);//清除颜色设置
	glMatrixMode(GL_PROJECTION);//设置投影方式
	gluOrtho2D (0.0,400.0,0.0,400.0);
	return ;
}

int main(int argc, char** argv){
	glutInit(&argc, argv);//初始化glut
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//设置显示模式为单缓冲,RGB模式
	glutInitWindowPosition(0,0);//设置窗口位置
	glutInitWindowSize(400,400);//设置窗口大小
	glutCreateWindow("GLUT_Project_1");//设置窗口标题
	init();
	glutDisplayFunc(display);
	glutMainLoop();
	return 0;
}

运行截图:

image-20200918185713588

  • 7
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值