计算机图形学 4 代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
int xs,ys,xe,ye;
void LineDDA(int x0,int y0,int x1,int y1) {
float dy,dx,x,y,m;
dx=x1-x0;
dy=y1-y0;
if(dx!=0)
{
m=dy/dx;
if(m<=1&&m>=-1)
{
y=y0;
for(x=x0;x<=x1;x++)
{
glVertex2i(x,int(y+0.5));
y+=m;
}
}
if(m>1||m<-1)
{
m=1/m;
x=x0;
for(y=y0;y<=y1;y++)
{
glVertex2i(int(x+0.5),y);
x+=m;
}
}
}
else
{
int x=x0,y;
y=(y0<=y1)? y0:y1;
int d=fabs((double)(y0-y1));
while(d>=0)
{
glVertex2i(x,y);
y++;
d--;
}
}
}
void lineSeqment()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POINTS);
LineDDA(xs,ys,xe,ye);
glEnd();
glFlush();
}
void main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
printf("输入线段起始和终止点坐标(范围为0-500,0-500)");
scanf("%d,%d,%d,%d",&xs,&ys,&xe,&ye);
glutInitWindowPosition(50,100);
glutInitWindowSize(500,500);
glutCreateWindow("DDA画线算法");
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,500.0,0.0,500.0);
glutDisplayFunc(lineSegment);
glutMainLoop();
}
4-2
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
int xs,ys,xe,ye;
//x0和y为起始坐标,x1和y1为终止坐标
void MidpointLine(int x0,int y0,int x1,int y1)
{ if ( (x0!=x1) &&(y0!=1) )
{ int a, b, delta1, delta2, d, x, y;
a=y0-y1;
b=x1-x0;
d=2*a+b,
delta1=2*a,
delta2=2*(a+b),
x=x0;
y=y0;
glVertex2i(x,y);
while(x<x1)
{ if (d<0)
{ x++;
y++;
d+=delta2;
}
else
{ x++;
d+=delta1;
}
glVertex2i(x,y);
}
}
else
{ int min ,d;
if (x0==x1)
{ int x=x0,y;
y=(y0<y1)?y0:y1;
d=fabs( (double) (y0-y1) );
while(d>=0)
{ glVetex2i(x,y);
y++;
d--;
}
}
if(y0==y1)
{ int x,y==y0;
x=(x0<=x1)? x0:x1;
d=fabs((double)(x0-x1));
while(d>=0)
{ glVertex2i(x,y);
x++;
d--;
}
}
}
}
void lineSegment()
{ glClear(GL_COLOR_BUFFER_BIT);
glClolor3f(0.0, 0.0, 0.0)
glBegain (GL_POINTS);
MidpointLine(xs,ys,xe,ye);
glEnd();
glFlush();
}
void main (int argc, char *argv [])
{ glutInit (&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
printf(“输入线段起始和终止坐标(范围为0-500,0-500)”);
scanf (“%d, %d, %d, %d”,&xs,&ys,&xe,&ye);
glutIntWindowsPosition(50,100);
glutIntWindowsSize(500,500);
gluClearColor(1.0,1.0,1.0,0.0);
glMstrixMode(GL_PORJECTION);
gluOrtho2D(0.0,500.0,0.0,500.0);
glutDisplayFunc(lineSegment);
glutMainLoop();
}
4-3:
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
int xs,ys,xe,ye;
//x0和y0为起始点坐标,x1和y1为终止点坐标void BresenhamLine(int x0,int y0,int x1,int y1) {
if( (x0!=x1)&&(y0!=y1) )
{
int x,y,dx,dy,e,i;
dx=x1-x0;
dy=y1-y0;
e=-dx;x=x0;y=y0;
for(i=0;i<=dx;i++)
{
glVertex2i(x,y);
x=x+1;
e=e+dy+dy;
if(e>=0)

y=y+1;
e=e-dx-dx;
}
}
}
else
{
int min ,d;
if(x0==x1)
{
int x=x0,y;
y=(y0<=y1)?y0:y1;
d=fabs( (double) (y0-y1) );
whlie(d>=0)
{
glVertex2i(x,y+d);
d--;
}
}
if(y0==y1)
{
int x,y=y0;
x=(x0<=x1)? x0:x1;
d=fabs( (double) (x0-x1) );
whlie(d>=0)
{
glVertex2i(x+d,y);
d--;
}
}
}
}
void lineSegment ( )
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0,0.0,0.0);
glBegin (GL_POINTS);
BresenhamLine (xs,ys,xe,ye);
glEnd;
glFlush ( );
}
void main (int argc,char *argv[ ])
{
glutInit (&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
printf (“输入起始坐标和终止点坐标(范围为0-500,0-500)”);
scanf(“%d,%d,%d,%d”,&xs,&ys,&xe,&ye);
glutInitWindowPosition(50,100);
glutInitWindowSize(500,500);
glutCreateWindow(“Bresenham画线算法”);
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,500.0,0.0,500.0);
glutDisplayFunc(LineSegment);
glutMainLoop( );
}。

相关文档
最新文档