Computer Graphics - code_2

來源:互聯網
上載者:User

標籤:

#include <windows.h>#include <stdlib.h>#include <string.h>#include <tchar.h>#include <vector>#include <string>#include <time.h>using namespace std;typedef struct  Point {//點結構public:int x;int y;struct Point() :x(0), y(0) {}struct Point(int  a, int  b) :x(a), y(b) {}}Point;typedef struct Node {//鏈表結點public:Point data;struct Node *next = NULL;struct Node() :data(0, 0), next(NULL) {}struct Node(int a, int b) :data(a, b), next(NULL) {}}Node, *pNode;//全域變數static TCHAR szWindowClass[] = _T("win32app");static TCHAR szTitle[] = _T("Win32  Application");HINSTANCE hInst;UINT WIDTH = 800;//視窗寬度UINT HEIGHT = 600;//視窗高度COLORREF bkg_clr = RGB(255, 255, 255);//背景色Node stack;//棧vector<Point  >  points_set;//多邊形點集//函式宣告LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//訊息處理void display(HDC hDC);//WM_PAINT訊息響應,繪製函數/*繪圖相關*/void line(HDC hDC, int x_beg, int y_beg, int x_end, int y_end, COLORREF color = RGB(255, 255, 255), int line_width=1, int line_style= PS_SOLID);//畫線void ellipse(HDC hDC, int x_left, int y_top, int x_right, int y_bottom, COLORREF color = RGB(255, 255, 255), int line_width = 1, int line_style = PS_SOLID);//橢圓void rect(HDC hDC, int x_left, int y_top, int x_right, int y_bottom, COLORREF color = RGB(255, 255, 255), int line_width = 1, int line_style = PS_SOLID);//矩形void polygon(HDC hDC, vector<Point>* set, COLORREF color = RGB(255, 255, 255), int line_width = 1, int line_style = PS_SOLID);//繪製多邊形void polygon_points();//多邊形點集產生/*棧相關*/void  SetStackEmpty(pNode L);//將棧置空bool isStackEmpty(pNode L);//判斷棧是否空void StackPush(pNode L, Point e);//入棧Point StackPop(pNode L);//出棧/*實現*/void ScanLineFill(HDC hDC, int x, int y, COLORREF oldcolor, COLORREF newcolor);//掃描填充int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){WNDCLASSEX wcex;//初始化視窗類別wcex.cbSize = sizeof(WNDCLASSEX);wcex.style = CS_HREDRAW | CS_VREDRAW|CS_OWNDC;wcex.lpfnWndProc = WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)CreateSolidBrush(bkg_clr);wcex.lpszMenuName = NULL;wcex.lpszClassName = szWindowClass;wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));if (!RegisterClassEx(&wcex)){//註冊視窗類別MessageBox(NULL,_T("Call to RegisterClassEx failed!"),_T("Win32 Application"),NULL);return 1;}hInst = hInstance; HWND hWnd = CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,200,100,WIDTH, HEIGHT,NULL,NULL,hInstance,NULL);//建立視窗if (!hWnd){//如果建立失敗MessageBox(NULL,_T("Call to CreateWindow failed!"),_T("Win32 Application"),NULL);return 1;}ShowWindow(hWnd,nCmdShow);//顯示UpdateWindow(hWnd);//在顯示前再次更新MSG msg;while (GetMessage(&msg, NULL, 0, 0)){//擷取訊息佇列中的訊息TranslateMessage(&msg);DispatchMessage(&msg);}return (int)msg.wParam;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){PAINTSTRUCT ps;HDC hdc;switch (message){case WM_PAINT://重繪hdc = BeginPaint(hWnd, &ps);display(hdc);EndPaint(hWnd, &ps);ReleaseDC(hWnd,hdc);break;case WM_CHAR://響應按鍵switch (wParam) {case 27://EscPostQuitMessage(0);break;}return 0;case WM_DESTROY:PostQuitMessage(0);break;case WM_CLOSE:PostQuitMessage(0);return 0;default:return DefWindowProc(hWnd, message, wParam, lParam);break;}return 0;}void display(HDC hDC) {polygon_points();srand((unsigned)time(NULL));polygon(hDC, &points_set,RGB(255,0,0),3);ellipse(hDC, 50, 50, 200, 150, RGB(0, 255, 0),3);rect(hDC, 50, 300, 150, 550, RGB(0, 0, 255), 3);for (;;) {int rgb_a = rand() % 256;int rgb_b = rand() % 256;int rgb_c = rand() % 256;ScanLineFill(hDC, 100, 75, /*bkg_clr*/GetPixel(hDC, 100, 75), RGB(rgb_a, rgb_b, rgb_c));ScanLineFill(hDC, WIDTH / 2 + 10, HEIGHT / 2 + 10,  /*bkg_clr*/GetPixel(hDC, WIDTH / 2 + 10, HEIGHT / 2 + 10), RGB(rgb_b, rgb_c, rgb_a));ScanLineFill(hDC, 75, 400,/*bkg_clr*/GetPixel(hDC, 75, 400), RGB(rgb_c, rgb_b, rgb_a));if (MessageBox(0,  _T("continue?"), _T("warning"),MB_YESNO | MB_ICONQUESTION) == IDYES)continue;elsebreak;}}void line(HDC hDC, int x_beg, int y_beg, int x_end, int y_end, COLORREF color,int line_width , int line_style) {HPEN pen_old;pen_old=(HPEN)SelectObject(hDC, CreatePen(line_style,line_width,color));//選擇畫筆MoveToEx(hDC, x_beg, y_beg, NULL);LineTo(hDC, x_end, y_end);SelectObject(hDC, pen_old);}void ellipse(HDC hDC, int x_left, int y_top, int x_right, int y_bottom, COLORREF color, int line_width, int line_style) {HPEN pen_old;pen_old = (HPEN)SelectObject(hDC, CreatePen(line_style, line_width, color));//選擇畫筆SelectObject(hDC,GetStockObject(NULL_BRUSH));Ellipse(hDC, x_left, y_top, x_right, y_bottom);SelectObject(hDC, pen_old);}void rect(HDC hDC, int x_left, int y_top, int x_right, int y_bottom, COLORREF color, int line_width, int line_style) {HPEN pen_old;pen_old = (HPEN)SelectObject(hDC, CreatePen(line_style, line_width, color));//選擇畫筆SelectObject(hDC, GetStockObject(NULL_BRUSH));Rectangle( hDC, x_left, y_top, x_right, y_bottom);SelectObject(hDC, pen_old);}void polygon_points() {Point A(20+WIDTH/2, 150+HEIGHT/2);Point B(100 + WIDTH / 2, -130 + HEIGHT / 2);Point C(-20 + WIDTH / 2, 15 + HEIGHT / 2);Point D(-120 + WIDTH / 2, -55 + HEIGHT / 2);Point E(-20 + WIDTH / 2, 100 + HEIGHT / 2);Point F(-80 + WIDTH / 2, 80 + HEIGHT / 2);points_set.push_back(A);points_set.push_back(B);points_set.push_back(C);points_set.push_back(D);points_set.push_back(E);points_set.push_back(F);}void polygon(HDC hDC,vector<Point>* set, COLORREF color, int line_width, int line_style) {for (auto pr = set->begin()+1; pr != set->end(); ++pr) {auto pl = pr - 1;line(hDC, pl->x, pl->y, pr->x, pr->y, color,line_width);}line(hDC, (set->end()-1)->x, (set->end()-1)->y, set->begin()->x, set->begin()->y, color, line_width);}void  SetStackEmpty(pNode L) {//將棧置空L->next = NULL;}bool isStackEmpty(pNode L) {//判是否棧空if (L->next == NULL)return TRUE;elsereturn FALSE;}void StackPush(pNode L, Point e) {//入棧pNode temp = new Node;pNode pt = L;while (pt->next)pt = pt->next;temp->data.x = e.x;temp->data.y = e.y;pt->next = temp;}Point StackPop(pNode L) {//出棧Point temp;pNode pt = L;while (pt->next)pt = pt->next;temp.x = pt->data.x;temp.y = pt->data.y;pt = L;while(pt->next->next)pt = pt->next;pt->next = NULL;return temp;}void ScanLineFill(HDC hDC,int x, int y, COLORREF oldcolor, COLORREF newcolor) {int xl, xr, i;bool scanNeedFill;Point pt;SetStackEmpty(&stack);pt.x = x;pt.y = y;StackPush(&stack, pt);while (!isStackEmpty(&stack)) {pt = StackPop(&stack);y = pt.y;x = pt.x;while (GetPixel(hDC, x, y) == oldcolor) {//向右填充SetPixel(hDC, x, y, newcolor);x++;}xr = x - 1;x = pt.x - 1;while (GetPixel(hDC, x, y) == oldcolor) {//向左填充SetPixel(hDC, x, y, newcolor);x--;}xl = x + 1;//處理上面一條掃描線x = xl;y = y + 1;while (x < xr) {scanNeedFill = FALSE;while (GetPixel(hDC, x, y) == oldcolor) {scanNeedFill = TRUE;x++;}if (scanNeedFill) {pt.x = x - 1;pt.y = y;StackPush(&stack, pt);scanNeedFill = FALSE;}while (GetPixel(hDC, x, y) != oldcolor&&x < xr)x++;}//處理下面一條掃描線x = xl;y = y - 2;while (x < xr) {scanNeedFill = FALSE;while (GetPixel(hDC, x, y) == oldcolor) {scanNeedFill = TRUE;x++;}if (scanNeedFill) {pt.x = x - 1;pt.y = y;StackPush(&stack, pt);scanNeedFill = FALSE;}while (GetPixel(hDC, x, y) != oldcolor&&x < xr)x++;}}}

  

Computer Graphics - code_2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.