图形学1.doc_第1页
图形学1.doc_第2页
图形学1.doc_第3页
图形学1.doc_第4页
图形学1.doc_第5页
已阅读5页,还剩45页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

直线中点/ TestView.cpp : implementation of the CTestView class/#include stdafx.h#include Test.h#include TestDoc.h#include TestView.h#define ROUND(a) int(a+0.5) /四舍五入#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CTestViewIMPLEMENT_DYNCREATE(CTestView, CView)BEGIN_MESSAGE_MAP(CTestView, CView)/AFX_MSG_MAP(CTestView)ON_COMMAND(ID_MENUMbline, OnMENUMbline)/AFX_MSG_MAP/ Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView:OnFilePrintPreview)END_MESSAGE_MAP()/ CTestView construction/destructionCTestView:CTestView()/ TODO: add construction code hereCTestView:CTestView()BOOL CTestView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: Modify the Window class or styles here by modifying/ the CREATESTRUCT csreturn CView:PreCreateWindow(cs);/ CTestView drawingvoid CTestView:OnDraw(CDC* pDC)CTestDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data here/ CTestView printingBOOL CTestView:OnPreparePrinting(CPrintInfo* pInfo)/ default preparationreturn DoPreparePrinting(pInfo);void CTestView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add extra initialization before printingvoid CTestView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add cleanup after printing/ CTestView diagnostics#ifdef _DEBUGvoid CTestView:AssertValid() constCView:AssertValid();void CTestView:Dump(CDumpContext& dc) constCView:Dump(dc);CTestDoc* CTestView:GetDocument() / non-debug version is inlineASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CTestDoc);return (CTestDoc*)m_pDocument;#endif /_DEBUG/ CTestView message handlersvoid CTestView:Mbline()/Bresenham函数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(ROUND(x),ROUND(y),rgb); if(dSetWindowText(案例:直线中点Bresenham算法);RedrawWindow();Mbline();圆中点/ TestView.cpp : implementation of the CTestView class/#include stdafx.h#include Test.h#include TestDoc.h#include TestView.h#define ROUND(a) int(a+0.5) /四舍五入#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CTestViewIMPLEMENT_DYNCREATE(CTestView, CView)BEGIN_MESSAGE_MAP(CTestView, CView)/AFX_MSG_MAP(CTestView)ON_COMMAND(ID_MENU_Mbcircle, OnMENUMbcircle)/AFX_MSG_MAP/ Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView:OnFilePrintPreview)END_MESSAGE_MAP()/ CTestView construction/destructionCTestView:CTestView()/ TODO: add construction code hereCTestView:CTestView()BOOL CTestView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: Modify the Window class or styles here by modifying/ the CREATESTRUCT csreturn CView:PreCreateWindow(cs);/ CTestView drawingvoid CTestView:OnDraw(CDC* pDC)CTestDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data here/ CTestView printingBOOL CTestView:OnPreparePrinting(CPrintInfo* pInfo)/ default preparationreturn DoPreparePrinting(pInfo);void CTestView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add extra initialization before printingvoid CTestView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add cleanup after printing/ CTestView diagnostics#ifdef _DEBUGvoid CTestView:AssertValid() constCView:AssertValid();void CTestView:Dump(CDumpContext& dc) constCView:Dump(dc);CTestDoc* CTestView:GetDocument() / non-debug version is inlineASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CTestDoc);return (CTestDoc*)m_pDocument;#endif /_DEBUG/ CTestView message handlersvoid CTestView:GetMaxX()/得到客户区的最大横坐标CRect Rect;GetClientRect(&Rect);MaxX=Rect.right;void CTestView:GetMaxY()/得到客户区最大纵坐标CRect Rect;GetClientRect(&Rect);MaxY=Rect.bottom;void CTestView:Mbcircle()/Bresenham算法double x,y,d; d=1.25-R;x=0;y=R;for(x=0;xy;x+)CirclePoint(x,y);/调用八分法画圆子函数 if (dSetWindowText(案例:圆中点Bresenham算法);RedrawWindow();GetMaxX();GetMaxY();Mbcircle();椭圆中点/ TestView.cpp : implementation of the CTestView class/#include stdafx.h#include Test.h#include TestDoc.h#include TestView.h#define ROUND(a) int(a+0.5) /四舍五入#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CTestViewIMPLEMENT_DYNCREATE(CTestView, CView)BEGIN_MESSAGE_MAP(CTestView, CView)/AFX_MSG_MAP(CTestView)ON_COMMAND(ID_MENUMbellipse, OnMENUMbellipse)/AFX_MSG_MAP/ Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView:OnFilePrintPreview)END_MESSAGE_MAP()/ CTestView construction/destructionCTestView:CTestView()/ TODO: add construction code hereCTestView:CTestView()BOOL CTestView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: Modify the Window class or styles here by modifying/ the CREATESTRUCT csreturn CView:PreCreateWindow(cs);/ CTestView drawingvoid CTestView:OnDraw(CDC* pDC)CTestDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data here/ CTestView printingBOOL CTestView:OnPreparePrinting(CPrintInfo* pInfo)/ default preparationreturn DoPreparePrinting(pInfo);void CTestView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add extra initialization before printingvoid CTestView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add cleanup after printing/ CTestView diagnostics#ifdef _DEBUGvoid CTestView:AssertValid() constCView:AssertValid();void CTestView:Dump(CDumpContext& dc) constCView:Dump(dc);CTestDoc* CTestView:GetDocument() / non-debug version is inlineASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CTestDoc);return (CTestDoc*)m_pDocument;#endif /_DEBUG/ CTestView message handlersvoid CTestView:GetMaxX()/得到客户区的最大横坐标CRect Rect;GetClientRect(&Rect);MaxX=Rect.right;void CTestView:GetMaxY()/得到客户区最大纵坐标CRect Rect;GetClientRect(&Rect);MaxY=Rect.bottom;void CTestView:Mbellipse()/椭圆的Bresenham算法double x,y,d1,d2;x=0;y=b;d1=b*b+a*a*(-b+0.25);EllipsePoint(x,y);while(b*b*(x+1)a*a*(y-0.5)/椭圆AC弧段if (d10)if (d2SetWindowText(案例:椭圆中点Bresenham算法);RedrawWindow();GetMaxX();GetMaxY();Mbellipse();直线反走样void CTestView:Mbline()/走样直线CClientDC dc(this);COLORREF rgb=RGB(0,0,0);/定义直线颜色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(ROUND(x),ROUND(y),rgb);if(d0)y+;d+=1-k;elsed-=k; dc.TextOut(10,10,走样直线);/输出文字void CTestView:AntiDisline()/距离加权反走样直线CClientDC dc(this);double x,y,d,k;k=(y1-y0)/(x1-x0);d=0;x=x0+10;y=y0+10;COLORREF rgb1=RGB(0,0,0),rgb2=RGB(0,0,0);for(x=x0;x1.0)y+;d-; dc.TextOut(10,100,反走样直线);/输出文字多边形有效边/ TestView.cpp : implementation of the CTestView class/#include stdafx.h#include Test.h#include TestDoc.h#include TestView.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif#define ROUND(a) int(a+0.5)/四舍五入/ CTestViewIMPLEMENT_DYNCREATE(CTestView, CView)BEGIN_MESSAGE_MAP(CTestView, CView)/AFX_MSG_MAP(CTestView)ON_COMMAND(ID_MENUAET, OnMenuAET)/AFX_MSG_MAP/ Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView:OnFilePrintPreview)END_MESSAGE_MAP()/ CTestView construction/destructionCTestView:CTestView()/ TODO: add construction code here/设置多边形的个顶点Point0=CPoint(550,400);/P0Point1=CPoint(350,600);/P1Point2=CPoint(250,350);/P2Point3=CPoint(350,50);/P3Point4=CPoint(500,250);/P4Point5=CPoint(600,50);/P5Point6=CPoint(800,450);/P6CTestView:CTestView()BOOL CTestView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: Modify the Window class or styles here by modifying/ the CREATESTRUCT csreturn CView:PreCreateWindow(cs);/ CTestView drawingvoid CTestView:OnDraw(CDC* pDC)CTestDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data herepDC-Polygon(Point,7);/绘制多边形/输出多边形的顶点编号pDC-TextOut(550,410,P0);pDC-TextOut(350,600,P1); pDC-TextOut(230,340,P2);pDC-TextOut(350,30,P3);pDC-TextOut(490,220,P4);pDC-TextOut(600,30,P5);pDC-TextOut(805,450,P6); / CTestView printingBOOL CTestView:OnPreparePrinting(CPrintInfo* pInfo)/ default preparationreturn DoPreparePrinting(pInfo);void CTestView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add extra initialization before printingvoid CTestView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add cleanup after printing/ CTestView diagnostics#ifdef _DEBUGvoid CTestView:AssertValid() constCView:AssertValid();void CTestView:Dump(CDumpContext& dc) constCView:Dump(dc);CTestDoc* CTestView:GetDocument() / non-debug version is inlineASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CTestDoc);return (CTestDoc*)m_pDocument;#endif /_DEBUG/ CTestView message handlersvoid CTestView:OnMenuAET() /菜单函数/ TODO: Add your command handler code hereAfxGetMainWnd()-SetWindowText(案例:多边形有效边表填充算法);/显示标题CColorDialog ccd(GetColor);if(ccd.DoModal()=IDOK)/调用调色板选取前景色GetColor=ccd.GetColor();RedrawWindow();/刷新屏幕CreatBucket();/初始化桶Et();/用于建立边表PolygonFill();/多边形填充void CTestView:CreatBucket()/建立桶结点int ScanMin,ScanMax;/确定扫描线的最小值和最大值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-next=NULL;else/建立桶的其它结点CurrentB-next=new Bucket;/新建一个桶结点CurrentB=CurrentB-next;/使CurrentB指向新建的桶结点CurrentB-ScanLine=i;CurrentB-p=NULL;/没有连接边链表CurrentB-next=NULL;void CTestView: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);/代表/kEi.next=NULL;CurrentE=CurrentB-p;/获得桶上链接边表的地址if(CurrentB-p=NULL)/当前桶结点上没有链接边结点CurrentE=&Ei;/赋边的起始地址CurrentB-p=CurrentE;/第一个边结点直接连接到对应的桶中elsewhile(CurrentE-next!=NULL)/如果当前边已连有边结点CurrentE=CurrentE-next;/移动指针到当前边的最后一个边结点CurrentE-next=&Ei;/把当前边接上去if(Pointj.yScanLine!=Pointj.y)CurrentB=CurrentB-next;Ei.x=Pointj.x;Ei.yMax=Pointi.y;Ei.k=double(Pointi.x-Pointj.x)/(Pointi.y-Pointj.y);Ei.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=NULL;void CTestView:AddEdge(AET *NewEdge)/插入临时边表T1=HeadE;if(T1=NULL)/边表为空,将边表置为TempEdgeT1=NewEdge;HeadE=T1;elsewhile(T1-next!=NULL)/边表不为空,将TempEdge连在该边之后T1=T1-next;T1-next=NewEdge;void CTestView:EdgeOrder()/对边表进行排序T1=HeadE;if(T1=NULL)return;if(T1-next=NULL)/如果该边表没有再连边表return;/桶结点只有一条边,不需要排序elseif(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;elseT2=T1;T1=T1-next;void CTestView:PolygonFill()/多边形填充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(TempEdge);/将该边插入临时Aet表EdgeOrder();/使得边表按照x递增的顺序存放T1=HeadE;/根据ymax抛弃扫描完的边结点if

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论