Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include<windows.h>
- #include<math.h>
- #include"vector2D.h"
- COLORREF clrs[6]={0xAF0000,0x00AF00,0x0000AF,0x00AFAF,0xAF00AF};
- double scale=1;
- COLORREF defClr=clrs[5];
- int clrPt=5;
- int maxRec=4;
- void redrawAll(HWND);
- LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
- int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
- PSTR szCmdLine, int iCmdShow)
- {
- static TCHAR szAppName[] = TEXT ("Fractal Curves Demo") ;
- HWND hwnd ;
- MSG msg ;
- WNDCLASS wndclass ;
- wndclass.style = CS_HREDRAW | CS_VREDRAW ;
- wndclass.lpfnWndProc = WndProc ;
- wndclass.cbClsExtra = 0 ;
- wndclass.cbWndExtra = 0 ;
- wndclass.hInstance = hInstance ;
- wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
- wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
- wndclass.lpszMenuName = NULL ;
- wndclass.lpszClassName = szAppName ;
- if (!RegisterClass (&wndclass))
- {
- MessageBox (NULL, TEXT ("Can not register the window class!"),
- szAppName, MB_ICONERROR) ;
- return 0 ;
- }
- hwnd = CreateWindow (szAppName, " CSCB506-5: Recursive Curves Demo",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT,
- CW_USEDEFAULT, CW_USEDEFAULT,
- NULL, NULL, hInstance, NULL) ;
- ShowWindow (hwnd, iCmdShow) ;
- UpdateWindow (hwnd) ;
- while (GetMessage (&msg, NULL, 0, 0))
- {
- TranslateMessage (&msg) ;
- DispatchMessage (&msg) ;
- }
- return msg.wParam ;
- }
- LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- static int cxClient, cyClient ;
- switch (message)
- {
- case WM_SIZE :
- cxClient = LOWORD (lParam) ;
- cyClient = HIWORD (lParam) ;
- scale=cxClient<cyClient?cxClient*0.1:cyClient*0.1;
- return 0 ;
- case WM_KEYDOWN:
- switch (wParam)
- {
- case VK_LEFT:
- clrPt=(clrPt+1)%6;defClr=clrs[clrPt];
- break ;
- case VK_RIGHT:
- clrPt=(clrPt+5)%6;defClr=clrs[clrPt];
- break ;
- case VK_UP:
- maxRec--;
- break ;
- case VK_DOWN:
- maxRec++;
- break ;
- default: return 0;
- }
- InvalidateRect(hwnd,NULL,TRUE);
- return 0 ;
- case WM_PAINT:
- redrawAll(hwnd);
- return 0 ;
- case WM_DESTROY:
- PostQuitMessage (0) ;
- return 0 ;
- }
- return DefWindowProc (hwnd, message, wParam, lParam) ;
- }
- //Инкрементален алгоритъм на Мичнър за растеризация на окръжност
- void circle(HDC hdc,POINT cen,int R,COLORREF c){
- int x0=cen.x,y0=cen.y,x=0,y=R,d=3-2*R;//започва се от най-високата точка
- while(x<=y){//изчисляват се пикселите за горната дясна осмина
- //от окръжността. За останалите части се използва симетрията.
- SetPixel (hdc,x0+x,y0+y,c);SetPixel (hdc,x0-x,y0+y,c);
- SetPixel (hdc,x0+x,y0-y,c);SetPixel (hdc,x0-x,y0-y,c);
- SetPixel (hdc,x0+y,y0+x,c);SetPixel (hdc,x0+y,y0-x,c);
- SetPixel (hdc,x0-y,y0+x,c);SetPixel (hdc,x0-y,y0-x,c);
- d=d<0?d+4*x++ +6:d+4*(x++ -y--)+10;
- }
- }
- void drawCircle(HDC hdc, POINT startPoint, int radius) {
- static int rec=0; rec++;
- POINT leftCenter = startPoint;
- POINT rightCenter = startPoint;
- leftCenter.x += radius;
- rightCenter.x -= radius;
- if(rec>maxRec) circle(hdc, startPoint, radius, RGB(160,0,0));
- else{
- drawCircle(hdc, startPoint, radius);
- drawCircle(hdc, leftCenter, radius/2);
- drawCircle(hdc, rightCenter, radius/2 );
- }
- rec--;
- }
- void redrawAll(HWND hwnd){
- HDC hdc;RECT rect;PAINTSTRUCT ps;
- TCHAR txt[30];
- // wsprintf(txt, "Дълбочинанарекурсията: %i ", maxRec);
- hdc = BeginPaint (hwnd, &ps) ;
- GetClientRect (hwnd, &rect) ;
- // DrawText (hdc,txt, -1,&rect,DT_SINGLELINE | DT_TOP | DT_LEFT) ;
- Point cen((rect.left+rect.right)/2,(rect.top+rect.bottom)/2);
- cen=cen*(1/scale);
- // DrawText (hdc, "Кривана Koch: Up/Down задълбочина"
- // " , Left/Right зацвят, Home/End = смяна.", -1,
- // &rect,DT_SINGLELINE | DT_BOTTOM | DT_LEFT) ;
- Point a(-5,0),b(5,0);a=a+cen;b=b+cen;
- Point abCenter = (a+b)*(1./2.);
- POINT startPoint;
- startPoint.x=(LONG)(abCenter.getX()*scale);
- startPoint.y=(LONG)(abCenter.getY()*scale);
- drawCircle(hdc, startPoint, 200);
- EndPaint (hwnd, &ps) ;
- }
Advertisement
Add Comment
Please, Sign In to add comment