2023年计算机图形学实验报告新编.pdf

上传人:无*** 文档编号:92970184 上传时间:2023-06-18 格式:PDF 页数:28 大小:1.27MB
返回 下载 相关 举报
2023年计算机图形学实验报告新编.pdf_第1页
第1页 / 共28页
2023年计算机图形学实验报告新编.pdf_第2页
第2页 / 共28页
点击查看更多>>
资源描述

《2023年计算机图形学实验报告新编.pdf》由会员分享,可在线阅读,更多相关《2023年计算机图形学实验报告新编.pdf(28页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、实验一直线与圆的绘制(一)实验目的掌握用B e se n h a m 法编程实现直线和圆的绘制。会编程绘制虚线、点划线和具有一定宽度的直线。(二)实验内容用实现直线和圆的绘制基本规定:(1)数据输入项为:直线的起点与终点坐标,圆心坐标与半径(2)直线与圆输出在中附加规定:(1)通过用户输入可改变直线的线型(实线、虚线与点划线)(2)通过用户输入可改变直线的线宽(用方刷子解决)(3)通过用户输入可改变直线和圆的颜色(三)实验所用仪表及设备使用实验室提供的PC机。使用V i s u a l C+编程。(四)实验环节1.设计思绪1)设计对话框类2)设计菜单3)设计 CM y dra wVi e w

2、类数据成员pro t e c t e d:d o u b le x 0,y 0,x 1,yl,R;直线始点与终点、圆的半径in t cx,c y;/圆的坐标;成员函数vo i d Mybline();/直线中点 Bresen h a m 函数v o i d C i rclePoin t(dou b le x,d o uble y);/l 分子画圆子函数void Mb c ircle();圆中点 Bre s enh a m 函数4)程序代码v o i d CMyd r a wV i e w:Mybline()/中点 B resen h am 函数(SCCIi e n tDC d c(t h i

3、s);(3COL ORREF r g b=RG B(0,2 5 5,0);/定义直线颜色为蓝色0d ou b I e x,y,d,k,t:x=x 0;y=y 0;0if(a b s(xl-xO)le-6)(if(ylyO)(0 t=y 0;yO=yl;y 1 =t;00)0for(x=x 0,y=y0;y 1 )00Sif(y 1 y 0)S t=yO;y 0=y 1;y l=t;S00 t=x 0;x 0=xl;x 1 =t:m 00 d=l-0.5*k;目 for(x=xO/y=y 0;y=O)a 000X+;00 1 3 d+=1 k;00 01 3 e ls e0 0d+=1;000S

4、)ra f(0=k&kxl)0但 00t=xO;x 0=x 1 ;xl=t;团 t=yO;y 0=y 1;y l=t;TOSd=0.5-k;03Sfor(x=x 0,y=yO;xxl;x+)S S(d c.Se t PixelfROUN D(x),ROU N D(y),r g b);B 00 i f(d=-l&kxl)000t=xO;xO=x 1 ;xl=t;励团 t=yO;y 0=y 1;y 1 =t;0)0d=-O.5-k;0 0fo r(x=x 0zy=y O;x0)0 0 团 y-;0 d=l-k;m )由 elseE0 d-=-k;000)i f(k-l)明团i f(yOyl;y-)

5、m(0 d c.SetPixel(ROUND(x),ROUND(y),rgb);函 if(d S etWi n d o wText(基本图形扫描转换:Mybli n e );/R ed r a w Wi n dow();0My b line();v o i d CMy d rawVi e w::Mbc i rc 1 e()圆中点 Br e se n h a m 函数(0d o u b 1 e x,y,d;d=l.2 5 R;x=0;y=R;0for(x=O;x y;x+)(但 C ir c 1 ePoint(x,y);团 if(dSetWind o wText(基本图形扫描转换:Mbcir c

6、 1 e );Mb c ircle();)vo i d CMy d rawView:OnM E NUITEM c lear()清屏菜单函数/TODO:Add your command han d 1 er code hereSRed r awWindow();5)运营结果直线:(五)思考题如何修改程序使其适合于当直线斜率大于一或小于零时的情况?答:分组讨论大于一和小于零的情况,分别计算机d的值,程序如下:i f (k 1 )。oif(y l y O)a t=y O;y 0 =y 1 ;y 1 =t;”=xO;xO=x l;x 1 =t?d=1 -0 .5*k;。f o r(x=xO,y=y O

7、;y =0)f lx+;。d+=l-k?g e l s e2 d+=1 ;0 J o)i f (k =-l&k x 1)O f i j o t =x0:xO=x1:xl=t ;q t =y 0 ;y 0=y l;y l=t;g d=-0.5 -k:。o f or(x=x 0 ,y=y 0;x 0)。o(j-=1 -k;)。e ls e d-=-k;。)“f (k v-1)。if(y 0 。皿。dc.S e t P ixe l(R O U N D (x),。if (dC Bitm a p Bitmap,*pBitma p;Bitmap.LoadBitma p(IDB_B I TM AP1);。C

8、DC MemDC;oMemDC.CreateComp a tibleDC(G e t D C();叩Bitmap=MemDC.S el e ct 0 b j e c t(&Bitmap):MemDC.Bit B lt(O,O,R e c t.Wid t h(),R e c t.H e igh t(),&Pic t ure,0,0,SRC C OPY);。MemDC.TextO u t(wxl+wxr)/2,wyb-20J窗口)窗 口标题。/绘制窗口和直线C P e n P e n3,*p O 1 d Pen 3;/定义3个像素宽度的画笔Pen 3.C rea t e P e n(P S_SO

9、LID,3,RG B(0,0,0);pOld P en3=Mem D C.S e 1 e ctOb j ect(&P e n3);M e mDC.M o veTo(wx 1 ,wyt);Mem DC.L in e To(wx r,wyt);McmDC.L i n eTo(w x r,wyb);McmDC.LincTo(wxl,wyb);oM e mDC.LineTo(wxl,w y t);Mcm DC.S e 1 e c 10 b j ect(pO 1 d P e n 3);P e n 3.Dele t e Obje c t();C Pen Penl,*pOId Penl;定义1个像素宽度的画

10、笔oPenl.CreatePen(PS_SO L I D,1 ,RGB(0,0,255);pOldP e n l=MemDC.Sele c tObject(&Pen 1 );i f(m_ i=1)(e mDC.M o veTo(ROU ND(P o i n t x 0),ROUND(Po i n t y 0):ooMemDC.Li n eTo(ROUND(Po i ntx 1 ),ROUND(Pointyl);。)oMemDC.Se 1 e ctOb j e c t(pOl d Pe n 1 );Pe n 1.Delet e Object。;。CDC*dc=GetDC();dc-BitBlt(

11、0,0,Re ct.Width。,R e ct.Height(),&M e mDC,0,0,SRCCOPY);oMemDC.Sele c(Object(pB itm a p);/C MyCutView p rinti n gB OOL CM y C u tView:O n Pr e p a rePri n ting(CPrintinfo*p I n fo)/d e f a ult prepara t io nrctum D o P re p ar e P rintin g(plnfo);)vo i d CMyCu t View::OnBegin P r inting(C DC*/*pDC*/,

12、CPr i ntl n f o*/*plnfo*/)/T ODO:add extr a i n it i ali z a t i o n before p rinting)v o i d CMyC u t View:O n E n dP r in t i ng(CDC*/*pDC*/,CPrintlnf o*/*pln f o*/)/TODO:ad d cleanu p aft e r p rinting)/I I I /H llllllllllllllllllllll/CMyCutV i ew di a gn o st i c s#ifdef _DEBUGv o id CM y Cut Vi

13、e w::Ass e rt Valid()const(C V iew:A s s er t Vali d();)v oid C MyC u tV i ew:Dump(C D u m p C o n t e x t&d e)const C V ie w::Dump(dc);)C MyCu t D o c*CMyCutView:Ge t Documcnt()/non-dc b u g version is in 1 in e(A S SERT(m_pDocument-IsKindO f(RUNTIM E_CL A SS(C M y CutD oc);r e t urn(CM y CutD o c*

14、)m_pDo c um e n t;)#end if/.DEBUG111/1/IIHU/C My C u tV i ew m e ssa g e handlersv o i d CMyCut View:On MENU I T E Mdrawli n e()(/T O D O:Ad d y o u r command han d 1 e r co d e herei f(F A LS E=m_ at t atch)(Pictur e,C r e ateCompatib 1 e DC(GetDC();oC B i tm a p*B i tmap,*pBitmap;a Bitm a p=n ew C

15、 Bitma p;。/BitmapLoa d Bitmap(IDB_BITMAPl);8P B itma p=Pi c t u re.Selec t O b j e ct(B i t in a p);m_a t tatch=TRUE;)m_draw=T RUE;m_i=0;olnvalida t e(FA L SE);AfxGetM a i nWn d()-SetW i ndowTe x t(M Cohe n-Sutherlan d 直线裁剪算法);显示标题oMe s s ageB ox(请使用鼠标在屏幕上绘制直线,然后点击裁剪按钮进行裁剪“,“提醒”,MB_OKCANCEL);。vo i d

16、 CMyCutView:OnMENUITEMclip()/裁剪菜单函数(。TODO:Add y o ur c ommand ha n dler c o de h e reC u t();Inva 1 ida t e(F ALSE);Ivo i d C M y CutVie w::O n LBut t onDow n(U I NT nFlags,CP o i n t point)(“/TO D 0:Add y our mes s age h a nd 1 er c o de h e re a n d/o r ca 1 I defau 1 t0Af(T R U E=m _ draw)i f(m_

17、i 2)P oin t x m_ i=poin t.x;Poi n tym_i=p oint.y;。,m_i+;)CV i e w:OnLButtonD o w n(nF 1 ags,point);)v o i d CMyCutView::O n Mou s e Move(UINT n F la g s,CPoint point)/TO DO:Add y o u r mess a ge handler code h e r e and/or ca 1 1 defaultif(TRUE=m_d r aw)。i f(m _i2)。Po i nt x m_ i=p oint.x;Poin t ym_

18、 i=poi n t.y;oginva 1 idate(FALSE);力)CView::OnMo u se Mo v e(nFla g s,p o in t);v o i d C MyCutVie w:C u t()B O O L C h a nge;dou b 1 e x,y;oR C 0=E nC od e(P oi n t x 0 ,P oi n t y 0 );RC 1 =E n C o de(P o i nt x l,P oi n t y 1 );whi le(T R U E)。Ch a nge=F A L S E;i f (0 =(RC O|R C 1)。简取之a ret u rn

19、;00 j-els e i f (0!=(R C 0&R C 1 )简弃之ret u r n;)e Is ei f(O=R C 0)/假 如P O点在窗口内,互换PO和P l,保证p O点在窗口外互换点的坐标值。d ouble T Pointx,TPoin t y;TP o i n tx=P o in t x 0;TP o i n t y=Point y 0 ;。oPoi n t x0=P o in t xl;P ointy 0=Poin t y 1;wPointxl=T P o in t x:Pointyl=TPoi n t y;。/互换点的编码值。unsig n ed i n t TRC;

20、。T RC=RC0;RC O =RC1;RC1=TRC;O 0)。按左、右、下、上的顺序裁剪gif(RC0&LEFT)P 0 点位于窗口的左侧0(8 x=w x 1 ;求交点y。y=Po i ntyO+(P o in t yl-Po i n t y0)*(x-Point x 0)/(Poi n tx 1-Pointx 0);。ePo i ntx 0=x;Poi n tyO=y;。Cha n g e=T RUE;ORC0=En Code(Po i ntx0,Po i n t y 0);RCl=EnCode(Poin t x 1 ,Pointy 1 );0 g-if(RC0&RIGHT)/P O

21、点位于窗口的右侧gg x=wxr;求交点 y。y=Po inty0+(Poi n t y 1-Po i nty0)*(x-Poi n tx0)/(Poi ntx l -Pointx 0);ePointx 0=x;Poin t y0=y;。C h ange=T R UE;oRC 0=EnC o d e(Pointx 0,Poi n t y 01);RC1=E nCode(Point x 1,Pointy 1 );j 8。oif(RCO&BOT TOM)P 0点位于窗口的下侧0(8 oy=wy b:求交点 xx=Poi n txO+(P oi n txl-Po i n t x 0)*(y-Poin

22、ty 0)/(P o i n ty 1 -Po i n t y 0);。o i n t x 0=x;Pointy 0=y;。Change=TRUE:RC0=E nC o de(Poi n t x 0,Poi n tyO);R C l=En Code(Po i ntxl,Point y 1 );0 0 J o i f(RCO&T O P)/P O点位于窗口的上侧0 0 0 a y=w yt;/求交点 x。x=P o intx0+(Poi n txl-Pointx0)*(y-Point y 0)/(Poin t y 1 -Po i ntyO);M P ointx O=x;P o i n t yO=

23、y;。Ch a nge=TRUE:ooMRCO=EnCode(Poi n tx 0,Pointy 0);RC 1=EnC o de(P o in t x 1 ,Po i nty 1 );o o o i f (F A L S E=C ha n g e)o o r e t u r n;)d 01)u n s i g ned i n t CM y C u t V i ew::E nC ode(do u b 1 e L i n e P x,d o u b le L i neP y)/端点编码函数/顺序左右下上RC=O ;i f (L i neP x wx r)(R C=R C|R IGHT;i f (L i n eP ywyt)oRC=RC|TOP;retu r n RC;4.运营结果女WF)MKT)5(V)*9 *e(H)a0 一 W.方 西如*F)”(E)BV(V)e s KH)W,s 男9 CohenSuthwtandMKMS|!一(F)K B S(V)Bp 1 一-w-S 2如何修正算法,使其可应用于多边形窗口的裁剪?农口 编写新的多边形编码,并逐边进行裁剪。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com