深圳大学计算机图形学大作业实验报告

(2)圆:

Bresenham画圆:

CircleBresenham(CPoint center, int r) {

CClientDC dc(this); int x,y,d; x=0; y=r; d=3-2*r; while(x

}

} x=x+1; }

if (x==y) { dc.SetPixel(center.x+x,center.y+y,RGB(0,0,0)); dc.SetPixel(center.x-x,center.y+y,RGB(0,0,0)); dc.SetPixel(center.x+x,center.y-y,RGB(0,0,0)); dc.SetPixel(center.x-x,center.y-y,RGB(0,0,0)); dc.SetPixel(center.x+y,center.y+x,RGB(0,0,0)); dc.SetPixel(center.x-y,center.y+x,RGB(0,0,0)); dc.SetPixel(center.x+y,center.y-x,RGB(0,0,0)); dc.SetPixel(center.x-y,center.y-x,RGB(0,0,0)); }

(3)填充:

扫描线转换算法:

edge_scan(){ HeadE=NULL; for(CurrentB=HeadB; CurrentB!=NULL;CurrentB=CurrentB->next) { for (CurrentE=CurrentB->p;CurrentE!=NULL;CurrentE=CurrentE->next) {

上开

AET *TempEdge= new AET;

TempEdge->x=CurrentE->x; TempEdge->ymax=CurrentE->ymax; TempEdge->dx=CurrentE->dx; TempEdge->next=NULL; AddEdge(TempEdge); //将该边插入活性边表中 }

EdgeOrder(); //活性边表按照x值排序

T1=HeadE; //根据ymax抛弃扫描完的边节点 if (T1==NULL) { return; }

while(CurrentB->ScanLine>=T1->ymax) //放弃该节点,AEL表指针后移,下闭{ T1=T1->next; HeadE=T1; if (HeadE==NULL) { return; } }

if (T1->next!=NULL) { T2=T1; T1=T2->next; }

while (T1!=NULL) { if (CurrentB->ScanLine>=T1->ymax) //跳过一个节点 { T2->next=T1->next; T1->next=NULL; T1=T2->next; } else { T2=T1; T1=T2->next;

} } BOOL In= false; double xb,xe; //扫描线的起点与终点 for (T1=HeadE;T1!=NULL;T1=T1->next) //填充扫描线和多边形相交的区间 { if (In==false) { xb=T1->x; In=true; } else { xe=T1->x-1; //左闭右开 CClientDC dc(this); for (double x=xb;x<=xe; x++) { dc.SetPixel(INT(x),CurrentB->ScanLine,RGB(255,0,0)); } Sleep(1); //延时 1ms, 效果 In=false; } } for (T1=HeadE;T1!=NULL;T1=T1->next) { T1->x=T1->x+T1->dx; } }

delete HeadB; delete CurrentB; delete CurrentE; delete HeadE;

}

//填充语句

(4)二维裁剪: 多边形裁剪:

CohenSutherlandLineClip(float x0, float y0,float x1,float y1,CRect *rect) //P0(x0,y0),P1(x1,y1)为待裁剪线段 //rect 为裁剪窗口 {

//用于计算端点的编码 boolean accept,done;

OutCode outCode0,outCode1; OutCode * outCodeOut; float x,y;

accept = FALSE; done = FALSE;

CompOutCode(x0,y0,rect,&outCode0); CompOutCode(x1,y1,rect,&outCode1); do{ if (outCode0.all == 0 && outCode1.all == 0) //完全可见 { accept = TRUE; done = TRUE; } else if (outCode0.all&outCode1.all!=0) //显然不可见 done = TRUE; else //进行求交测试 { if(outCode0.all!=0) //判断哪一点位于窗口之外 outCodeOut=&outCode0; else outCodeOut=&outCode1;

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4