Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <tchar.h>
- #include <fstream>
- static int sx, sy;
- const int SCALE = 1000;
- const int MARK = 4;
- const int WIDTH = 400;
- const int HEIGHT = 300;
- float v11, v12, v13, v21, v22, v23, v31, v32, v33, v43;
- float rho = 300., thetta = 75., phi =30., ScreenDist = 500.;
- float A,B,C,D,An,Bn,Cn;
- float xt[3],yt[3],zt[3];
- float Al,Bl,Cl;
- float alpha;
- float th, ph, costh, cosph, sinth, sinph;
- float factor = atan(1.0)/45.;
- 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;
- PAINTSTRUCT ps;
- static HBRUSH hBrush;
- class TFPoint {
- public:
- float X;
- float Y;
- float Z;
- };
- void VidMatCoeff(float rho, float thetta, float phi)
- {
- th = thetta * factor; ph = phi * factor;
- costh = cos(th); sinth=sin(th); cosph=cos(ph); sinph=sin(ph);
- v11=-sinth; v12=-cosph*costh; v13=-sinph*costh;
- v21=costh; v22=-cosph*sinth; v23=-sinph*sinth;
- v31=0.; v32=sinph; v33=-cosph;
- v43=rho;
- }
- POINT Perspective(float x, float y, float z)
- {
- POINT point;
- float xe, ye, ze;
- VidMatCoeff(rho, thetta, phi);
- xe=v11*x + v21*y;
- ye=v12*x + v22*y + v32*z;
- ze=v13*x + v23*y + v33 *z + v43;
- point.x = ScreenDist * xe/ze + 400.;
- point.y = ScreenDist * ye/ze + 300.;
- return point;
- }
- void DcInLp(POINT &point)
- {
- point.x = point.x = point.x*SCALE/sx;
- point.y = SCALE - point.y*SCALE/sy;
- }
- void transform(HDC& hdc)
- {
- SetMapMode(hdc, MM_ANISOTROPIC);
- SetWindowExtEx(hdc, SCALE, -SCALE, NULL);
- SetViewportExtEx(hdc, sx, sx, NULL);
- SetViewportOrgEx(hdc, 0, sy, NULL);
- }
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- TCHAR WinName[]=_T("MainFrame");
- int APIENTRY WinMain(HINSTANCE This, HINSTANCE Prev, LPSTR cmd, int mode)
- {
- HWND hWnd;
- MSG msg;
- WNDCLASS wc;
- wc.hInstance=This;
- wc.lpszClassName=WinName;
- wc.lpfnWndProc=WndProc;
- wc.style=CS_HREDRAW|CS_VREDRAW;
- wc.hIcon=LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor=LoadCursor(NULL,IDC_ARROW);
- wc.lpszMenuName=NULL;
- wc.cbClsExtra=0;
- wc.cbWndExtra=0;
- wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
- if(!RegisterClass(&wc)) return 0;
- hWnd=CreateWindow(WinName, _T("Кривая Безье"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, This, NULL);
- ShowWindow(hWnd, mode);
- while(GetMessage(&msg,NULL,0,0))
- {TranslateMessage(&msg);
- DispatchMessage(&msg);
- } return 0;}
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hdc;
- static HPEN hDash, hBezier;
- static HBRUSH hRect, hSel;
- static POINT pt[20];
- static POINT point;
- RECT rt;
- static int count, index;
- static bool capture;
- int i;
- std::ifstream in;
- std::ofstream out;
- switch(message)
- {
- case WM_CREATE:
- in.open("dat.txt");
- if(in.fail()){
- MessageBox(hWnd, _T("Файл dat.txt не найден"),
- _T("Открытие файла"), MB_OK|MB_ICONEXCLAMATION);
- PostQuitMessage(0);
- return 1;}
- for (count=0; in>>pt[count].x; count++) in >> pt[count].y;
- in.close();
- hDash=CreatePen(PS_DASH, 1, 0);
- hBezier = CreatePen(PS_SOLID, 4, RGB(0,0,255));
- hRect = CreateSolidBrush(RGB(128, 0, 128));
- hSel = CreateSolidBrush(RGB(255, 0, 0));
- break;
- case WM_SIZE:
- sx=500;
- sy=500;
- break;
- case WM_LBUTTONDOWN:
- point.x=LOWORD(lParam);
- point.y=HIWORD(lParam);
- DcInLp(point);
- for(i=0;i<=count;i++){
- SetRect(&rt, pt[i].x-MARK, pt[i].y-MARK, pt[i].x+MARK, pt[i].y+MARK);
- if(PtInRect(&rt, point))
- {
- index=i;
- capture=true;
- hdc=GetDC(hWnd);
- transform(hdc);
- FillRect(hdc, &rt, hSel);
- ReleaseDC(hWnd, hdc);
- SetCapture(hWnd);
- return 0;}}
- break;
- case WM_LBUTTONUP:
- if(capture)
- {
- ReleaseCapture();
- capture=false;
- }
- break;
- case WM_MOUSEMOVE:
- if (capture)
- {
- point.x=LOWORD(lParam);
- point.y=HIWORD(lParam);
- DcInLp(point);
- pt[index]=point;
- InvalidateRect(hWnd, NULL, TRUE);
- }
- break;
- case WM_PAINT:
- hdc=BeginPaint(hWnd, &ps);
- transform(hdc);
- SelectObject(hdc, hDash);
- Polyline(hdc, pt, count);
- SelectObject(hdc, hBezier);
- PolyBezier(hdc, pt, count);
- for(i=0; i<count; i++){
- SetRect(&rt, pt[i].x-MARK, pt[i].y-MARK, pt[i].x+MARK, pt[i].y+MARK);
- FillRect(hdc, &rt, hRect);
- }
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY: PostQuitMessage(0);
- DeleteObject(hDash);
- DeleteObject(hBezier);
- DeleteObject(hRect);
- DeleteObject(hSel);
- out.open("dat.txt");
- for (i=0; i<count; i++) out << pt[i].x << '\t' << pt[i].y << '\n';
- out.close();
- break;
- default: return DefWindowProc(hWnd, message, wParam, lParam);
- } return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement