Advertisement
Guest User

KGLab5_3

a guest
May 25th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include <math.h>
  4. #include <fstream>
  5.  
  6. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  7. TCHAR WinName[] = _T("MainFrame");
  8.  
  9. static int sx,sy;
  10. const int SCALE = 1000;
  11. const int MARK = 4;
  12. int n,i,j,first;
  13. float x,y,X,Y,t,xA,xB,xC,xD,yA,yB,yC,yD,a0,a1,a2,a3,b0,b1,b2,b3,eps=0.04;
  14.  
  15. void DcInLp (POINT &point)
  16. {
  17.     point.x = point.x*SCALE/sx;
  18.     point.y = SCALE - point.y * SCALE/sy;
  19. }
  20.  
  21. void transform(HDC &hdc)
  22. {
  23.     SetMapMode (hdc, MM_ANISOTROPIC);
  24.     SetWindowExtEx(hdc,SCALE,-SCALE,NULL);
  25.     SetViewportExtEx(hdc,sx,sy,NULL);
  26.     SetViewportOrgEx(hdc,0,sy,NULL);
  27. }
  28.  
  29.  
  30. int APIENTRY WinMain(HINSTANCE This, HINSTANCE Prev, LPSTR cmd, int mode)
  31. {
  32.     HWND hWnd;
  33.     MSG msg;
  34.     WNDCLASS wc;
  35.     wc.hInstance = This;
  36.     wc.lpszClassName = WinName;
  37.     wc.lpfnWndProc = WndProc;
  38.     wc.style = CS_HREDRAW | CS_VREDRAW;
  39.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  40.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  41.     wc.lpszMenuName = NULL;
  42.     wc.cbClsExtra = 0;
  43.     wc.cbWndExtra = 0;
  44.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  45.     if(!RegisterClass(&wc)) return 0;
  46.     hWnd = CreateWindow(WinName, _T("B-spline"),
  47.         WS_OVERLAPPEDWINDOW,
  48.         CW_USEDEFAULT,
  49.         CW_USEDEFAULT,
  50.         CW_USEDEFAULT,
  51.         CW_USEDEFAULT,
  52.         HWND_DESKTOP,
  53.         NULL,
  54.         This,
  55.         NULL);
  56.  
  57.     ShowWindow(hWnd, mode);
  58.     while(GetMessage(&msg, NULL, 0, 0))
  59.     {
  60.         TranslateMessage(&msg);
  61.         DispatchMessage(&msg);
  62.     }
  63.     return 0; }
  64. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  65. {
  66.     HDC hdc;
  67.     PAINTSTRUCT ps;
  68.     static HBRUSH hRect,hSel;
  69.     static HPEN hDash, hBezier;
  70.     static POINT pt[20];
  71.     static POINT point;
  72.     RECT rt;
  73.     static int count, index;
  74.     static bool capture;
  75.     int i;
  76.     std::ifstream in;
  77.     std::ofstream out;
  78.     switch(message){
  79.     case WM_CREATE:
  80.         in.open("dat.txt");
  81.         if (in.fail())
  82.         {
  83.             MessageBox (hWnd,_T("Файл не найден"),_T("Открытие файла"),MB_OK|MB_ICONEXCLAMATION);
  84.             PostQuitMessage(0);
  85.             return 1; }
  86.         for (count = 0; in >> pt [count].x; count++) in >> pt[count].y;
  87.         in.close();
  88.         hDash = CreatePen (PS_DASH,1,0);
  89.         hBezier = CreatePen (PS_SOLID, 4, RGB(255,0,0));
  90.         hRect = CreateSolidBrush (RGB(128,0,128));
  91.         hSel = CreateSolidBrush (RGB(255,0,0));
  92.         break;
  93.     case WM_SIZE:
  94.         sx = LOWORD(lParam);
  95.         sy = HIWORD(lParam);
  96.         break;
  97.     case WM_LBUTTONDOWN:
  98.         point.x = LOWORD(lParam);
  99.         point.y = HIWORD(lParam);
  100.         DcInLp(point);
  101.         for (i = 0; i <= count; i++)
  102.         {
  103.             SetRect(&rt, pt[i].x-MARK, pt[i].y-MARK, pt[i].x+MARK,pt[i].y+MARK);
  104.             if(PtInRect(&rt, point))
  105.             {
  106.                 index = i;
  107.                 capture = true;
  108.                 hdc = GetDC(hWnd);
  109.                 transform(hdc);
  110.                 FillRect(hdc, &rt, hSel);
  111.                 ReleaseDC(hWnd, hdc);
  112.                 SetCapture(hWnd);
  113.                 return 0; } }
  114.         break;
  115.     case WM_LBUTTONUP:
  116.         if(capture)
  117.         {
  118.             ReleaseCapture();
  119.             capture = false;
  120.         }
  121.         break;
  122.     case WM_MOUSEMOVE:
  123.         if (capture)
  124.         {
  125.             point.x = LOWORD (lParam);
  126.             point.y = HIWORD (lParam);
  127.             DcInLp (point);
  128.             pt[index] = point;
  129.             InvalidateRect (hWnd,NULL,TRUE);
  130.         }
  131.         break;
  132.     case WM_PAINT:
  133.         hdc = BeginPaint (hWnd,&ps);
  134.         transform (hdc);
  135.         SelectObject (hdc, hDash);
  136.         Polyline (hdc, pt, count);
  137.         SelectObject (hdc, hBezier);
  138.         point.x = LOWORD(lParam);
  139.         point.y = HIWORD(lParam);
  140.         DcInLp(point);
  141.         for (i = 0; i <= count; i++)
  142.         {
  143.             MoveToEx(hdc, pt[i].x-eps, pt[i].y-eps, NULL);
  144.             LineTo(hdc, pt[i].x+eps,pt[i].y+eps);
  145.             MoveToEx(hdc, pt[i].x+eps, pt[i].y-eps, NULL);
  146.             LineTo(hdc, pt[i].x-eps,pt[i].y+eps);
  147.         }
  148.         first = 1;
  149.         for (i=1;i<count-1;i++)
  150.         { xA=pt[i-1].x;xB=pt[i].x;xC=pt[i+1].x;xD=pt[i+2].x;
  151.         yA=pt[i-1].y;yB=pt[i].y;yC=pt[i+1].y;yD=pt[i+2].y;
  152.         a3=(-xA+3*(xB-xC)+xD)/6; b3=(-yA+3*(yB-yC)+yD)/6;
  153.         a2=(xA-2*xB+xC)/2; b2=(yA-2*yB+yC)/2;
  154.         a1=(xC-xA)/2; b1=(yC-yA)/2;
  155.         a0=(xA+4*xB+xC)/6; b0=(yA+4*yB+yC)/6;
  156.         for (j=0;j<=count;j++)
  157.         { t=(float)j/(float)count;
  158.         X=((a3*t+a2)*t+a1)*t+a0; Y=((b3*t+b2)*t+b1)*t+b0;
  159.         if (first)
  160.         { first=0;
  161.         MoveToEx(hdc,X,Y, NULL);
  162.         }
  163.         else LineTo(hdc,X,Y);
  164.         } }
  165.         EndPaint(hWnd,&ps);
  166.         break;
  167.     case WM_DESTROY:
  168.         DeleteObject(hDash);
  169.         DeleteObject(hBezier);
  170.         DeleteObject(hRect);
  171.         DeleteObject(hSel);
  172.         out.open("dat.txt");
  173.         for (i = 0; i<count; i++) out << pt[i].x << '\t'<< pt[i].y << '\n';
  174.         out.close();
  175.         PostQuitMessage(0);
  176.         break;
  177.     default: return DefWindowProc(hWnd, message, wParam, lParam);
  178.     } return 0; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement