Advertisement
Bassone

Problem 2

Nov 22nd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.96 KB | None | 0 0
  1. #if defined(UNICODE) && !defined(_UNICODE)
  2.     #define _UNICODE
  3. #elif defined(_UNICODE) && !defined(UNICODE)
  4.     #define UNICODE
  5. #endif
  6.  
  7. #include <tchar.h>
  8. #include <windows.h>
  9. #include <cmath>
  10.  
  11. int Round ( double x )
  12. {
  13.     return (int) ( x + 0.5 );
  14. }
  15.  
  16.  
  17. void swappoints ( int &x1 , int &y1 , int &x2 , int &y2)
  18. {
  19.     int temp = x1 ;
  20.     x1 = x2 ;
  21.     x2 = temp ;
  22.     temp = y1 ;
  23.     y1 = y2 ;
  24.     y2 = temp;
  25. }
  26.  
  27. void DrawLine ( HDC hdc , int xs , int ys , int xe , int ye , COLORREF c )
  28. {
  29.     int dx = xe - xs , dy = ye - ys ;
  30.     if ( abs(dx) > abs(dy) )
  31.     {
  32.         double m = (double) dy / dx ;
  33.         if ( xs > xe)
  34.             swappoints ( xs , ys , xe , ye);
  35.         int x = xs ;
  36.         double y = ys;
  37.         SetPixel(hdc , xs , ys , c);
  38.         while ( x < xe )
  39.         {
  40.             x++;
  41.             y+=m;
  42.             SetPixel(hdc , x , Round(y),c);
  43.         }
  44.     }
  45.         else
  46.         {
  47.             double m = (double) dx / dy ;
  48.         if ( ys > ye)
  49.             swappoints ( xs , ys , xe , ye);
  50.         int x = xs ;
  51.         double y = ys;
  52.         SetPixel(hdc , xs , ys , c);
  53.         while ( y < ye )
  54.         {
  55.             y++;
  56.             x+=m;
  57.             SetPixel(hdc , Round(x) , y ,c);
  58.         }
  59.     }
  60.  
  61. }
  62.  
  63. void Draw8points ( HDC hdc , int x , int y , int xc , int yc , COLORREF c)
  64. {
  65.     SetPixel(hdc , xc + x , yc + y , c);
  66.     DrawLine(hdc , xc , yc , xc + x , yc + y , RGB(255,0,0));
  67.     SetPixel(hdc , xc - x , yc + y , c);
  68.     DrawLine(hdc , xc , yc , xc - x , yc + y , RGB(0,255,0));
  69.     SetPixel(hdc , xc - x , yc - y , c);
  70.     DrawLine(hdc , xc , yc , xc - x , yc - y , RGB(255,250,0));
  71.     SetPixel(hdc , xc + x , yc - y , c );
  72.     DrawLine(hdc , xc , yc , xc + x , yc - y , RGB(255,0,255));
  73.     SetPixel(hdc , xc + y , yc + x , c);
  74.     DrawLine(hdc , xc , yc , xc + y , yc + x , RGB(0,255,255));
  75.     SetPixel(hdc , xc - y , yc + x , c);
  76.     DrawLine(hdc , xc , yc , xc - y , yc + x , RGB(255,255,255));
  77.     SetPixel(hdc , xc - y , yc - x , c);
  78.     DrawLine(hdc , xc , yc , xc - y , yc - x , RGB(255,169,175));
  79.     SetPixel(hdc , xc + y , yc - x , c);
  80.     DrawLine(hdc , xc , yc , xc + y , yc - x , RGB(123,123,123));
  81.  
  82. }
  83.  
  84. void DrawCircle ( HDC hdc , int xc , int yc , int R , COLORREF c )
  85. {
  86.  
  87.     double x = 0 , y = R ;
  88.     Draw8points( hdc , x , y , xc , yc , c );
  89.     double d = 1 - R ;
  90.     while ( x < y )
  91.     {
  92.         if ( d < 0 )
  93.         {
  94.             d += 2 * x + 3 ;
  95.             x++ ;
  96.         }
  97.         else
  98.         {
  99.             d += 2 * ( x - y ) + 5 ;
  100.             x++ ;
  101.             y-- ;
  102.         }
  103.         Draw8points(hdc , Round (x) , Round (y) , xc , yc , c);
  104.     }
  105.  
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. /*  Declare Windows procedure  */
  114. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  115.  
  116. /*  Make the class name into a global variable  */
  117. TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
  118.  
  119. int WINAPI WinMain (HINSTANCE hThisInstance,
  120.                      HINSTANCE hPrevInstance,
  121.                      LPSTR lpszArgument,
  122.                      int nCmdShow)
  123. {
  124.     HWND hwnd;               /* This is the handle for our window */
  125.     MSG messages;            /* Here messages to the application are saved */
  126.     WNDCLASSEX wincl;        /* Data structure for the windowclass */
  127.  
  128.     /* The Window structure */
  129.     wincl.hInstance = hThisInstance;
  130.     wincl.lpszClassName = szClassName;
  131.     wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
  132.     wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
  133.     wincl.cbSize = sizeof (WNDCLASSEX);
  134.  
  135.     /* Use default icon and mouse-pointer */
  136.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  137.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  138.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  139.     wincl.lpszMenuName = NULL;                 /* No menu */
  140.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  141.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  142.     /* Use Windows's default colour as the background of the window */
  143.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  144.  
  145.     /* Register the window class, and if it fails quit the program */
  146.     if (!RegisterClassEx (&wincl))
  147.         return 0;
  148.  
  149.     /* The class is registered, let's create the program*/
  150.     hwnd = CreateWindowEx (
  151.            0,                   /* Extended possibilites for variation */
  152.            szClassName,         /* Classname */
  153.            _T("Code::Blocks Template Windows App"),       /* Title Text */
  154.            WS_OVERLAPPEDWINDOW, /* default window */
  155.            CW_USEDEFAULT,       /* Windows decides the position */
  156.            CW_USEDEFAULT,       /* where the window ends up on the screen */
  157.            544,                 /* The programs width */
  158.            375,                 /* and height in pixels */
  159.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  160.            NULL,                /* No menu */
  161.            hThisInstance,       /* Program Instance handler */
  162.            NULL                 /* No Window Creation data */
  163.            );
  164.  
  165.     /* Make the window visible on the screen */
  166.     ShowWindow (hwnd, nCmdShow);
  167.  
  168.     /* Run the message loop. It will run until GetMessage() returns 0 */
  169.     while (GetMessage (&messages, NULL, 0, 0))
  170.     {
  171.         /* Translate virtual-key messages into character messages */
  172.         TranslateMessage(&messages);
  173.         /* Send message to WindowProcedure */
  174.         DispatchMessage(&messages);
  175.     }
  176.  
  177.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  178.     return messages.wParam;
  179. }
  180.  
  181.  
  182. /*  This function is called by the Windows function DispatchMessage()  */
  183. int xc , yc , x , y  ;
  184. int counter = 0 ;
  185. bool startpaint = false ;
  186.  
  187.  
  188. PAINTSTRUCT ps ;
  189. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  190. {
  191.     switch (message)                  /* handle the messages */
  192.     {
  193.         case WM_DESTROY:
  194.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  195.             break;
  196.  
  197.         case WM_LBUTTONDOWN:
  198.             if ( counter == 0)
  199.             {
  200.             xc = LOWORD(lParam);
  201.             yc = HIWORD(lParam);
  202.             counter++ ;
  203.             }
  204.             else
  205.             {
  206.             x = LOWORD(lParam);
  207.             y = HIWORD(lParam);
  208.             counter-- ;
  209.             startpaint = true ;
  210.             }
  211.             break;
  212.  
  213.         case WM_PAINT:
  214.             if ( startpaint == true )
  215.             {
  216.                 BeginPaint( hwnd , &ps );
  217.                 double R =  sqrt( pow( x - xc , 2.0 ) + pow( y - yc , 2.0) ) ;
  218.                 DrawCircle ( ps.hdc , xc , yc , R , RGB(0,0,255));
  219.                 EndPaint( hwnd , &ps );
  220.             }
  221.                 break;
  222.  
  223.  
  224.  
  225.  
  226.         default:                      /* for messages that we don't deal with */
  227.             return DefWindowProc (hwnd, message, wParam, lParam);
  228.     }
  229.  
  230.     return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement