Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <windowsx.h>
- #include "Data.h"
- #include "Scene2D.h"
- LRESULT _stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
- int _stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- WNDCLASS wc;
- wc.style = CS_OWNDC;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
- wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(6);
- wc.lpszMenuName = 0;
- wc.lpszClassName = (LPCSTR)"MainWindowClass";
- RegisterClass(&wc);
- HWND hWnd = CreateWindow(
- (LPCSTR)"MainWindowClass",
- (LPCSTR)"VTORAYA BLYAT'",
- WS_OVERLAPPEDWINDOW,
- 200,200,800,800,
- nullptr,nullptr,hInstance,nullptr);
- ShowWindow(hWnd,nCmdShow);
- UpdateWindow(hWnd);
- MSG msg;
- while(GetMessage(&msg,nullptr,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- Scene2D scene(-3, 3, -2, 2);
- LRESULT _stdcall WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch(msg)
- {
- case WM_PAINT:
- {
- HDC dc = GetDC(hWnd);
- scene.Clear(hWnd, dc);
- scene.PlotAxe(dc);
- scene.ThirdMethodFuncPlot(dc);
- scene.PlotContinue(dc);
- ReleaseDC(hWnd, dc);
- return DefWindowProc(hWnd,msg,wParam,lParam);
- }
- case WM_SIZE:
- {
- HDC dc = GetDC(hWnd);
- scene.SetResolution(hWnd, dc);
- ReleaseDC(hWnd,dc);
- InvalidateRect(hWnd,nullptr,false);
- return 0;
- }
- case WM_LBUTTONDOWN:
- {
- scene.startDragging(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
- return 0;
- }
- case WM_MOUSEMOVE:
- {
- if (scene.IsDragging())
- {
- scene.dragging(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
- InvalidateRect(hWnd, nullptr, false);
- }
- return 0;
- }
- case WM_LBUTTONUP:
- {
- scene.StopDragging();
- return 0;
- }
- case WM_MOUSEWHEEL:
- {
- POINT P;
- P.x = GET_X_LPARAM(lParam);
- P.y = GET_Y_LPARAM(lParam);
- ScreenToClient(hWnd, &P);
- bool delta = (GET_WHEEL_DELTA_WPARAM(wParam) >= 0) ? true : false;
- scene.Zooming(delta, P.x, P.y);
- InvalidateRect(hWnd, nullptr, false);
- return 0;
- }
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- return 0;
- }
- default:
- {
- return DefWindowProc(hWnd, msg, wParam, lParam);
- }
- }
- return 0;
- }
- #ifndef SCENE_2D_H
- #define SCENE_2D_H
- #include "Camera2D.h"
- class Scene2D : public Camera2D {
- public:
- Scene2D(double L, double R, double B, double T) : Camera2D(L, R, B, T) {}
- void PlotAxe(HDC dc)
- {
- HPEN hPen;
- hPen = CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
- SelectObject(dc, hPen);
- Axes(dc);
- createCoords();
- }
- void FirstMethodFuncPlot(HDC dc)
- {
- createMassD();
- }
- void SecondMethodFuncPlot(HDC dc)
- {
- CreateMassD_();
- }
- void ThirdMethodFuncPlot(HDC dc)
- {
- CreateMassD__();
- }
- void PlotContinue(HDC dc)
- {
- HPEN uPen, sPen;
- uPen = CreatePen(PS_DOT, 2, RGB(255, 0, 0));
- sPen = CreatePen(PS_DOT, 2, RGB(0, 0, 255));
- SelectObject(dc, uPen);
- double u = (R - L) / W, // исходная функция
- x = L;
- MoveTo(x, expr);
- while (x <= R)
- {
- x += u;
- LineTo(dc, x, expr);
- }
- SelectObject(dc, sPen);
- double k = (R - L) / W, // многочлен
- t = L;
- MoveTo(t, newton(t));
- while (t <= R)
- {
- t += k;
- LineTo(dc, t, newton(t));
- }
- }
- };
- #endif SCENE_2D_H
- #ifndef CAMERA_2D_H
- #define CAMERA_2D_H
- #include <windows.h>
- class Camera2D
- {
- protected:
- double z;
- double oldl, oldr;
- double L, R, B, T;
- int W, H;
- int WorldToScreenX(double X)
- {
- return (int)((X - L) / (R - L)*W);
- }
- int WorldToScreenY(double Y)
- {
- return (int)((T - Y) / (T - B)*H);
- }
- double ScreenToWorldX(int X)
- {
- return L + (R - L)*((double)X + 0.5) / (double)W;
- }
- double ScreenToWorldY(int Y)
- {
- return T + (B - T)*((double)Y + 0.5) / (double)H;
- }
- private:
- bool isdragging;
- double dx, dy;
- int x_, y_;
- double posX, posY;;
- public:
- Camera2D(double L, double R, double B, double T) : L(L), R(R), B(B), T(T)
- {
- }
- void Clear(HWND hWnd, HDC dc)
- {
- RECT rect;
- GetClientRect(hWnd, &rect);
- FillRect(dc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
- }
- void SetResolution(HWND hwnd, HDC dc)
- {
- RECT rect;
- GetClientRect(WindowFromDC(dc), &rect);
- W = rect.right + 1;
- H = rect.bottom + 1;
- if (oldr == oldl == 0.0)
- {
- oldr = R; oldl = L;
- }
- else
- {
- L = (oldr + oldl) / 2 - (T - B) / 2 * W / H;
- R = (oldr + oldl) / 2 + (T - B) / 2 * W / H;
- }
- }
- void MoveTo(double X, double Y)
- {
- posX = X;
- posY = Y;
- }
- void LineTo(HDC dc, double X, double Y)
- {
- ::MoveToEx(dc, WorldToScreenX(posX), WorldToScreenY(posY), NULL);
- ::LineTo(dc, WorldToScreenX(X), WorldToScreenY(Y));
- MoveTo(X, Y);
- }
- void Axes(HDC dc)
- {
- MoveTo(L, 0);
- LineTo(dc, R, 0);
- MoveTo(0, B);
- LineTo(dc, 0, T);
- }
- void OtherShit(HDC dc)
- {
- }
- void startDragging(int x, int y)
- {
- isdragging = true;
- x_ = x;
- y_ = y;
- }
- void dragging(int x, int y)
- {
- dx = x - x_;
- dy = y - y_;
- L = L - (R - L)*dx / W;
- R = R - (R - L)*dx / W;
- B = B + (T - B)*dy / H;
- T = T + (T - B)*dy / H;
- x_ = x;
- y_ = y;
- }
- void StopDragging()
- {
- isdragging = false;
- }
- bool IsDragging()
- {
- return isdragging;
- }
- void Zooming(bool increase, int X, int Y)
- {
- double k = 1.0;
- X = ScreenToWorldX(X);
- Y = ScreenToWorldY(Y);
- if (increase)
- k = 1.1;
- else k = 0.9;
- L = X - (X - L) / k;
- R = X + (R - X) / k;
- B = Y - (Y - B) / k;
- T = Y + (T - Y) / k;
- }
- };
- #endif CAMERA_2D_H
- #define _USE_MATH_DEFINES
- #include <vector>
- #include <cmath>
- using namespace std;
- const int n = 6; // степень многочлена
- const int a = -2, b = 1; // границы интервала
- double h = static_cast<double>(b - a) / n; // приращение узлов интерполяции
- #define expr (1+x+x*x)/(1+x*x+x*x*x*x) // исходная функция
- double* xCoords = new double[n + 1], // создание массивов узлов интерполяции и значений выражения в них
- * yCoords = new double[n + 1];
- void createCoords() { // заполнение массивов
- int i = 0;
- for (double x = a; x <= b; x += h) {
- *(xCoords + i) = x;
- *(yCoords + i) = expr;
- i++;
- }
- }
- double* massD = new double[n + 1]; // массив коэфицентов d
- void createMassD() { // и тут заполнение
- *(massD + 0) = *(yCoords + 0); // d0 = f(x0)
- for (int j = 1; j < n; j++)
- for (int i = 0; i < n - j; i++) {
- *(yCoords + i) = (*(yCoords + i + 1) - *(yCoords + i)) / (*(xCoords + i + j) - *(xCoords + i)); // магия копипасты
- *(massD + j) = *(yCoords + 0);
- }
- }
- void CreateMassD_() { // и тут заполнение
- massD[0] = yCoords[0]; // d0 = f(x0)
- vector<vector<double> > vec(n + 1, vector<double>(n + 1, 0));
- int t = 1, k = n - 1;
- for (int i = 0; i <= n; i++)
- vec[0][i] = yCoords[i];
- for (int i = 1; i <= n; i++) {
- for (int j = 0; j <= k; j++)
- vec[i][j] = (vec[i - 1][j] - vec[i - 1][j + 1]) / (xCoords[j] - xCoords[j + t]);
- t++;
- k--;
- }
- for (int i = 1; i <= n; i++)
- massD[i] = vec[i][0];
- }
- void CreateMassD__() { // и тут заполнение
- double y = 1.0, u = 0.0;
- int e = 1;
- massD[0] = yCoords[0]; // d0 = f(x0)
- for (int m = 1; m <= n; m++) {
- for (int i = 0; i <= m; i++) {
- for (int j = 0; j <= m, j != i; j++)
- y *= 1 / (xCoords[i] - xCoords[j]);
- y *= yCoords[i];
- u += y;
- y = 1.0;
- }
- massD[e] = u;
- u = 0.0;
- e++;
- }
- }
- double newton(double x_) { // тут считаем значения Pn(x)
- double value = *(massD + 0), p = 1;
- for (int i = 1; i < n; i++) {
- p = p * (x_ - *(xCoords + i - 1));
- value += *(massD + i) * p;
- }
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement