dda算法c语言opengl实现

#include <GL/glut.h>
#include <stdio.h>
#include <cstdlib>

void dda_line(int x1, int y1, int x2, int y2) {
	glPointSize(1.0);
	glBegin(GL_POINTS);
	glVertex2i(x1, y1);
	glVertex2i(x2, y2);
	// x,y的增量
	int dx = x2 - x1;
	int dy = y2 - y1;
	double dert_x;
	double dert_y;
	double x, y;
	int steps;
	// 步长,看x和y哪个大
	if (abs(dx) > abs(dy))
		steps = abs(dx);
	else
		steps = abs(dy);
	// 根据步长,算x和y的增量
	dert_x = (GLdouble)dx / (GLdouble)steps;
	dert_y = (GLdouble)dy / (GLdouble)steps;
	x = x1;
	y = y1;
	for (int i = 1; i <= steps; i++) {
		x += dert_x;
		y += dert_y;
		glVertex2f(x, y);
	}
	glEnd();
}
void myDisplay(void) {
	glClear(GL_COLOR_BUFFER_BIT);
	dda_line(250, 250, 350, 450);
	glFlush();
}
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(500, 200);
	glutInitWindowSize(500, 500);
	glutCreateWindow("DDA");
	gluOrtho2D(0.0, 500.0, 0.0, 500.0);
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值