自学计算机图形学OpenGL(七)中点画线法

 思路:网格的中点在直线与网格的交点的上方,则选下面的点,若中点在直线的下方,则选上面的点。

算法我就不写了,因为我也没看懂...直接上代码

#include <glut.h>
#include<math.h>

GLdouble m1 = 0, m2 = 0;

//中点画线
void halfzd(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2) {
	double a = y1 + m2 - y2;
	double b = x2 - x1 - m1;
	double d = a + a + b;
	double d1 = a + a;
	double d2 = a + b + a + b;
	glBegin(GL_POINTS);
	glVertex2f(x1 + m1, y1 + m2);
	while (x1 + m1 < x2) {
		if (d < 0) {
			x1++; y1++; d += d2;
		}
		else {
			x1++; d += d1;
		}
		glVertex2f(x1 + m1, y1 + m2);
	}
	glEnd();
}

//绘制函数
void myDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glPointSize(1);
	glColor3f(0.9, 0.5, 0.8);
	halfzd(1.5, 3.8, 189.8, 267.5);
	glFlush();
}

void init()
{
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, 600, 0, 500);
}

void main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);    //设置显示模式
	glutInitWindowSize(300, 200); //设置窗口大小
	glutInitWindowPosition(600, 500);   //设置窗口在屏幕上的位置 
	glutCreateWindow("halfzd application"); //创建窗口并给出标题
	
	init();
	glutDisplayFunc(myDisplay);
	glutMainLoop();     //消息循环(处理操作系统等的消息,例如键盘、鼠标事件等)
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值