计算机图形学上机实验之DDV算法

#include <gl/glut.h>//实用工具包
#include<math.h>
#include<stdio.h>
#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glaux.h>
#include<stdlib.h>
struct GLintPoint{
	GLint x;
	GLint y;
};

GLintPoint p[2];
bool sig=false;
int scrw=640,scrh=480;

void drawline()
{
	int dx=p[1].x-p[0].x;
	int dy=p[1].y-p[0].y;
	int steps,k;
	float xIncrement,yIncrement;
	float x=p[0].x,y=p[0].y;
	if (fabs(dx)>fabs(dy))
	{
		steps=fabs(dx);
	}else 
	{
		steps=fabs(dy);
	}
	xIncrement=float(dx)/float(steps);//计算两个方向增量
	yIncrement=float(dy)/float(steps);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0f,0.0f,0.0f);
	glBegin(GL_LINES);
	glVertex2i(x,y);//绘制起点
	for(k=0;k<steps;k++)
	{
		x+=xIncrement;
		y+=yIncrement;
		glVertex2i(x,y);
	}
	glEnd();
	glFlush();
}
void myDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glColor3f(0.0,0.0,1.0);
	if(sig)drawline();
	glutSwapBuffers();
}
void  myMouse(int button,int state,int x,int y)
{
	if(button==GLUT_LEFT_BUTTON&& state==GLUT_DOWN)
	{
		p[0].x=x;
		p[0].y=scrh-y;
		sig=true;
	}
	glutPostRedisplay();
}
void myMotion(int x,int y)
{
	p[1].x=x;
	p[1].y=scrh-y;
	glutPostRedisplay();
}
int main(int argc,char **argv)
{
	glutInit(&argc,argv);
	glutInitWindowSize(scrw,scrh);
	glutInitWindowPosition(0,0);
	glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
    glutCreateWindow("gujinjie");
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0,scrw,0,scrh);
		glMatrixMode(GL_MODELVIEW);
	glClearColor(0.0f,0.0f,0.0f,0.0f);
	glViewport(0,0,scrw,scrh);
	glutMouseFunc(myMouse);
	glutDisplayFunc(myDisplay);
	glutMotionFunc(myMotion);
	glutMainLoop();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

&小鹏鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值