实验三 区域填充扫描线算法.doc_第1页
实验三 区域填充扫描线算法.doc_第2页
实验三 区域填充扫描线算法.doc_第3页
实验三 区域填充扫描线算法.doc_第4页
实验三 区域填充扫描线算法.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

计算机图形学实验报告班级:计科14-3班 学号:1406010325 姓名: 张磊 日期:2017.4.11 成绩:_1、实验题目及目的 1.1 实验题目 实验三 区域填充扫描线算法1.2 实验目的掌握区域填充扫描线算法2、实验内容及实验步骤 实验内容:要求:(1)画基本图形(2)采用扫描线填充算法进行区域填充实验步骤:void ScanLineFill4(HDC hdc, int x, int y, int oldColor, int newColor);扫描线填充算法LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);处理主窗口的消息,WM_PAINT绘制要填充的图形3、程序关键代码和注释 #include stdafx.h#include 区域填充的扫描线算法.h#define MAX_LOADSTRING 100HINSTANCE hInst;/ 当前实例TCHAR szTitleMAX_LOADSTRING;/ 标题栏文本TCHAR szWindowClassMAX_LOADSTRING;/ 主窗口类名ATOMMyRegisterClass(HINSTANCE hInstance);BOOLInitInstance(HINSTANCE, int);LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);INT_PTR CALLBACKAbout(HWND, UINT, WPARAM, LPARAM);int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine); / TODO: 在此放置代码。MSG msg;HACCEL hAccelTable;/ 初始化全局字符串LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance);/ 执行应用程序初始化:if (!InitInstance (hInstance, nCmdShow)return FALSE;hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY);/ 主消息循环:while (GetMessage(&msg, NULL, 0, 0)if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)TranslateMessage(&msg);DispatchMessage(&msg);return (int) msg.wParam;ATOM MyRegisterClass(HINSTANCE hInstance)WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style= CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc= WndProc;wcex.cbClsExtra= 0;wcex.cbWndExtra= 0;wcex.hInstance= hInstance;wcex.hIcon= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY);wcex.hCursor= LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName= MAKEINTRESOURCE(IDC_MY);wcex.lpszClassName= szWindowClass;wcex.hIconSm= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL);return RegisterClassEx(&wcex);BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) HWND hWnd; hInst = hInstance; / 将实例句柄存储在全局变量中 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) return FALSE; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE;typedef struct int y, xLeft, xRight; Span;#define STACK_SIZE100/max stack size is 100Span SSTACK_SIZE;int top = 0;void SetStackEmpty()top = 0;bool isStackEmpty()return top = 0;bool PushStack(Span *pS)if (top 0)top-;*pS = Stop;return true;elsereturn false;void ScanLineFill4(HDC hdc, int x, int y, int oldColor, int newColor)Span span;span.y = y;span.xLeft = x;while (GetPixel(hdc, span.xLeft, span.y) = oldColor)SetPixel(hdc, span.xLeft, span.y, newColor);span.xLeft-;if (span.xLeft = x)/not a seed point actuallyreturn;elsespan.xLeft+;/compansatespan.xRight = x + 1;while (GetPixel(hdc, span.xRight, span.y) = oldColor)SetPixel(hdc, span.xRight, span.y, newColor);span.xRight+;if (span.xRight = x + 1)/cannot extend to rightspan.xRight = x;elsespan.xRight-;/compansateSetStackEmpty();PushStack(&span);while (!isStackEmpty()PopStack(&span);Span S;/new spanS.y = span.y + 1;S.xLeft = span.xLeft;bool xLeftNotSet = false;while (GetPixel(hdc, S.xLeft, S.y) = oldColor)S.xLeft-;if (S.xLeft = span.xLeft)/xLeft is not setxLeftNotSet = true;elseS.xLeft+;/compansateint i = S.xLeft;while (i S.xLeft)S.xRight = i - 1;PushStack(&S);xLeftNotSet = true;while (i = span.xRight & GetPixel(hdc, i, S.y) != oldColor)i+;/similar to aboveS.y = span.y - 1;S.xLeft = span.xLeft;bool xLeftNotSet = false;while (GetPixel(hdc, S.xLeft, S.y) = oldColor)S.xLeft-;if (S.xLeft = span.xLeft)/xLeft is not setxLeftNotSet = true;elseS.xLeft+;/compansateint i = S.xLeft;while (i S.xLeft)S.xRight = i - 1;PushStack(&S);xLeftNotSet = true;while (i = span.xRight & GetPixel(hdc, i, S.y) != oldColor)i+;LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)int wmId, wmEvent;PAINTSTRUCT ps;HDC hdc;switch (message)case WM_COMMAND:wmId = LOWORD(wParam);wmEvent = HIWORD(wParam);/ 分析菜单选择:switch (wmId)case IDM_ABOUT:DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);break;case IDM_EXIT:DestroyWindow(hWnd);break;default:return DefWindowProc(hWnd, message, wParam, lParam);break;case WM_LBUTTONDOWN:long x = LOWORD(lParam);long y = HIWORD(lParam);HDC hdc = GetDC(hWnd);ScanLineFill4(hdc, x, y, 0xcccccc, 0xffff00);/cyanReleaseDC(hWnd, hdc);break;case WM_PAINT:hdc = BeginPaint(hWnd, &ps);/ TODO: 在此添加任意绘图代码./画一个田字HPEN hPen = CreatePen(PS_SOLID, 10, 0xcccccc);HGDIOBJ hPenOld = SelectObject(hdc, hPen);MoveToEx(hdc, 100, 100, NULL);LineTo(hdc, 300, 100);LineTo(hdc, 300, 300);LineTo(hdc, 100, 300);LineTo(hdc, 100, 100);MoveToEx(hdc, 100, 200, NULL);LineTo(hdc, 300, 200);MoveToEx(hdc, 200, 100, NULL);LineTo(hdc, 200, 300);SelectObject(hdc, hPenOld);DeleteObject(hPen);/画一个三角形HBRUSH hBr = CreateSolidBrush(0xcccccc);HGDIOBJ hBrOld = SelectObject(hdc, hBr);POINT pt3;pt0.x = 400;pt0.y = 100;pt1.x = 500;pt1.y = 300;pt2.x = 600;pt2.y = 200;Polygon(hdc, pt, 3);SelectObject(hdc, hBrOld);DeleteObject(hBr);/提示信息TCHAR info = _T(在要填充的区域点击鼠标左键.);DrawText(hdc, info, ARRAYSIZE(info)-1, &ps.rcPaint, 0);EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);return 0;INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)UNREFERENCED_PARAMETER(lParam);switch (message)case WM_INITDIALOG:return (INT_PTR)TRUE;case WM_COMMAND:if (LOWORD(wParam) = IDOK | LOWORD(wParam) = IDCANCEL)EndDialog(hDlg, LOWORD(wParam);return (INT_PTR)TRUE

温馨提示

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

评论

0/150

提交评论