




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、计算机图形学实验报告班级 计算机科学与技术 姓名 学号 2014 年 6 月 2 日实验一 基本图形生成算法一、实验目的: 1、掌握中点bresenham绘制直线的原理; 2、设计中点bresenham算法; 3、掌握八分法中点bresenham算法绘制圆的原理; 4、设计八分法绘制圆的中点bresenham算法; 5、掌握绘制1/4椭圆弧的上半部分和下半部分的中点bresenham算法原理; 6、掌握下半部分椭圆偏差判别式的初始值计算方法; 7、设计顺时针四分法绘制椭圆的中点bresenham算法。二、实验过程:1、 实验描述 实验1:使用中点bresenham算法绘制斜率为0=k=1的直线
2、。 实验2:使用中点bresenham算法绘制圆心位于屏幕客户区中心的圆。 实验3:使用中点bresenham算法绘制圆心位于屏幕客户区中心的椭圆。2、实验过程 1)用mfc(exe)建立一个单文档工程; 2)编写对话框,生成相应对象,设置相应变量; 3)在类clineview中声明相应函数,并在相关的cpp文件中实现; 4)在ondraw()函数里调用函数实现绘制直线、圆、椭圆; 5)运行程序,输入相应值,绘制出图形。3、 源代码实验1:直线中点bresenham算法1./ cline.cpp : implementation file/ cline dialogcline:cline(cw
3、nd* pparent /*=null*/): cdialog(cline:idd, pparent)/afx_data_init(cline)m_x0 = 0;m_y0 = 0;m_x1 = 0;m_y1 = 0;/afx_data_initvoid cline:dodataexchange(cdataexchange* pdx)cdialog:dodataexchange(pdx);/afx_data_map(cline)ddx_text(pdx, idc_x0, m_x0);ddx_text(pdx, idc_y0, m_y0);ddx_text(pdx, idc_x1, m_x1);d
4、dx_text(pdx, idc_y1, m_y1);/afx_data_mapbegin_message_map(cline, cdialog)/afx_msg_map(cline)/afx_msg_mapend_message_map()2、 / lineview.hclass clineview : public cviewpublic:clinedoc* getdocument();.void mbline(double,double,double,double); /直线中点bresenham函数.3、/ line.cpp /*直线中点bresenham函数*/void clinev
5、iew:mbline(double x0, double y0, double x1, double y1) cclientdc dc(this); colorref rgb=rgb(0,0,255); /定义直线颜色为蓝色double x,y,d,k;x=x0; y=y0; k=(y1-y0)/(x1-x0); d=0.5-k;for(x=x0;x=x1;x+)dc.setpixel(int)x,(int)y,rgb);if(d0)y+;d+=1-k;elsed-=k;4、/lineview.cppvoid clineview:ondraw(cdc* pdc)clinedoc* pdoc =
6、 getdocument();assert_valid(pdoc);/ todo: add draw code for native data herecline a;a.domodal();/初始化clineview:mbline(a.m_x0,a.m_y0,a.m_x1,a.m_y1);实验2:圆中点bresenham算法 1、/cricle.cpp/ ccricle dialogccricle:ccricle(cwnd* pparent /*=null*/): cdialog(ccricle:idd, pparent)/afx_data_init(ccricle)m_r = 0;/afx
7、_data_initvoid ccricle:dodataexchange(cdataexchange* pdx)cdialog:dodataexchange(pdx);/afx_data_map(ccricle)ddx_text(pdx, r_edit, m_r);/afx_data_map 2、/ccircleview.hclass cccircleview : public cview.public:cccircledoc* getdocument();void circlepoint(double,double); /八分法画圆函数void mbcircle(double); /圆中点
8、bresenham函数. 3、/ccircleview.cppvoid cccircleview:ondraw(cdc* pdc)cccircledoc* pdoc = getdocument();assert_valid(pdoc);/ todo: add draw code for native data hereccricle r;r.domodal();cccircleview:mbcircle(r.m_r);/画圆4、/ccircleview.cpp/*八分法画圆*/void cccircleview:circlepoint(double x,double y)cclientdc d
9、c(this);colorref rgb=rgb(0,0,255);dc.setpixel(int)(300+x),(int)(300+y),rgb);dc.setpixel(int)(300-x),(int)(300+y),rgb);dc.setpixel(int)(300+x),(int)(300-y),rgb);dc.setpixel(int)(300-x),(int)(300-y),rgb);dc.setpixel(int)(300+y),(int)(300+x),rgb);dc.setpixel(int)(300-y),(int)(300+x),rgb);dc.setpixel(in
10、t)(300+y),(int)(300-x),rgb);dc.setpixel(int)(300-y),(int)(300-x),rgb);/*圆中点bresenham函数*/void cccircleview:mbcircle(double r)double x,y,d;colorref rgb=rgb(0,0,255);d=1.25-r;x=0; y=r;for(x=0;xy;x+)circlepoint(x,y); /调用八分法画圆子函数if(d0)d+=2*x+3;elsed+=2*(x-y)+5;y-;实验3:椭圆中点bresenham算法1、/ellipse1.cpp/ celli
11、pse dialogcellipse:cellipse(cwnd* pparent /*=null*/): cdialog(cellipse:idd, pparent)/afx_data_init(cellipse)m_a = 0;m_b = 0;/afx_data_initvoid cellipse:dodataexchange(cdataexchange* pdx)cdialog:dodataexchange(pdx);/afx_data_map(cellipse)ddx_text(pdx, idc_edit1, m_a);ddx_text(pdx, idc_edit2, m_b);/af
12、x_data_map2、/ellipseview.hclass cellipseview : public cview.public:cellipsedoc* getdocument();void ellipsepoint(double,double); /四分法画椭圆void mbellipse(double a, double b); /椭圆中点bresenham函数.3、/ellipse.cpp/*四分法画椭圆*/void cellipseview:ellipsepoint(double x,double y)cclientdc dc(this);colorref rgb=rgb(0,0
13、,255); dc.setpixel(int)(300+x),(int)(300+y),rgb);dc.setpixel(int)(300-x),(int)(300+y),rgb);dc.setpixel(int)(300+x),(int)(300-y),rgb);dc.setpixel(int)(300-x),(int)(300-y),rgb);/*椭圆中点bresenham函数*/void cellipseview:mbellipse(double a, double b)double x,y,d1,d2;x=0;y=b;d1=b*b+a*a*(-b+0.25);ellipsepoint(
14、x,y);while(b*b*(x+1)a*a*(y-0.5)/椭圆ac弧段if(d10)if(d20)d2+=b*b*(2*x+2)+a*a*(-2*y+3);x+;elsed2+=a*a*(-2*y+3);y-;ellipsepoint(x,y);4、/ellipseview.cppvoid cellipseview:ondraw(cdc* pdc)cellipsedoc* pdoc = getdocument();assert_valid(pdoc);/ todo: add draw code for native data herecellipse el;el.domodal();/初
15、始化cellipseview:mbellipse(el.m_a, el.m_b);/画椭圆4、 实结果验实验1:直线中点bresenham算法实验2:圆中点bresenham算法实验3:椭圆中点bresenham算法实验二 有效边表填充算法一、实验目的:1、设计有效边表结点和边表结点数据结构;2、设计有效边表填充算法;3、编程实现有效边表填充算法。2、 实验过程:1、实验描述 下图1 所示多边形覆盖了 12 条扫描线,共有 7 个顶点和 7 条边。7 个顶点分别为:p0(7,8) ,p1(3,12) ,p2(1,7) ,p3(3,1), p4(6,5), p5(8,1), p6(12,9)。在
16、 1024768 的显示分辩率下,将多边形顶点放大为 p0(500,400) ,p1(350,600) ,p2(250,350),p3(350,50), p4(500,250), p5(600,50), p6(800,450)。请使用有效边表算法填充该多边形。2、 实验过程 (1)建立aet类和bucket类; (2)初始化桶,并在建立桶结点时为其表示的扫描线初始化为带头结点的链表; (3)对每个桶结点进行循环,将桶内每个结点的边表合并为有效边表,并进行有效边表循环; (4)按照扫描线从小到大的移动顺序,计算当前扫描线与多边形各边的交点,然后把这些交点按x值递增的顺序进行排序,配对,以确定填充
17、区间; (5)用指定颜色点亮填充区间内的所有像素,即完成填充工作。3、 源代码1、/aet.hclass aet public:aet();virtual aet();double x;int ymax;double k;/代替1/kaet *next;/aet.cppaet:aet()aet:aet()2、 /bucket.h#include aet.hclass bucket public:bucket();virtual bucket();int scanline;aet *p;/桶上的边表指针bucket *next;/ bucket.cppbucket:bucket()bucket:
18、bucket()3、/test2.cppvoid ctest2view:et()/构造边表函数for(int i=0;ipointi.y)/终点比起点高while(currentb-scanline!=pointi.y)/在桶内寻找该边的ymincurrentb=currentb-next;/移到下一个桶结点ei.x=pointi.x;/计算aet表的值ei.ymax=pointj.y;ei.k=double(pointj.x-pointi.x)/(pointj.y-pointi.y);/代表1/kei.next=null;currente=currentb-p;/获得桶上连接边表的地址if(
19、currentb-p=null)currente=&ei;/边表的起始地址currentb-p=currente;/第一个边表直接连接到对应的桶中elsewhile(currente-next!=null)/如果当前边已连有边结点currente=currente-next;/移动指针到当前边的最后一个结点currente-next=&ei;/把当前边接上去if(pointj.yscanline!=pointj.y)/在桶内寻找该边的ymincurrentb=currentb-next;/移到下一个桶结点ei.x=pointj.x;/计算aet表的值ei.ymax=pointi.y;ei.k=
20、double(pointi.x-pointj.x)/(pointi.y-pointj.y);/代表1/kei.next=null;currente=currentb-p;/获得桶上连接边表的地址if(currente=null)currente=&ei;/边表的起始地址currentb-p=currente;/第一个边表直接连接到对应的桶中elsewhile(currente-next!=null)/如果当前边已连有边结点currente=currente-next;/移动指针到当前边的最后一个结点currente-next=&ei;/把当前边接上去currentb=null;currente
21、=null;void ctest2view:addedge(aet *newedge)/插入临时边表函数t1=heade;if(t1=null)/边表为空,将边表置为tempedget1=newedge;heade=t1;elsewhile(t1-next!=null)/边表不为空,将tempedge连在该边之后t1=t1-next;t1-next=newedge;void ctest2view:edegeorder()/对边表进行排序函数t1=heade;if(t1=null)return;if(t1-next=null)/如果该边表没有再连边表return;/桶结点只有一条边,不需要排序e
22、lseif(t1-next-xx)/边表按x值排序t2=t1-next;t1-next=t2-next;t2-next=t1;heade=t2; t2=heade; t1=heade-next; while(t1-next!=null)/继续两两比较相连的边表的x值,进行排序 if(t1-next-xx) t2-next=t1-next; t1-next=t1-next-next; t2-next-next=t1;t2=t2-next; else t2=t1; t1=t1-next; void ctest2view:polygonfill()/多边形填充函数cclientdc dc(this)
23、;colorref rgb=rgb(0,0,255); 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-k=currente-k; tempedge-next=null; addedge(te
24、mpedge);/将该边插入临时aet表edegeorder();/边表按照x递增的顺序存放 t1=heade;/根据ymax抛弃扫描完的边结点if(t1=null)return;while(currentb-scanline=t1-ymax)/放弃该结点,aet表指针后移,下闭上开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;
25、t1=t2-next;elset2=t1;t1=t2-next;bool in=false;/设置一个bool变量in,初始值为假double xb,xe;/扫描线的起点和终点for(t1=heade;t1!=null;t1=t1-next)/填充扫描线和多边形相交的区间if(in=false)xb=t1-x;in=true;/,每访问一个结点,把in值取反一次else/如果in值为真,则填充从当前结点的x值开始到下一个结点的x值结束的区间xe=t1-x-1;/左闭右开cclientdc dc(this);for(double x=xb;xscanline,rgb);/填充 sleep(1);
26、/延时1ms,提高填充过程的可视性 in=false;for(t1=heade;t1!=null;t1=t1-next)/边连贯性 t1-x=t1-x+t1-k;/x=x+1/kdelete headb;delete currentb;delete currente;delete heade;/*初始化桶*void ctest2view:creatbucket() int scanmax,scanmin;scanmax = scanmin = point0.y;for(int i=1;inumber;i+)if(pointi.yscanmax)scanmax = pointi.y; /扫描线的最大值for( i=scanmin;iscanline = scanmin;currentb-p = null; /没有连接边链表currentb
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030博物馆行业市场现状供需分析及投资评估规划分析研究报告
- 2025-2030公募基金行业兼并重组机会研究及决策咨询报告
- 船舶翻新施工方案
- 路桥机器施工方案
- 汉川泳池施工方案
- 涵管接口施工方案
- 树拆除施工方案
- 铁矿爆破施工方案
- 业主自住租赁协议
- 坡道护坡施工方案
- 中职学生教育管理工作课件
- 水肥一体化技术 稿课件
- 作业现场安全监督检查卡(配电)
- 施工班组考核表
- 车间粉尘清扫记录表
- 分布式光伏发电项目EPC总承包合同
- 六年级下册数学课件-2.3 圆柱(复习) ︳西师大版 (10张PPT)
- 国际五一劳动节颁奖荣誉晚会动态PPT模板
- 全息经络刮痧疗法(内部培训)课件
- CPK计算表格EXCEL模板
- 消防安全知识课件PPT(72张)
评论
0/150
提交评论