




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上洛陽理工學(xué)院實驗報告系別計算機與信息工程系班級B學(xué)號B姓名課程名稱計算機圖形學(xué)實驗日期2013-11-7實驗名稱多邊形填充算法編程成績實驗?zāi)康模?熟悉多邊形填充算法,掌握MFC圖形編程的基本方法和調(diào)試技巧。實驗條件: 計算機;VS2008;OpenGL實驗內(nèi)容: 1使用MFC技術(shù)實現(xiàn)多邊形有效邊表填充算法,參考界面效果如下:/ ChildView.cpp : CChildView 類的實現(xiàn)#include stdafx.h#include demo.h#include ChildView.h#include #define Round(d) int(floor(d+0
2、.5)/四舍五入宏定義#ifdef _DEBUG#define new DEBUG_NEW#endif/ CChildViewCChildView:CChildView()CChildView:CChildView()BEGIN_MESSAGE_MAP(CChildView, CWnd)ON_WM_PAINT()ON_WM_CREATE()ON_COMMAND(ID_DRAW_PIC, &CChildView:OnDrawPic)END_MESSAGE_MAP()/ CChildView 消息處理程序BOOL CChildView:PreCreateWindow(CREATESTRUCT& c
3、s) if (!CWnd:PreCreateWindow(cs)return FALSE;cs.dwExStyle |= WS_EX_CLIENTEDGE;cs.style &= WS_BORDER;cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, :LoadCursor(NULL, IDC_ARROW), reinterpret_cast(COLOR_WINDOW+1), NULL);return TRUE;void CChildView:OnPaint() CPaintDC dc(this); / 用于
4、繪制的設(shè)備上下文/ TODO: 在此處添加消息處理程序代碼DrawGraph();/ 不要為繪制消息而調(diào)用CWnd:OnPaint()void CChildView:ReadPoint()/點表P0.x = 50; P0.y = 100;P1.x = -150;P1.y = 300;P2.x = -250;P2.y = 50;P3.x = -150;P3.y = -250;P4.x = 0; P4.y = -50;P5.x = 100; P5.y = -250;P6.x = 300; P6.y = 150;void CChildView:DrawPolygon(CDC *pDC)/繪制多邊形邊
5、界CLine *line = new CLine;CP2 t;for(int i = 0; i MoveTo(pDC, Pi);t = Pi;elseline-LineTo(pDC, Pi);line-LineTo(pDC, t);/閉合多邊形delete line;void CChildView:DrawGraph()/繪制圖形CRect rect; /定義客戶區(qū)GetClientRect(&rect); /獲得客戶區(qū)的大小CDC *pDC = GetDC();/定義設(shè)備上下文指針 pDC-SetMapMode(MM_ANISOTROPIC);/自定義坐標(biāo)系pDC-SetWindowExt(
6、rect.Width(), rect.Height();/設(shè)置窗口比例pDC-SetViewportExt(rect.Width(), -rect.Height();/設(shè)置視區(qū)比例,且x軸水平向右,y軸垂直向上pDC-SetViewportOrg(rect.Width() / 2, rect.Height() / 2);/設(shè)置客戶區(qū)中心為坐標(biāo)系原點rect.OffsetRect(-rect.Width() / 2, -rect.Height() / 2);/矩形與客戶區(qū)重合if(!bFill)DrawPolygon(pDC);/繪制多邊形elseFillPolygon(pDC);/填充多邊形R
7、eleaseDC(pDC);/釋放DCvoid CChildView:FillPolygon(CDC *pDC)/填充多邊形for(int i = 0; i SetPoint(P1, 7);/初始化Fill對象fill-CreateBucket();/建立桶表fill-CreateEdge();/建立邊表 fill-Gouraud(pDC);/填充多邊形delete fill;/撤銷內(nèi)存int CChildView:OnCreate(LPCREATESTRUCT lpCreateStruct)if (CWnd:OnCreate(lpCreateStruct) = -1)return -1;/
8、TODO: 在此添加您專用的創(chuàng)建代碼bFill = FALSE;ReadPoint();return 0;void CChildView:OnDrawPic()/ TODO: 在此添加命令處理程序代碼COLORREF GetClr = RGB(0, 0, 0);/調(diào)色板顏色CColorDialog ccd(GetClr, CC_SOLIDCOLOR);if(IDOK = ccd.DoModal()/調(diào)用顏色對話框選取填充色GetClr = ccd.GetColor();elsereturn;bRed = GetRValue(GetClr);/獲取紅色分量bGreen = GetGValue(G
9、etClr);/獲取綠色分量bBlue = GetBValue(GetClr);/獲取藍(lán)色分量bFill = TRUE;Invalidate(); 2使用MFC技術(shù)實現(xiàn)多邊形邊緣填充算法,參考界面效果如下:/ demoView.cpp : CdemoView 類的實現(xiàn)#include stdafx.h#include demo.h#include demoDoc.h#include demoView.h#include #define Round(d) int(floor(d+0.5)/四舍五入宏定義#ifdef _DEBUG#define new DEBUG_NEW#endif/ Cdemo
10、ViewIMPLEMENT_DYNCREATE(CdemoView, CView)BEGIN_MESSAGE_MAP(CdemoView, CView)/ 標(biāo)準(zhǔn)打印命令ON_COMMAND(ID_FILE_PRINT, &CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CdemoView:OnFilePrintPreview)ON_COMMAND(ID_DRAW_PIC, &CdemoView:OnDrawPic)END_MESSAG
11、E_MAP()/ CdemoView 構(gòu)造/析構(gòu)CdemoView:CdemoView()/ TODO: 在此處添加構(gòu)造代碼CdemoView:CdemoView()BOOL CdemoView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: 在此處通過修改/ CREATESTRUCT cs 來修改窗口類或樣式return CView:PreCreateWindow(cs);/ CdemoView 繪制void CdemoView:OnDraw(CDC* /*pDC*/)CdemoDoc* pDoc = GetDocument();ASSERT_VALID(p
12、Doc);if (!pDoc)return;/ TODO: 在此處為本機數(shù)據(jù)添加繪制代碼DrawGraph();/ CdemoView 打印void CdemoView:OnFilePrintPreview()AFXPrintPreview(this);BOOL CdemoView:OnPreparePrinting(CPrintInfo* pInfo)/ 默認(rèn)準(zhǔn)備return DoPreparePrinting(pInfo);void CdemoView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 添加額外的打印前進(jìn)
13、行的初始化過程void CdemoView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 添加打印后進(jìn)行的清理過程void CdemoView:OnRButtonUp(UINT nFlags, CPoint point)ClientToScreen(&point);OnContextMenu(this, point);void CdemoView:OnContextMenu(CWnd* pWnd, CPoint point)theApp.GetContextMenuManager()-ShowPopupMenu(IDR_PO
14、PUP_EDIT, point.x, point.y, this, TRUE);void CdemoView:ReadPoint()/點表P0.x = 50; P0.y = 100;P1.x = -150;P1.y = 300;P2.x = -250;P2.y = 50;P3.x = -150;P3.y = -250;P4.x = 0; P4.y = -50;P5.x = 100; P5.y = -250;P6.x = 300; P6.y = 150;void CdemoView:DrawPolygon(CDC *pDC) for(int i = 0; i MaxX)MaxX = Pi.x;i
15、f(Pi.x MaxY)MaxY = Pi.y;if(Pi.y MinY)MinY = Pi.y;CLine *line = new CLine;CP2 t;for(int i = 0; i MoveTo(pDC, Pi);t = Pi;elseline-LineTo(pDC, Pi);line-LineTo(pDC, t);/閉合多邊形line-MoveTo(pDC, CP2(MinX, MinY);/繪制包圍盒l(wèi)ine-LineTo(pDC, CP2(MinX, MaxY);line-LineTo(pDC, CP2(MaxX, MaxY);line-LineTo(pDC, CP2(MaxX
16、, MinY);line-LineTo(pDC, CP2(MinX, MinY);delete line;void CdemoView:FillPolygon(CDC *pDC) COLORREF BClr = RGB(255, 255, 255);/背景色COLORREF FClr = GetClr;/填充色int ymin, ymax;/邊的最小y值與最大y值double x, y, k;/x,y當(dāng)前點,k斜率的倒數(shù)for(int i = 0; i 7; i+)/循環(huán)多邊形所有邊int j = (i + 1) % 7;k = (Pi.x - Pj.x) / (Pi.y - Pj.y);/計
17、算/kif(Pi.y Pj.y)/得到每條邊y的最大值與最小值ymin = Round(Pi.y);ymax = Round(Pj.y);x = Pi.x;/得到x|yminelseymin = Round(Pj.y);ymax = Round(Pi.y);x = Pj.x;for(y = ymin; y ymax; y+)/沿每一條邊循環(huán)掃描線for(int m = Round(x); m GetPixel(m, Round(y)/如果是填充色pDC-SetPixelV(m, Round(y), BClr);/置為背景色elsepDC-SetPixelV(m, Round(y), FClr)
18、;/置為填充色x += k;/計算下一條掃描線的x起點坐標(biāo)void CdemoView:DrawGraph()/繪制圖形CRect rect;/定義客戶區(qū)GetClientRect(&rect);/獲得客戶區(qū)的大小CDC *pDC = GetDC();/定義設(shè)備上下文指針 pDC-SetMapMode(MM_ANISOTROPIC);/自定義坐標(biāo)系pDC-SetWindowExt(rect.Width(), rect.Height();/設(shè)置窗口比例pDC-SetViewportExt(rect.Width(), -rect.Height();/設(shè)置視區(qū)比例,且x軸水平向右,y軸垂直向上pDC
19、-SetViewportOrg(rect.Width() / 2, rect.Height() / 2);/設(shè)置客戶區(qū)中心為坐標(biāo)系原點rect.OffsetRect(-rect.Width() / 2, -rect.Height() / 2);/矩形與客戶區(qū)重合if(!bFill)DrawPolygon(pDC);/繪制多邊形elseFillPolygon(pDC);/填充多邊形ReleaseDC(pDC);/釋放DC/ CdemoView 診斷#ifdef _DEBUGvoid CdemoView:AssertValid() constCView:AssertValid();void Cde
20、moView:Dump(CDumpContext& dc) constCView:Dump(dc);CdemoDoc* CdemoView:GetDocument() const / 非調(diào)試版本是內(nèi)聯(lián)的ASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CdemoDoc);return (CdemoDoc*)m_pDocument;#endif /_DEBUG/ CdemoView 消息處理程序void CdemoView:OnInitialUpdate()CView:OnInitialUpdate();/ TODO: 在此添加專用代碼和/或調(diào)用基類bFill
21、= FALSE;ReadPoint();GetClr = RGB(0,0,0);MinX = MaxX = P0.x;MinY = MaxY = P0.y;void CdemoView:OnDrawPic()/ TODO: 在此添加命令處理程序代碼CColorDialog ccd(GetClr, CC_SOLIDCOLOR);if(IDOK = ccd.DoModal()/調(diào)用顏色對話框選取填充色GetClr = ccd.GetColor();elsereturn;bFill = TRUE;Invalidate(FALSE); 3.使用MFC技術(shù)實現(xiàn)種子填充算法,參考界面效果如下:/ demo
22、View.cpp : CdemoView 類的實現(xiàn)#include stdafx.h#include demo.h#include demoDoc.h#include demoView.h#include #define Round(d) int(floor(d+0.5)/四舍五入宏定義#ifdef _DEBUG#define new DEBUG_NEW#endif/ CdemoViewIMPLEMENT_DYNCREATE(CdemoView, CView)BEGIN_MESSAGE_MAP(CdemoView, CView)/ 標(biāo)準(zhǔn)打印命令ON_COMMAND(ID_FILE_PRINT,
23、 &CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CdemoView:OnFilePrintPreview)ON_WM_LBUTTONDOWN()ON_COMMAND(ID_DRAW_PIC, &CdemoView:OnDrawPic)END_MESSAGE_MAP()/ CdemoView 構(gòu)造/析構(gòu)CdemoView:CdemoView()/ TODO: 在此處添加構(gòu)造代碼bFill = FALSE;SeedClr = RGB(
24、255, 0, 0);CdemoView:CdemoView()BOOL CdemoView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: 在此處通過修改/ CREATESTRUCT cs 來修改窗口類或樣式return CView:PreCreateWindow(cs);/ CdemoView 繪制void CdemoView:OnDraw(CDC* /*pDC*/)CdemoDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;/ TODO: 在此處為本機數(shù)據(jù)添加繪制代碼DrawGra
25、ph();/ CdemoView 打印void CdemoView:OnFilePrintPreview()AFXPrintPreview(this);BOOL CdemoView:OnPreparePrinting(CPrintInfo* pInfo)/ 默認(rèn)準(zhǔn)備return DoPreparePrinting(pInfo);void CdemoView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 添加額外的打印前進(jìn)行的初始化過程void CdemoView:OnEndPrinting(CDC* /*pDC*/, C
26、PrintInfo* /*pInfo*/)/ TODO: 添加打印后進(jìn)行的清理過程void CdemoView:OnRButtonUp(UINT nFlags, CPoint point)ClientToScreen(&point);OnContextMenu(this, point);void CdemoView:OnContextMenu(CWnd* pWnd, CPoint point)theApp.GetContextMenuManager()-ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);/ CdemoView
27、 診斷#ifdef _DEBUGvoid CdemoView:AssertValid() constCView:AssertValid();void CdemoView:Dump(CDumpContext& dc) constCView:Dump(dc);CdemoDoc* CdemoView:GetDocument() const / 非調(diào)試版本是內(nèi)聯(lián)的ASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CdemoDoc);return (CdemoDoc*)m_pDocument;#endif /_DEBUGvoid CdemoView:DrawGraph(
28、)/繪制圖形CDC *pDC = GetDC();/定義設(shè)備上下文指針GetClientRect(&rect);/獲得客戶區(qū)的大小pDC-SetMapMode(MM_ANISOTROPIC);/自定義坐標(biāo)系pDC-SetWindowExt(rect.Width(), rect.Height();/設(shè)置窗口比例pDC-SetViewportExt(rect.Width(), -rect.Height();/設(shè)置視區(qū)比例,且x軸水平向右,y軸垂直向上pDC-SetViewportOrg(rect.Width() / 2, rect.Height() / 2);/設(shè)置客戶區(qū)中心為坐標(biāo)系原點rect.
29、OffsetRect(-rect.Width() / 2, -rect.Height() / 2);/矩形與客戶區(qū)重合CDC MemDC;/內(nèi)存DCCBitmap NewBitmap, *pOldBitmap;/內(nèi)存中承載圖像的位圖MemDC.CreateCompatibleDC(pDC);/建立與屏幕pDC兼容的MemDC NewBitmap.LoadBitmap(IDB_BITMAP1);/導(dǎo)入空心漢字位圖pOldBitmap = MemDC.SelectObject(&NewBitmap);/將兼容位圖選入MemDC MemDC.SetMapMode(MM_ANISOTROPIC);/M
30、emDC自定義坐標(biāo)系MemDC.SetWindowExt(rect.Width(), rect.Height();MemDC.SetViewportExt(rect.Width(), -rect.Height();MemDC.SetViewportOrg(rect.Width() / 2, rect.Height() / 2);pDC-BitBlt(-rect.Width() / 2, -rect.Height() / 2, rect.Width(), rect.Height(), &MemDC, -rect.Width() / 2, -rect.Height() / 2, SRCCOPY);
31、/將內(nèi)存位圖拷貝到屏幕if(bFill)CharFill(pDC);/填充空心漢字MemDC.SelectObject(pOldBitmap);/恢復(fù)位圖NewBitmap.DeleteObject();/刪除位圖MemDC.DeleteDC();/刪除MemDCReleaseDC(pDC);/釋放DCvoid CdemoView:CharFill(CDC *pDC)/文字填充函數(shù)COLORREF BoundaryClr = RGB(0,0,0);/邊界色BOOL bSpanFill;pHead = new CStackNode;/建立棧結(jié)點pHead-pNext = NULL;/棧頭結(jié)點的指
32、針域總為空Push(Seed);/種子像素入棧int x, y, x0 = Round(Seed.x), y0 = Round(Seed.y);/x,y用于判斷種子與圖形的位置關(guān)系x = x0 - 1;while(pDC-GetPixel(x, y0) != BoundaryClr & pDC-GetPixel(x, y0) != SeedClr )/左方判斷x-;if(x GetPixel(x0, y) != BoundaryClr & pDC-GetPixel(x0, y) != SeedClr)/上方判斷y+;if(y = rect.Height() / 2)/到達(dá)客戶區(qū)最上端Messa
33、geBox(L種子不在圖形之內(nèi), L警告);return;x = x0 + 1;while(pDC-GetPixel(x, y0) != BoundaryClr & pDC-GetPixel(x, y0) != SeedClr)/右方判斷x+;if(x = rect.Width() / 2)/到達(dá)客戶區(qū)最右端MessageBox(L種子不在圖形之內(nèi), L警告);return;y = y0 - 1;while(pDC-GetPixel(x0, y) != BoundaryClr & pDC-GetPixel(x0, y) != SeedClr)/下方判斷y-;if(y pNext != NULL
34、)/如果棧不為空Pop(PopPoint);if(pDC-GetPixel(Round(PopPoint.x), Round(PopPoint.y) = SeedClr)continue;/ 分別向左和向右填充掃描線PointTemp = PopPoint;while(pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != BoundaryClr & pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != SeedClr)pDC-SetPixelV(Round(PointTemp.x),
35、 Round(PointTemp.y), SeedClr);PointTemp.x+;xright = PointTemp.x - 1;PointTemp.x = PopPoint.x - 1;while(pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != BoundaryClr & pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != SeedClr)pDC-SetPixelV(Round(PointTemp.x), Round(PointTemp.y), SeedClr);Po
36、intTemp.x-;xleft=PointTemp.x + 1;/處理上一條掃描線PointTemp.x = xleft;PointTemp.y = PointTemp.y + 1;while(PointTemp.x GetPixel(Round(PointTemp.x), Round(PointTemp.y) != BoundaryClr & pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != SeedClr)bSpanFill = TRUE;PointTemp.x+;if(bSpanFill)if(PointTemp.x = xr
37、ight & pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != BoundaryClr & pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != SeedClr)PopPoint = PointTemp;elsePopPoint.x = PointTemp.x - 1; PopPoint.y = PointTemp.y;Push(PopPoint);bSpanFill = FALSE;while(pDC-GetPixel(Round(PointTemp.x), Round(Poi
38、ntTemp.y) = BoundaryClr & PointTemp.x GetPixel(Round(PointTemp.x), Round(PointTemp.y) = SeedClr & PointTemp.x xright) PointTemp.x+;/處理下一條掃描線PointTemp.x = xleft;PointTemp.y = PointTemp.y - 2;while(PointTemp.x GetPixel(Round(PointTemp.x), Round(PointTemp.y) != BoundaryClr & pDC-GetPixel(Round(PointTemp.x),
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030中國服裝企業(yè)管理與ERP軟件行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 2025至2030中國暖通空調(diào)空氣濾清器行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 2025至2030中國無線寬帶CPE行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 2025至2030中國無機電致變色材料行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 2025至2030中國扁平電纜組件行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 2025至2030中國微藻DHA市場營銷策略與競爭前景分析報告
- 2025至2030中國微型監(jiān)控攝像機市場經(jīng)營管理風(fēng)險與運營效益分析報告
- 2025至2030中國干發(fā)香波行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 2025至2030中國帆船夾克行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 2025年韓語TOPIK中級考試真題卷:詞匯與語法難點解析試題
- 2025年 道路運輸企業(yè)主要負(fù)責(zé)人考試模擬試卷(100題)附答案
- 2025至2030中國執(zhí)法系統(tǒng)行業(yè)經(jīng)營效益及前景運行態(tài)勢分析報告
- 供應(yīng)鏈公司展會策劃方案
- 南通市崇川區(qū)招聘 社區(qū)工作者筆試真題2024
- 2025年廣東省萬閱大灣區(qū)百校聯(lián)盟中考二模語文試題(含答案)
- 【藝恩】出游趨勢洞察報告
- 護(hù)士理論考試試題及答案
- 學(xué)生因病缺課管理制度
- 分部驗收橋梁主體驗收評估報告
- 計算機網(wǎng)絡(luò)設(shè)計畢業(yè)設(shè)計論文
- 關(guān)于郵政代理金融業(yè)務(wù)發(fā)展轉(zhuǎn)型的思考
評論
0/150
提交評論