VasilM

nqkakwi_krygove

Nov 16th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. //main.cpp
  2.  
  3. #include<windows.h>
  4. #include<math.h>
  5. #include"vector2D.h"
  6.  
  7.  
  8.  
  9. COLORREF clrs[6]={0xAF0000,0x00AF00,0x0000AF,0x00AFAF,0xAF00AF};
  10. double scale=1;
  11. COLORREF defClr=clrs[5];
  12. int clrPt=5;
  13. int maxRec=4;
  14.  
  15. void redrawAll(HWND);
  16.  
  17. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  18.  
  19. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  20.                     PSTR szCmdLine, int iCmdShow)
  21. {
  22.     static TCHAR szAppName[] = TEXT ("Fractal Curves Demo") ;
  23.     HWND         hwnd ;
  24.     MSG          msg ;
  25.     WNDCLASS     wndclass ;
  26.  
  27.     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  28.     wndclass.lpfnWndProc   = WndProc ;
  29.     wndclass.cbClsExtra    = 0 ;
  30.     wndclass.cbWndExtra    = 0 ;
  31.     wndclass.hInstance     = hInstance ;
  32.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  33.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  34.     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  35.     wndclass.lpszMenuName  = NULL ;
  36.     wndclass.lpszClassName = szAppName ;
  37.  
  38.     if (!RegisterClass (&wndclass))
  39.     {
  40.         MessageBox (NULL, TEXT ("Can not register the window class!"),
  41.             szAppName, MB_ICONERROR) ;
  42.         return 0 ;
  43.     }
  44.  
  45.     hwnd = CreateWindow (szAppName, " CSCB506-5: Recursive Curves Demo",
  46.         WS_OVERLAPPEDWINDOW,
  47.         CW_USEDEFAULT, CW_USEDEFAULT,
  48.         CW_USEDEFAULT, CW_USEDEFAULT,
  49.         NULL, NULL, hInstance, NULL) ;
  50.     ShowWindow (hwnd, iCmdShow) ;
  51.     UpdateWindow (hwnd) ;
  52.  
  53.     while (GetMessage (&msg, NULL, 0, 0))
  54.     {
  55.         TranslateMessage (&msg) ;
  56.         DispatchMessage (&msg) ;
  57.     }
  58.     return msg.wParam ;
  59. }
  60.  
  61. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  62. {
  63.     static int cxClient, cyClient ;
  64.  
  65.     switch (message)
  66.     {
  67.     case WM_SIZE :
  68.         cxClient = LOWORD (lParam) ;
  69.         cyClient = HIWORD (lParam) ;
  70.         scale=cxClient<cyClient?cxClient*0.1:cyClient*0.1;
  71.         return 0 ;
  72.     case WM_KEYDOWN:
  73.         switch (wParam)
  74.         {
  75.         case VK_LEFT:
  76.             clrPt=(clrPt+1)%6;defClr=clrs[clrPt];
  77.             break ;
  78.  
  79.         case VK_RIGHT:
  80.             clrPt=(clrPt+5)%6;defClr=clrs[clrPt];
  81.             break ;
  82.  
  83.         case VK_UP:
  84.             maxRec--;
  85.             break ;
  86.  
  87.         case VK_DOWN:
  88.             maxRec++;
  89.             break ;
  90.         default: return 0;
  91.         }
  92.         InvalidateRect(hwnd,NULL,TRUE);
  93.         return 0 ;
  94.  
  95.     case WM_PAINT:
  96.         redrawAll(hwnd);
  97.         return 0 ;
  98.  
  99.     case WM_DESTROY:
  100.         PostQuitMessage (0) ;
  101.         return 0 ;
  102.     }
  103.     return DefWindowProc (hwnd, message, wParam, lParam) ;
  104. }
  105.  
  106. //Инкрементален алгоритъм на Мичнър за растеризация на окръжност
  107. void circle(HDC hdc,POINT cen,int R,COLORREF c){
  108.     int x0=cen.x,y0=cen.y,x=0,y=R,d=3-2*R;//започва се от най-високата точка
  109.     while(x<=y){//изчисляват се пикселите за горната дясна осмина
  110.         //от окръжността. За останалите части се използва симетрията.
  111.         SetPixel (hdc,x0+x,y0+y,c);SetPixel (hdc,x0-x,y0+y,c);
  112.         SetPixel (hdc,x0+x,y0-y,c);SetPixel (hdc,x0-x,y0-y,c);
  113.         SetPixel (hdc,x0+y,y0+x,c);SetPixel (hdc,x0+y,y0-x,c);
  114.         SetPixel (hdc,x0-y,y0+x,c);SetPixel (hdc,x0-y,y0-x,c);
  115.         d=d<0?d+4*x++ +6:d+4*(x++ -y--)+10;
  116.     }
  117. }
  118.  
  119. void drawCircle(HDC hdc, POINT startPoint, int radius) {
  120.     static int rec=0; rec++;
  121.  
  122.     POINT leftCenter = startPoint;
  123.     POINT rightCenter = startPoint;
  124.  
  125.     leftCenter.x += radius;
  126.     rightCenter.x -= radius;
  127.  
  128.     if(rec>maxRec) circle(hdc, startPoint, radius, RGB(160,0,0));
  129.     else{      
  130.         drawCircle(hdc, startPoint, radius);
  131.         drawCircle(hdc, leftCenter, radius/2);
  132.         drawCircle(hdc, rightCenter, radius/2 );
  133.     }
  134.     rec--;
  135. }
  136.  
  137. void redrawAll(HWND hwnd){
  138.     HDC hdc;RECT rect;PAINTSTRUCT ps;
  139.     TCHAR txt[30];
  140.     //  wsprintf(txt, "Дълбочинанарекурсията: %i ", maxRec);
  141.     hdc = BeginPaint (hwnd, &ps) ;
  142.     GetClientRect (hwnd, &rect) ;
  143.     //      DrawText (hdc,txt, -1,&rect,DT_SINGLELINE | DT_TOP | DT_LEFT) ;
  144.  
  145.     Point cen((rect.left+rect.right)/2,(rect.top+rect.bottom)/2);
  146.     cen=cen*(1/scale);
  147.  
  148.     //          DrawText (hdc, "Кривана Koch: Up/Down задълбочина"
  149.     //                    " , Left/Right зацвят, Home/End = смяна.", -1,
  150.     //          &rect,DT_SINGLELINE | DT_BOTTOM | DT_LEFT) ;
  151.     Point a(-5,0),b(5,0);a=a+cen;b=b+cen;
  152.  
  153.     Point abCenter = (a+b)*(1./2.);
  154.  
  155.     POINT startPoint;
  156.     startPoint.x=(LONG)(abCenter.getX()*scale);
  157.     startPoint.y=(LONG)(abCenter.getY()*scale);
  158.  
  159.     drawCircle(hdc, startPoint, 200);
  160.  
  161.     EndPaint (hwnd, &ps) ;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment