《计算机图形学》课内实验报告实验二Word文档格式.docx

上传人:b****3 文档编号:6835791 上传时间:2023-05-07 格式:DOCX 页数:36 大小:172.71KB
下载 相关 举报
《计算机图形学》课内实验报告实验二Word文档格式.docx_第1页
第1页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第2页
第2页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第3页
第3页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第4页
第4页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第5页
第5页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第6页
第6页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第7页
第7页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第8页
第8页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第9页
第9页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第10页
第10页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第11页
第11页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第12页
第12页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第13页
第13页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第14页
第14页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第15页
第15页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第16页
第16页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第17页
第17页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第18页
第18页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第19页
第19页 / 共36页
《计算机图形学》课内实验报告实验二Word文档格式.docx_第20页
第20页 / 共36页
亲,该文档总共36页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

《计算机图形学》课内实验报告实验二Word文档格式.docx

《《计算机图形学》课内实验报告实验二Word文档格式.docx》由会员分享,可在线阅读,更多相关《《计算机图形学》课内实验报告实验二Word文档格式.docx(36页珍藏版)》请在冰点文库上搜索。

《计算机图形学》课内实验报告实验二Word文档格式.docx

1.DDA算法画线

图1DDA算法画线

源程序:

#include<

graphics.h>

#include<

conio.h>

math.h>

#defineROUND(a)((int)(a+0.5))

#defineOX320

#defineOY240

voidlineDDA(intxa,intya,intxb,intyb,intcolor);

voidsetpixel(intx,inty,intcolor);

main(){

intgdrive=DETECT,gmode=0;

initgraph(&

gdrive,&

gmode,"

d:

\\tc"

);

setbkcolor(BLACK);

line(0,OY,2*OX,OY);

line(OX,0,OX,2*OY);

lineDDA(300,300,0,0,WHITE);

getch();

closegraph();

return0;

}

