计算机图形学学习总结

发布时间 : 星期五 文章计算机图形学学习总结更新完毕开始阅读

y = y+1;

while (x0<=xr) {

spanNeedFill=FALSE;

//while((pDC->GetPixel(x0,y)!=RGB(0,0,255))&&(pDC->GetPixel(x0,y)!=newcolor)&&(x0GetPixel(x0,y)==oldcolor)

{ if (spanNeedFill==FALSE) spanNeedFill=TRUE; x0++; }

if(spanNeedFill) {

//if ((x0==xr)&&(pDC->GetPixel(x0,y)!=RGB(0,0,255))&&(pDC->GetPixel(x0,y)!=newcolor)) if (x0==xr)

{ stackpush(x0); stackpush(y);} else

{stackpush(x0-1); stackpush(y);}

spanNeedFill=FALSE; } //end if

xnextspan=x0;

while(pDC->GetPixel(x,y)!=oldcolor && x0

}//End of while(!isstackempty()) }

步骤6:编写OnDraw()函数

绘制多边形,设置种子位置和填充颜色。

void CSampleseedfillView::OnDraw(CDC* pDC) {

CSampleseedfillDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc);

// TODO: add draw code for native data here //定义画笔颜色

CPen PenRed(PS_SOLID,1,RGB(255,0,0));//定义红色笔 CPen PenBlue(PS_SOLID,1,RGB(0,0,255));//定义蓝色笔 int x,y;

//用蓝色笔画出多变形的边界 pDC->SelectObject(&PenBlue);

POINT polygon[5]={{300,120},{390,160},{430,320},{180,300},{150,240}}; pDC->Polygon(polygon,5);

//定义种子位置,背景色oldcolor和填充色newcolor x=300;y=200;

COLORREF newcolor=RGB(255,0,0); COLORREF oldcolor=RGB(255,255,255); //调用扫描线填充算法程序

floodfill4(x,y, oldcolor,newcolor); }

步骤7:编译、调试,运行程序。

实验五

一、实验目的

编写线段裁剪算法程序,验证算法的正确性。

21

二、实验任务

1. 编码裁剪算法的程序设计;

2. 要求用鼠标画线技术,实现交互式裁剪效果; 三、实验步骤

任务一:编码裁剪算法的程序设计

步骤1:创建Code_Clip工程文件;

步骤2:在主程序的程序头部定义符号常量 #define LEFT 1 #define RIGHT 2 #define BOTTOM 4 #define TOP 8

步骤3:定义成员变量和成员函数 int WT; int WB; int WR; int WL;

int C_S_Line(CDC* pDC,int x1,int y1,int x2,int y2); void encode(int x,int y,int *code);

步骤4:在构造函数中为窗口边界变量赋初值 CCode_ClipView::CCode_ClipView() {

// TODO: add construction code here WL=100;WR=400;WB=100;WT=300; }

步骤5:编写成员函数程序

void CCode_ClipView::encode(int x, int y, int *code) {

int c=0;

if (x

else if (x>WR) c=c|RIGHT; if (yWT) c=c|TOP; *code=c; }

int CCode_ClipView::C_S_Line(CDC* pDC,int x1, int y1, int x2, int y2) {

// CDC *pDC=GetDC(); int code1,code2,code,x,y;

encode(x1,y1,&code1); //(x1,y1)处的编码 encode(x2,y2,&code2); //(x1,y1)处的编码

while (code1!=0||code2!=0) //当code1不等于0或code2不等于0 {

if ((code1&code2)!=0) return 0; //当code1与 code2不等于0,在同侧;code=code1;

if (code1==0) code=code2; if ((LEFT&code)!=0) //求交点 {

x=WL;

y=y1+(y2-y1)*(WL-x1)/(x2-x1); }

else if ((RIGHT&code)!=0) {

x=WR;

y=y1+(y2-y1)*(WR-x1)/(x2-x1);

22

}

else if ((BOTTOM&code)!=0) {

y=WB;

x=x1+(x2-x1)*(WB-y1)/(y2-y1); }

else if ((TOP&code)!=0) {

y=WT;

x=x1+(x2-x1)*(WT-y1)/(y2-y1); }

if (code==code1) {x1=x;y1=y;

encode(x,y,&code1); } else {

x2=x;y2=y;

encode(x,y,&code2); } }

//end while,表示code1,code2都为0,其中的线段为可视部分pDC->MoveTo(x1,y1); pDC->LineTo(x2,y2); }

步骤6:编写OnDraw()程序

void CCode_ClipView::OnDraw(CDC* pDC) {

CCode_ClipDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc);

// TODO: add draw code for native data here //定义三条直线的坐标

int x11,y11,x21,y21,x12,y12,x22,y22,x13,y13,x23,y23; x11=50;y11=150;x21=450;y21=250; x12=150;y12=150;x22=350;y22=240; x13=50;y13=400;x23=500;y23=350; //定义画笔

CPen PenRed(PS_SOLID,1,RGB(255,0,0));//定义红色笔 CPen PenBlue(PS_SOLID,1,RGB(0,0,255));//定义蓝色笔 //先画出窗口,用蓝色

pDC->SelectObject(&PenBlue); pDC->Rectangle(WL,WB,WR,WT); //先画出三条直线,用红线 pDC->SelectObject(&PenRed);

pDC->MoveTo(x11,y11);pDC->LineTo(x21,y21); pDC->MoveTo(x12,y12);pDC->LineTo(x22,y22); pDC->MoveTo(x13,y13);pDC->LineTo(x23,y23); //用蓝线,画出裁剪三条线 pDC->SelectObject(&PenBlue); C_S_Line(pDC,x11,y11,x21,y21); C_S_Line(pDC,x12,y12,x22,y22); C_S_Line(pDC,x13,y13,x23,y23); }

步骤7:编译、调试,查看运行结果。

23

任务二:用鼠标实现交互式裁剪效果

步骤1:建立工程文件Mouse_Code_Clip; 步骤2:在主程序的程序头部定义符号常量 #define LEFT 1 #define RIGHT 2 #define BOTTOM 4 #define TOP 8

步骤3:定义成员变量和成员函数 //窗口的上、下、左、右边界 int WT; int WB; int WR; int WL; int m_ist; CPoint m_p2; CPoint m_p1;

int C_S_Line(CDC* pDC,int x1,int y1,int x2,int y2); void encode(int x,int y,int *code);

步骤4:在构造函数中为窗口边界变量赋初值 CCode_ClipView::CCode_ClipView() {

// TODO: add construction code here WL=100;WR=400;WB=100;WT=300; m_p1.x=0; m_p1.y=0; m_p2.x=0; m_p2.y=0;

m_ist=0; //m_ist=0表示起点,m_ist=1表示终点 }

步骤5:编写成员函数程序

void CCode_ClipView::encode(int x, int y, int *code) {

int c=0;

if (x

else if (x>WR) c=c|RIGHT; if (yWT) c=c|TOP; *code=c; }

int CCode_ClipView::C_S_Line(CDC* pDC,int x1, int y1, int x2, int y2) {

int code1,code2,code,x,y;

encode(x1,y1,&code1); //(x1,y1)处的编码 encode(x2,y2,&code2); //(x1,y1)处的编码

while (code1!=0||code2!=0) //当code1不等于0或code2不等于0 {

if ((code1&code2)!=0) return 0; //当code1与 code2不等于0,在同侧;code=code1;

if (code1==0) code=code2; if ((LEFT&code)!=0) //求交点 {

x=WL;

y=y1+(y2-y1)*(WL-x1)/(x2-x1); }

else if ((RIGHT&code)!=0) {

x=WR;

y=y1+(y2-y1)*(WR-x1)/(x2-x1);

24

联系合同范文客服:xxxxx#qq.com(#替换为@)