资源描述:
《图形学画圆实验报告.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、4.2.3程序实现与上机实习(二)一、实验目的编写圆和椭圆的扫描转换算法程序,验证算法的正确性。二、实验任务1.编写中点画圆法的扫描转换程序,考虑原点在(x0,y0)处程序的改动;2.添加鼠标程序,实现交互式画圆;3.编写中点画椭圆法的扫描转换程序;4.添加鼠标程序,实现交互式画椭圆;三、实验内容1.编写中点画圆法的扫描转换程序,考虑原点在(x0,y0)处程序的改动;分析:考虑圆心不再原点,设圆心坐标为(x0,y0)。通过平移坐标原点到圆心,则第二个8分圆上一点p(x,y),其原始坐标为x’=x+x0y’=y+y0即p’1(
2、x0+x,y+y0)其它7个对称点分别是:p’2(x0+y,y+x0),p’3(x0+y,y0-x),p’4(x0+x,y0-y),p’5(x0-x,y0-y),p’6(x0-y,y0-x),p’7(x0-y,y0+x),p’8(x0-x,y0+y)O(0,0)YX(x0,y0)R(x0+R,y0)p(x,y)X’Y’p’1(x0+x,y+y0)p’2(x0+y,y+x0)p’3(x0+y,y0-x)p’4(x0+x,y0-y))p’5(x0-x,y0-y)p’6(x0-y,y0-x)p’7(x0-y,y0+x)p’8(x0
3、-x,y0+y)算法程序如下:MidpointCircle(intx0,inty0,intr,intcolor){intx,y;floatd;x=0;y=r;d=1.25-r;CirPot(x0,y0,x,y,color);while(x<=y){if(d<0){d+=2*x+3;x++;}else{d+=2*(x-y)+5;x++;y--;}CirPot(x0,y0,x,y,color);}/*while*/}/*MidpointCiecle*/intCirPot(intx0,inty0,intx,inty,intcolo
4、r){Setpixel((x0+x),(y0+y));Setpixel((x0+y),(y0+x));Setpixel((x0+y),(y0-x));Setpixel((x0+x),(y0-y));Setpixel((x0-x),(y0-y));Setpixel((x0-y),(y0-x));Setpixel((x0-y),(y0+x));Setpixel((x0-x),(y0+y));}程序实现步骤:(1)建立MidPointCircle工程文件;(2)右击CMidPointCircleView类,建立成员函数voidMi
5、dpointCircle(CDC*pDC,intx0,inty0,intr,COLORREFcolor)intCirPot(CDC*pDC,intx0,inty0,intx,inty,COLORREFcolor)(3)编写成员函数代码,程序如下:voidCMidPointCircleView::MidpointCircle(CDC*pDC,intx0,inty0,intr,COLORREFcolor){intx,y;floatd;x=0;y=r;d=1.25-r;CirPot(pDC,x0,y0,x,y,color);whi
6、le(x<=y){if(d<0){d+=2*x+3;x++;}else{d+=2*(x-y)+5;x++;y--;}CirPot(pDC,x0,y0,x,y,color);}/*while*/}intCMidPointCircleView::CirPot(CDC*pDC,intx0,inty0,intx,inty,COLORREFcolor){pDC->SetPixel((x0+x),(y0+y),color);pDC->SetPixel((x0+y),(y0+x),color);pDC->SetPixel((x0+y),(
7、y0-x),color);pDC->SetPixel((x0+x),(y0-y),color);pDC->SetPixel((x0-x),(y0-y),color);pDC->SetPixel((x0-y),(y0-x),color);pDC->SetPixel((x0-y),(y0+x),color);pDC->SetPixel((x0-x),(y0+y),color);return0;}(4)编写OnDraw(CDC*pDC)函数,程序如下:voidCMidPointCircleView::OnDraw(CDC*pDC)
8、{CMidPointCircleDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahereMidpointCircle(pDC,100,100,10,RGB(255,0,0));Midpoint