Advertisement
Guest User

KGLab5_2

a guest
May 25th, 2017
76
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 <fstream>
  4. static int sx, sy;
  5. const int SCALE = 1000;
  6. const int MARK = 4;
  7. const int WIDTH = 400;
  8. const int HEIGHT = 300;
  9. float v11, v12, v13, v21, v22, v23, v31, v32, v33, v43;
  10. float rho = 300., thetta = 75., phi =30., ScreenDist = 500.;
  11. float A,B,C,D,An,Bn,Cn;
  12. float xt[3],yt[3],zt[3];
  13. float Al,Bl,Cl;
  14. float alpha;
  15. float th, ph, costh, cosph, sinth, sinph;
  16. float factor = atan(1.0)/45.;
  17. 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;
  18. PAINTSTRUCT ps;
  19. static HBRUSH hBrush;
  20. class TFPoint {
  21.     public:
  22.     float X;
  23.     float Y;
  24.     float Z;
  25. };
  26. void VidMatCoeff(float rho, float thetta, float phi)
  27. {
  28.     th = thetta * factor; ph = phi * factor;
  29.     costh = cos(th); sinth=sin(th); cosph=cos(ph); sinph=sin(ph);
  30.     v11=-sinth; v12=-cosph*costh; v13=-sinph*costh;
  31.     v21=costh; v22=-cosph*sinth; v23=-sinph*sinth;
  32.     v31=0.; v32=sinph; v33=-cosph;
  33.     v43=rho;
  34. }
  35. POINT Perspective(float x, float y, float z)
  36. {
  37.     POINT point;
  38.     float xe, ye, ze;
  39.     VidMatCoeff(rho, thetta, phi);
  40.     xe=v11*x + v21*y;
  41.     ye=v12*x + v22*y + v32*z;
  42.     ze=v13*x + v23*y + v33 *z + v43;
  43.     point.x = ScreenDist * xe/ze + 400.;
  44.     point.y = ScreenDist * ye/ze + 300.;
  45.     return point;
  46. }
  47. void DcInLp(POINT &point)
  48. {
  49.     point.x = point.x = point.x*SCALE/sx;
  50.     point.y = SCALE - point.y*SCALE/sy;
  51. }
  52. void transform(HDC& hdc)
  53. {
  54.     SetMapMode(hdc, MM_ANISOTROPIC);
  55.     SetWindowExtEx(hdc, SCALE, -SCALE, NULL);
  56.     SetViewportExtEx(hdc, sx, sx, NULL);
  57.     SetViewportOrgEx(hdc, 0, sy, NULL);
  58. }
  59. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  60. TCHAR WinName[]=_T("MainFrame");
  61. int APIENTRY WinMain(HINSTANCE This, HINSTANCE Prev, LPSTR cmd, int mode)
  62. {
  63.     HWND hWnd;
  64.     MSG msg;
  65.     WNDCLASS wc;
  66.     wc.hInstance=This;
  67.     wc.lpszClassName=WinName;
  68.     wc.lpfnWndProc=WndProc;
  69.     wc.style=CS_HREDRAW|CS_VREDRAW;
  70.     wc.hIcon=LoadIcon(NULL, IDI_APPLICATION);
  71.     wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  72.     wc.lpszMenuName=NULL;
  73.     wc.cbClsExtra=0;
  74.     wc.cbWndExtra=0;
  75.     wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
  76.     if(!RegisterClass(&wc)) return 0;
  77.     hWnd=CreateWindow(WinName, _T("Кривая Безье"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, This, NULL);
  78.     ShowWindow(hWnd, mode);
  79.     while(GetMessage(&msg,NULL,0,0))
  80.     {TranslateMessage(&msg);
  81.     DispatchMessage(&msg);
  82.     } return 0;}
  83. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  84. {
  85.     PAINTSTRUCT ps;
  86.     HDC hdc;
  87.     static HPEN hDash, hBezier;
  88.     static HBRUSH hRect, hSel;
  89.     static POINT pt[20];
  90.     static POINT point;
  91.     RECT rt;
  92.     static int count, index;
  93.     static bool capture;
  94.     int i;
  95.     std::ifstream in;
  96.     std::ofstream out;
  97.     switch(message)
  98.     {
  99.         case WM_CREATE:
  100.             in.open("dat.txt");
  101.             if(in.fail()){
  102.                 MessageBox(hWnd, _T("Файл dat.txt не найден"),
  103.                     _T("Открытие файла"), MB_OK|MB_ICONEXCLAMATION);
  104.                 PostQuitMessage(0);
  105.                 return 1;}
  106.     for (count=0; in>>pt[count].x; count++) in >> pt[count].y;
  107.     in.close();
  108.     hDash=CreatePen(PS_DASH, 1, 0);
  109.     hBezier = CreatePen(PS_SOLID, 4, RGB(0,0,255));
  110.     hRect = CreateSolidBrush(RGB(128, 0, 128));
  111.     hSel = CreateSolidBrush(RGB(255, 0, 0));
  112.     break;
  113.         case WM_SIZE:
  114.             sx=500;
  115.             sy=500;
  116.             break;
  117.         case WM_LBUTTONDOWN:
  118.             point.x=LOWORD(lParam);
  119.             point.y=HIWORD(lParam);
  120.             DcInLp(point);
  121.             for(i=0;i<=count;i++){
  122.             SetRect(&rt, pt[i].x-MARK, pt[i].y-MARK, pt[i].x+MARK, pt[i].y+MARK);
  123.             if(PtInRect(&rt, point))
  124.             {
  125.                 index=i;
  126.                 capture=true;
  127.                 hdc=GetDC(hWnd);
  128.                 transform(hdc);
  129.                 FillRect(hdc, &rt, hSel);
  130.                 ReleaseDC(hWnd, hdc);
  131.                 SetCapture(hWnd);
  132.                 return 0;}}
  133.             break;
  134.         case WM_LBUTTONUP:
  135.             if(capture)
  136.             {
  137.                 ReleaseCapture();
  138.                 capture=false;
  139.             }
  140.             break;
  141.         case WM_MOUSEMOVE:
  142.             if (capture)
  143.             {
  144.                 point.x=LOWORD(lParam);
  145.                 point.y=HIWORD(lParam);
  146.                 DcInLp(point);
  147.                 pt[index]=point;
  148.                 InvalidateRect(hWnd, NULL, TRUE);
  149.             }
  150.             break;
  151.         case WM_PAINT:
  152.             hdc=BeginPaint(hWnd, &ps);
  153.             transform(hdc);
  154.             SelectObject(hdc, hDash);
  155.             Polyline(hdc, pt, count);
  156.             SelectObject(hdc, hBezier);
  157.             PolyBezier(hdc, pt, count);
  158.             for(i=0; i<count; i++){
  159.             SetRect(&rt, pt[i].x-MARK, pt[i].y-MARK, pt[i].x+MARK, pt[i].y+MARK);
  160.                 FillRect(hdc, &rt, hRect);
  161.             }
  162.             EndPaint(hWnd, &ps);
  163.             break;
  164.         case WM_DESTROY: PostQuitMessage(0);
  165.             DeleteObject(hDash);
  166.             DeleteObject(hBezier);
  167.             DeleteObject(hRect);
  168.             DeleteObject(hSel);
  169.             out.open("dat.txt");
  170.             for (i=0; i<count; i++) out << pt[i].x << '\t' << pt[i].y << '\n';
  171.             out.close();
  172.             break;
  173.         default: return DefWindowProc(hWnd, message, wParam, lParam);
  174.     } return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement