您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 图形学算法设计和流程图
DDA算法设计A、数据结构设计函数定义:DDA算法函数:voidCTestView::DDALine(intxa,intya,intxb,intyb){}方法调用函数:voidCTestView::OnDDALineFunc1(){}鼠标响应函数:voidCTestView::OnDDALineMouse1(){}鼠标按下事件:voidCTestView::OnLButtonDown(UINTnFlags,CPointpoint){}鼠标抬起事件:voidCTestView::OnLButtonUp(UINTnFlags,CPointpoint){}B、核心算法设计DDA算法:设直线起点(x0,y0),终点(x1,y1),k为直线斜率,因有直线公式为y=kx+b,则有斜率k=(y1-y0)/(x1-x0);当|k|=1时,y坐标:yi+1=yi+1,x坐标:xi+1=xi+1/k;当|k|1时,y坐标:yi+1=yi+k,x坐标:xi+1=xi+1。DDA算法(1)—输入DDA算法(2)—处理DDA算法(3)—输出DDA算法voidCTestView::OnDDALineFunc1(){//调用DDALine函数CTestView::DDALine(50,50,150,200);//起点(50,50),终点(150,200)}voidCTestView::OnDDALineMouse1(){//鼠标响应调用DDALine函数flag=5;}voidCTestView::OnLButtonDown(UINTnFlags,CPointpoint){//鼠标按下事件bx=point.x;//获取鼠标按下时的横坐标by=point.y;//获取鼠标按下时的横坐标CView::OnLButtonDown(nFlags,point);}voidCTestView::OnLButtonUp(UINTnFlags,CPointpoint){//鼠标抬起事件CClientDCdc(this);if(flag==5){CTestView::DDALine(bx,by,point.x,point.y);//鼠标响应调用DDALine函数}CView::OnLButtonUp(nFlags,point);}DDA算法voidCTestView::DDALine(intxa,intya,intxb,intyb){//DDALine函数算法CClientDCdc(this);floatx,y,k,dx,dy,temp;if(xa==xb){//当xa=xb时,k=0,直线垂直于y轴for(y=ya;y=yb;y++){dc.SetPixel(xa,y,RGB(255,0,0));}}else{//当xa!=xb时,计算斜率k,判断绝对值值域dx=(float)(xb-xa);dy=(float)(yb-ya);k=dy/dx;//求斜率DDA算法if(xaxb){if(xaxb){//确保直线从左向右画temp=xa;xa=xb;xb=temp;temp=ya;ya=yb;yb=temp;}y=ya;for(x=xa;x=xb;x++){//循环dc.SetPixel(x,(int)(y+0.5),RGB(255,0,0));//画点y=y+k;}}if(abs(k)=1){//判断k的绝对值,|k|=1时执行if(yayb){//确保直线从左向右画temp=xa;xa=xb;xb=temp;temp=ya;ya=yb;yb=temp;}x=xa;for(y=ya;y=yb;y++){//循环yi+1=yi+1dc.SetPixel((int)(x+0.5),y,RGB(255,0,0));//画点x=x+1/k;}}}}中点画圆算法A.数据结构设计:函数定义:中点画圆算法函数:voidCTestView::midpointcircle(intxa,intya,intxb,intyb){}鼠标按下事件:voidCTestView::OnLButtonDown(UINTnFlags,CPointpoint){}鼠标抬起事件:voidCTestView::OnLButtonUp(UINTnFlags,CPointpoint){}圆心在原点中点画圆事件:voidCTestView::Onzero1(){}圆心在任意点中点画圆事件:voidCTestView::Onanypoint1(){}B.中点画圆算法设计:函数为F(x,y)=x2+y2-r2的构造圆,半径r等于鼠标响应事件得到的亮点坐标的距离,即r=sqrt((xb-xa)*(xb-xa)+(yb-ya)*(yb-ya))。设d=F(x,y),判断d的符号,d=0的M点在圆上,d0的M点在圆外,d0的M点在圆内。当x=0、y=r时,d的初值为1.25-r,绘制该点和其在八分圆上的另外7个点。当d=0时,d更新为d+2x+3,(x,y)更新为(x+1,y);当d0时,d更新为d+2*(x-y)+5,(x,y)更新为(x+1,y+1)。当xy时,重复计算绘制点。中点画圆算法-流程图中点画圆算法-1voidCTestView::midpointcircle(intxa,intya,intxb,intyb){CClientDCdc(this);intfzbj=(xb-xa)*(xb-xa)+(yb-ya)*(yb-ya);intr=int(sqrt((double)fzbj)),c=0;intx,y;floatd;x=0;y=r;d=1.25-r;dc.SetPixel(xa+x,ya+y,c);dc.SetPixel(xa-x,ya+y,c);dc.SetPixel(xa+x,ya-y,c);dc.SetPixel(xa-x,ya-y,c);dc.SetPixel(xa+y,ya+x,c);dc.SetPixel(xa-y,ya+x,c);dc.SetPixel(xa+y,ya-x,c);dc.SetPixel(xa-y,ya+x,c);中点画圆算法-2while(x=y){if(d0)d+=2*x+3;else{d+=2*(x-y)+5;y--;}x++;dc.SetPixel(xa+x,ya+y,c);dc.SetPixel(xa-x,ya+y,c);dc.SetPixel(xa+x,ya-y,c);dc.SetPixel(xa-x,ya-y,c);dc.SetPixel(xa+y,ya+x,c);dc.SetPixel(xa-y,ya+x,c);dc.SetPixel(xa+y,ya-x,c);dc.SetPixel(xa-y,ya-x,c);}}voidCTestView::Onzero1(){flag=6;}voidCTestView::Onanypoint1(){flag=7;}中点画圆算法-3voidCTestView::OnLButtonDown(UINTnFlags,CPointpoint){bx=point.x;by=point.y;CView::OnLButtonDown(nFlags,point);}voidCTestView::OnLButtonUp(UINTnFlags,CPointpoint){CClientDCdc(this);elseif(flag==6){CTestView::midpointcircle(0,0,point.x,point.y);}elseif(flag==7){CTestView::midpointcircle(bx,by,point.x,point.y);}CView::OnLButtonUp(nFlags,point);}二维图形裁剪-线段裁剪计算点的code值流程图二维图形裁剪-线段裁剪计算直线与边界交点流程图二维图形裁剪-线段裁剪算法流程图1二维图形裁剪-线段裁剪算法流程图2二维图形裁剪-线段裁剪算法流程图总二维图形裁剪-1voidCTestView::OnCohenSutherland1(){flag=8;CClientDCdc(this);//边框dc.MoveTo(200,100);//LEFTdc.LineTo(200,400);dc.MoveTo(500,100);//RIGHTdc.LineTo(500,400);dc.MoveTo(200,100);//TOPdc.LineTo(500,100);dc.MoveTo(200,400);//BOTTONdc.LineTo(500,400);}voidCTestView::OnLButtonDown(UINTnFlags,CPointpoint){bx=point.x;by=point.y;CView::OnLButtonDown(nFlags,point);}二维图形裁剪-1voidCTestView::OnLButtonUp(UINTnFlags,CPointpoint){CClientDCdc(this);if(flag==8){dc.MoveTo(bx,by);dc.LineTo(point.x,point.y);ClipLine(bx,by,point.x,point.y);}CView::OnLButtonUp(nFlags,point);}intCTestView::encode(floatx,floaty){intc;c=0;if(xXL)c=c|LEFT;elseif(xXR)c=c|RIGHT;if(yYB)c=c|BOTTOM;elseif(yYT)c=c|TOP;returnc;}二维图形裁剪-2voidCTestView::ClipLine(floatx1,floaty1,floatx2,floaty2){CClientDCdc(this);floatx,y;intcode,code1,code2;code1=encode(x1,y1);code2=encode(x2,y2);while(code1!=0||code2!=0){if((code1&code2)!=0)return;code=code1;if(code1==0)code=code2;二维图形裁剪-3//当直线和左边界有交点if((LEFT&code)!=0){x=XL;y=y1+(y2-y1)*(XL-x1)/(x2-x1);}//当直线和右边界有交点elseif((RIGHT&code)!=0){x=XR;y=y1+(y2-y1)*(XR-x1)/(x2-x1);}//当直线和底边界有交点elseif((BOTTOM&code)!=0){y=YB;x=x1+(x2-x1)*(YB-y1)/(y2-y1);}//当直线与上边界有交点elseif((TOP&code)!=0){y=YT;x=x1+(x2-x1)*(YT-y1)/(y2-y1);}二维图形裁剪-4if(code==code1){x1=x;y1=y;code1=encode(x,y);}else{x2=x;y2=y;code2=encode(x,y);}}CPenpen,*old;pen.CreatePen(0,2,RGB(255,0,0));old=dc.SelectObject(&pen);dc.MoveTo(x1,y1);dc.LineTo(x2,y2);dc.SelectObject(old);}
本文标题:图形学算法设计和流程图
链接地址:https://www.777doc.com/doc-5305837 .html