voidlineDDA(intxa,intya,intxb,intyb,intcolor){

intdx=xb-xa;

intdy=yb-ya;

intsteps,i;

floatxIncrement,yIncrement;

floatx=xa;

floaty=ya;

if(abs(dx)>

abs(dy))

steps=abs(dx);

else

steps=abs(dy);

xIncrement=dx/(float)steps;

yIncrement=dy/(float)steps;

putpixel(ROUND(x),ROUND(y),color);

for(i=0;

i<

steps;

i++){

x+=xIncrement;

y+=yIncrement;

setpixel(ROUND(x),ROUND(y),color);

}

return;

voidsetpixel(intx,inty,intcolor){

putpixel(OX+x,OY-y,color);

2.中点bresenham算法画线

图2中点算法输入

图3中点算法画线

stdio.h>

/**********生成直线段的中点算法**********/

voidMidPointLine(intx1,inty1,intx2,inty2,intcolor)

{

floatx,y,dx,dy;

floatk,d;

floatIncrE,IncrNE;

dx=x2-x1;

dy=y2-y1;

k=dy/dx;

if(k>

=-1&

&

k<

=1)

{

d=dx-2*dy;

IncrE=-2*dy;

IncrNE=2*(dx-dy);

x=x1;

y=y1;

putpixel(x,y,color);

while(x<

x2)

{

if(d>

0)

d+=IncrE;

else

{

d+=IncrNE;

y++;

}

x++;

putpixel((int)x,(int)y,color);

}

d=dy-2*dx;

IncrE=-2*dx;

IncrNE=2*(dy-dx);

while(y<

y2)

x++;

y++;

/**********主函数**********/

voidmain()

intgraphdriver=DETECT,graphmode;

intx1,y1,x2,y2,color;

printf("

PleaseInputTwoPixels(x1,y1),(x2,y2):

"

scanf("

%d%d%d%d"

&

x1,&

y1,&

x2,&

y2);

InputColorlike'

RED'

or'

WHITE'

:

%d"

&

color);

graphdriver,&

graphmode,"

MidPointLine(x1,y1,x2,y2,color);

outtextxy(x1,y1,"

StartPoint"

outtextxy(x2,y2,"

EndPoint"

getch();

3.bresenham算法画线

图4bresenham算法画线

gl/glut.h>

voiddraw_pixel(intix,intiy)

glBegin(GL_POINTS);

glVertex2i(ix,iy);

glEnd();

voidBresenham(intx1,inty1,intxEnd,intyEnd)

intdx=abs(xEnd-x1),dy=abs(yEnd-y1);

intp=2*dy-dx;

inttwoDy=2*dy,twoDyMinusDx=2*dy-2*dx;

intx,y;

if(x1>

xEnd)

x=xEnd;

y=yEnd;

xEnd=x1;

y=y1;

draw_pixel(x,y);

while(x<

x++;

if(p<

p+=twoDy;

else

p+=twoDyMinusDx;

draw_pixel(x,y);

voiddisplay()

glClear(GL_COLOR_BUFFER_BIT);

Bresenham(0,0,400,400);

glFlush();

voidmyinit()

glClearColor(0.8,1.0,1.0,1.0);

glColor3f(0.0,0.0,1.0);

glPointSize(1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,500.0,0.0,500.0);

voidmain(intargc,char**argv)

glutInit(&

argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(500,500);

glutInitWindowPosition(200.0,200.0);

glutCreateWindow("

CG_test_Bresenham_Lineexample"

glutDisplayFunc(display);

myinit();

glutMainLoop();

4.扫描线算法填充

图5扫描线算法填充

stdlib.h>

windows.h>

GL/glut.h>

#defineNULL0//C++中没有NULL这个符号常量,这里用宏定义

#defineWINDOW_HEIGHT400//定义窗口高为400

#defineWINDOW_WIDTH400//定义窗口宽为400

structdcPt{//dcPt实际上是一个点的结构体

intx;

inty;

};

voidsetPixel(GLintx,GLinty)//用OpenGL函数改写setPixel

glVertex2i(x,y);

typedefstructtEdge{

intyUpper;

floatxIntersect,dxPerScan;

structtEdge*next;

}Edge;

/*InsertsedgeintolistinorderofincreasingxIntersectfield.*/

voidinsertEdge(Edge*list,Edge*edge)

Edge*p,*q=list;

p=q->

next;

while(p!

=NULL){

if(edge->

xIntersect<

p->

xIntersect)

p=NULL;

else{

q=p;

p=p->

edge->

next=q->

q->

next=edge;

/*Foranindex,returny-coordinateofnextnonhorizontalline*/

intyNext(intk,intcnt,dcPt*pts)

intj;

if((k+1)>

(cnt-1))

j=0;

j=k+1;

while(pts[k].y==pts[j].y)

if((j+1)>

j=0;

j++;

return(pts[j].y);

/*Storelower-ycoordinateandinverseslopeforeachedge.Adjustandstoreupper-ycoordinateforedgesthatarethelowermemberofamonoticallyincreasingordecreasingpairofedges*/

voidmakeEdgeRec

(dcPtlower,dcPtupper,intyComp,Edge*edge,Edge*edges[])

dxPerScan=

(float)(upper.x-lower.x)/(upper.y-lower.y);

xIntersect=lower.x;

if(upper.y<

yComp)

edge->

yUpper=upper.y-1;

yUpper=upper.y;

insertEdge(edges[lower.y],edge);

voidbuildEdgeList(intcnt,dcPt*pts,Edge*edges[])

Edge*edge;

dcPtv1,v2;

inti,yPrev=pts[cnt-2].y;

v1.x=pts[cnt-1].x;

v1.y=pts[cnt-1].y;

cnt;

v2=pts[i];

if(v1.y!

=v2.y){/*nonhorizontalline*/

edge=(Edge*)malloc(sizeof(Edge));

if(v1.y<

v2.y)/*up-goingedge*/

makeEdgeRec(v1,v2,yNext(i,cnt,pts),edge,edges);

else/*down-goingedge*/

makeEdgeRec(v2,v1,yPrev,edge,edges);

yPrev=v1.y;

v1=v2;

voidbuildActiveList(intscan,Edge*active,Edge*edges[])

Edge*p,*q;

p=edges[scan]->

while(p){

q=p->

insertEdge(active,p);

p=q;

voidfillScan(intscan,Edge*active)

Edge*p1,*p2;

inti;

p1=active->

while(p1){

p2=p1->

for(i=p1->

xIntersect;

p2->

i++)

setPixel((int)i,scan);

p1=p2->

voiddeleteAfter(Edge*q)

Edge*p=q->

next=p->

free(p);

/*Deletecompletededges.Update'

xIntersect'

fieldforothers*/

voidupdateActiveList(intscan,Edge*active)

Edge*q=active,*p=active->

while(p)

if(scan>

=p->

yUpper){

deleteAfter(q);

p->

xIntersect=p->

xIntersect+p->

dxPerScan;

voidresortActiveList(Edge*active)

Edge*q,*p=active->

active->

next=NULL;

voidscanFill(intcnt,dcPt*pts)

Edge*edges[WINDOW_HEIGHT],*active;

inti,scan;

WINDOW_HEIGHT;

edges[i]=(Edge*)malloc(sizeof(Edge));

edges[i]->

buildEdgeList(cnt,pts,edges);

active=(Edge*)malloc(sizeof(Edge));

for(scan=0;

scan<

scan++){

buildActiveList(scan,active,edges);

if(active->

next){

fillScan(scan,active);

updateActiveList(scan,active);

resortActiveList(active);

Sleep(20);

//为了放慢填充速度,便于观看填充过程,每填充一行停顿10毫秒,Sleep函数包含在头文件windows.h里面

/*Freeedgerecordsthathavebeenmalloc'

ed...*/

voidinit(void)

glClearColor(1.0,1.0,1.0,0.0);

glMatrixMode(GL_PROJECTION);

gluOrtho2D(0.0,WINDOW_WIDTH,0.0,WINDOW_HEIGHT);

voidmyDraw(void)

dcPtpts[]={//pts是表示填充图元的顶点数组,这里定义了一个六边形

50,50,

300,20,

300,300,

200,100,

150,350,

20,120

};

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);

scanFill(6,pts);

//第一个参数为填充图元的顶点数,第二个参数为顶点坐标数组

glFlush();

voidmain(intargc,char**argv)

glutInit(&

argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowPosition(50,100);

glutInitWindowSize(WINDOW_WIDTH,WINDOW_HEIGHT);

glutCreateWindow("

通用扫描线填充"

init();

glutDisplayFunc(myDraw);

glutMainLoop();

5.隐藏面消除算法

图6隐藏面消除算法输入

图7隐藏面消除算法绘图

gl/gl.h>

gl/glaux.h>

gl/glu.h>

iostream.h>

#defineSCAN_NUM500

typedefstructEdge//边Y桶中的边结构

//边的最大y值

floatxInt,dx;

//边的最低点的x值,之所以是最低点,是因为本程序是从下往上扫描的

intE_mark;

floatz,dzx,dzy;

structEdge*next;

//指向下一条边节点的指针

}Edge;

typedefstructpoint//定义一个点的结构体

floatz;

}point;

externpoint**points=NULL;

//存放多个多边形的顶点信息

externfloat**as=NULL;

//存放多边形所在平面的方程的四个系数

externint*counts=NULL;

//存放各个多边形的顶点个数

externintnum=0;

//记录多边形个数

voidDrawPoint(intx,inty)//画点函数

:

glBegin(GL_POINTS);

glVertex2d(x,y);

glEnd();

intnexty(intk,intcount,point*pts)//当前测试点的下一个点的纵坐标,其方向为顺时针

if((k+1)>

(count-1))

j=0;

elsej=k+1;

while(pts[k].y==pts[j].y)

if((j+1)>

j=0;

else

returnpts[j].y;

voidi

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 行政公文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2