Computer graphics-code_2

Source: Internet
Author: 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 {//DOT structure public:int x; int y;struct Point (): X (0), y (0) {}struct points (int a, int b): X (a), Y (b) {}}point;typedef struct node {//Link table node Public:poi  NT data;struct Node *next = null;struct node ():d ata (0, 0), next (null) {}struct Node (int a, int b):d ata (A, B), Next (null) {}}node, *pnode;//global variable static TCHAR szwindowclass[] = _t ("Win32app"); static TCHAR sztitle[] = _t ("Win32 application"); HIN Stance HInst; UINT width = 800;//window width uint height = 600;//window height colorref bkg_clr = RGB (255, 255, 255);//Background color node stack;//stack Vector<point &  Gt points_set;//Polygon Point Set//function declaration lresult CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);//Message processing void display (HDC hdc);//wm_ Paint message response, drawing function/* Drawing related */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);//Draw line 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);//ellipse 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);//Rectangle void Polygon (hdc hdc, VECTOR&LT;P oint>* set, Colorref color = RGB (255, 255, 255), int line_width = 1, int line_style = ps_solid);//Draw polygon void Polygon_poi NTS ();//Polygon Point set generation/* Stack related */void setstackempty (pnode L);//Stack empty bool Isstackempty (pnode L);//Determine if stack is empty void Stackpush (Pnode l, Point e);//into the stack point Stackpop (Pnode L);//out of the stack/* implementation */void Scanlinefill (HDC hdc, int x, int y, colorref oldcolor, COLORREF newcol OR);//scan fills int WINAPI WinMain (hinstance hinstance,hinstance hprevinstance,lpstr lpcmdline,int ncmdshow) {wndclassex wcex;//initialization window class 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)); RegisterClassEx (&wcex)) {//Register window Class 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) ;//Create window if (!hwnd) {//If Create failed MessageBox (null,_t ("Call to CreateWindow failed!"), _t ("Win32 application"), NULL); return 1;} ShowWindow (hwnd,ncmdshow);//Display UpdateWindow (HWND);//update msg Msg;while again before display (GetMessage (&msg, NULL, 0, 0)) {// Gets the message in the message Queue TranslateMessage (&msg);D ispatchmessage (&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://Redraw hdc = BeginPaint (hWnd, &ps);d isplay (HDC); EndPaint (HWnd, &ps); ReleaseDC (HWND,HDC); Break;case wm_char://response button switch (wParam) {case 27://escpostquitmessage (0); return 0;case wm_destroy:postquitmessage (0); Break;case wm_close:postquitmessage (0); return 0;default:return DefWindowProc (hWnd, message, WParam, LParam); return 0;} void display (HDC hdc) {polygon_points (); Srand ((unsigned) time (NULL));p Olygon (hdc, &AMP;POINTS_SET,RGB (255,0,0), 3); Ellipse (hdc, Max, Max, Max, Max, RGB (0, 255, 0), 3), Rect (hdc, n, Z, 550, RGB (0, 0, 255), 3); for (;;) {int rgb_a = rand ()% 256;int rgb_b = rand ()% 256;int Rgb_c = rand ()% 256; Scanlinefill (hdc, +,/*bkg_clr*/getpixel, HDC, +, (), RGB (Rgb_a, Rgb_b, Rgb_c)); Scanlinefill (hdc, WIDTH/2 +, HEIGHT/2 + ten,/*bkg_clr*/getpixel (hdc, WIDTH/2 + ten, HEIGHT/2 +), RGB (Rgb_b, Rgb_c, rgb_a)); Scanlinefill (hdc, RGB_A, 400,/*bkg_clr*/getpixel (HDC, n, N), RGB (Rgb_c, Rgb_b, Messa));Gebox (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_o Ld;pen_old= (Hpen) SelectObject (hdc, CreatePen (Line_style,line_width,color));//Select Brush 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) {H PEN pen_old;pen_old = (hpen) SelectObject (hdc, CreatePen (Line_style, line_width, color));//Select Brush 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));//Select Brush 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 (+ WIDTH/2, -130 + HEIGHT/2); Point C ( -20 + WIDTH/2, + HEIGHT/2); Point D ( -120 + WIDTH/2, -55 + HEIGHT/2); Point E ( -20 + WIDTH/2, + HEIGHT/2); Point F ( -80 + WIDTH/2, + height/2);p oints_set.push_back (A);p oints_set.push_back (B);p oints_set.push_back (C);p oints _set.push_back (D);p oints_set.push_back (E);p oints_set.push_back (F); void Polygon (HDC hdc,vector<point>* set, colorref color, int line_width, int line_style) {for (Auto PR = Set->beg In () +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) {//stack empty l->next = null;} BOOL Isstackempty (Pnode L) {//If the stack is empty if (L->next = = NULL) return True;elsereturn FALSE;} void StAckpush (Pnode L, point e) {//stack Pnode temp = new Node;pnode pt = L;while (pt->next) pt = pt->next;temp->data.x = e. X;TEMP-&GT;DATA.Y = E.y;pt->next = temp;} Point Stackpop (Pnode L) {//out of Stack point Temp;pnode pt = l;while (pt->next) pt = pt->next;temp.x = PT-&GT;DATA.X;TEMP.Y = P T->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);p t.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) {//Right-fill setpixel (hdc, x, y, Newcolor); XR = X-1;x = Pt.x-1;while (GetPixel (hdc, x, y) = = Oldcolor) {//left padding SetPixel (hdc, x, y, newcolor); x--;} XL = x + 1;//processing above a scan line x = Xl;y = y + 1;while (x < XR) {Scanneedfill = False;while (HDC, x, y) = = GetPixel) {SC Anneedfill = 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 + +;} Process the following scan line x = Xl;y = y-2;while (x < XR) {Scanneedfill = False;while (HDC, x, y) = = GetPixel) {Oldcolor = 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.