计算机图形学第一次报告

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

计算机图形学第一次实习报告一、第一种方法的运行结果及代码(用openGL画图)

#include "stdafx.h"

#define GLUT_DISABLE_ATEXIT_HACK

#include

#include

void Intial(void)

{

glClearColor(1.0f,1.0f,1.0f,1.0f);//设计窗口的背景颜色为白色

glMatrixMode(GL_PROJECTION);//指定设置投影参数

gluOrtho2D(0.0,200.0,0.0,150.0);//设置投影参数

}

void DDAline(int x0,int y0,int x1,int y1)

{

int dx,dy,epsl,k;

float x,y,xIncre,yIncre;

dx = x1 - x0;

dy = y1 - y0;

x = x0;

y = y0;

if(abs(dx) > abs(dy))epsl = abs(dx);

else epsl = abs(dy);

xIncre = (float)dx/epsl;

yIncre = (float)dy/epsl;

for(k = 0;k <= epsl;k++)

{

glVertex2f(x,y);

x += xIncre;

y += yIncre;

}

}

void BresenhamLine(int x0,int y0,int x1,int y1) {

int x,y,dx,dy,e;

dx = x1 - x0;

dy = y1 - y0;

e = -dx;

x = x0;y = y0;

while(x <= x1)

{

glVertex2f(x,y);

x++;

e = e + 2*dy;

if(e > 0){

y ++;

e = e - 2*dx;

}

}

}

void CirclePoint(int x,int y)

{

glVertex2f(x+60,y+60);

glVertex2f(y+60,x+60);

glVertex2f(-x+60,y+60);

glVertex2f(-y+60,x+60);

glVertex2f(-x+60,-y+60);

glVertex2f(-y+60,-x+60);

glVertex2f(x+60,-y+60);

glVertex2f(y+60,-x+60);

}

void MidBresenham(int r)

{

int x,y,d;

x = 0;

y = r;

d = 1-r;

while(x <= y)

{

CirclePoint(x,y);

if(d<0) d+=2*x+3;

else{

d+=2*(x-y)+5;

y--;

}

x++;

}

}

void MidBresenhamEllipse(int a,int b) {

int x,y;

float d1,d2;

x=0;y=b;

d1=b*b+a*a*(-b+0.25);

glVertex2f(x+150,y+70);

glVertex2f(-x+150,-y+70);

glVertex2f(-x+150,y+70);

glVertex2f(x+150,-y+70);

while(b*b*(x+1)

{

if(d1<=0)

{

d1+=b*b*(2*x+3);

x++;

}

else

{

d1+=b*b*(2*x+3)+a*a*(-2*y+2);

x++;

y--;

}

glVertex2f(x+150,y+70);

glVertex2f(-x+150,-y+70);

glVertex2f(-x+150,y+70);

glVertex2f(x+150,-y+70);

}

d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;

while(y>0)

{

if(d2<=0)

{

d2+=b*b*(2*x+2)+a*a*(-2*y+3);

x++;y--;

}

else

{

d2+=a*a*(-2*y+3);

y--;

}

glVertex2f(x+150,y+70);

glVertex2f(-x+150,-y+70);

glVertex2f(-x+150,y+70);

glVertex2f(x+150,-y+70);

}

}

void display()

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f,0.0f,0.0f);

glBegin(GL_POINTS);

glPointSize(3);

DDAline(100, 1, 1, 300 ); //DDA方法画直线

BresenhamLine(50,1,400,300); //Bresenham方法画直线

MidBresenham(50); //中点Bresenham方法画圆MidBresenhamEllipse(35,50); //中点Bresenham方法画椭圆glEnd();

glEnd();

glFlush();

}

相关文档
最新文档