Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <tchar.h>
- #include <math.h>
- #include <fstream>
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- TCHAR WinName[] = _T("MainFrame");
- static int sx,sy;
- const int SCALE = 1000;
- const int MARK = 4;
- int n,i,j,first;
- 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;
- void DcInLp (POINT &point)
- {
- 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,sy,NULL);
- SetViewportOrgEx(hdc,0,sy,NULL);
- }
- 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("B-spline"),
- 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)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- static HBRUSH hRect,hSel;
- static HPEN hDash, hBezier;
- 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("Файл не найден"),_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(255,0,0));
- hRect = CreateSolidBrush (RGB(128,0,128));
- hSel = CreateSolidBrush (RGB(255,0,0));
- break;
- case WM_SIZE:
- sx = LOWORD(lParam);
- sy = HIWORD(lParam);
- 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);
- point.x = LOWORD(lParam);
- point.y = HIWORD(lParam);
- DcInLp(point);
- for (i = 0; i <= count; i++)
- {
- MoveToEx(hdc, pt[i].x-eps, pt[i].y-eps, NULL);
- LineTo(hdc, pt[i].x+eps,pt[i].y+eps);
- MoveToEx(hdc, pt[i].x+eps, pt[i].y-eps, NULL);
- LineTo(hdc, pt[i].x-eps,pt[i].y+eps);
- }
- first = 1;
- for (i=1;i<count-1;i++)
- { xA=pt[i-1].x;xB=pt[i].x;xC=pt[i+1].x;xD=pt[i+2].x;
- yA=pt[i-1].y;yB=pt[i].y;yC=pt[i+1].y;yD=pt[i+2].y;
- a3=(-xA+3*(xB-xC)+xD)/6; b3=(-yA+3*(yB-yC)+yD)/6;
- a2=(xA-2*xB+xC)/2; b2=(yA-2*yB+yC)/2;
- a1=(xC-xA)/2; b1=(yC-yA)/2;
- a0=(xA+4*xB+xC)/6; b0=(yA+4*yB+yC)/6;
- for (j=0;j<=count;j++)
- { t=(float)j/(float)count;
- X=((a3*t+a2)*t+a1)*t+a0; Y=((b3*t+b2)*t+b1)*t+b0;
- if (first)
- { first=0;
- MoveToEx(hdc,X,Y, NULL);
- }
- else LineTo(hdc,X,Y);
- } }
- EndPaint(hWnd,&ps);
- break;
- case WM_DESTROY:
- 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();
- PostQuitMessage(0);
- break;
- default: return DefWindowProc(hWnd, message, wParam, lParam);
- } return 0; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement