Новичок
Джуниор
Регистрация: 14.03.2017
Сообщений: 1
|
Мировое окно, порт просмотра и функция отсечения
Добрый день!
Помогите, пожалуйста. Как переменной присвоить мировое окно и порт просмотра, а также как создать функцию отсечения отрезков? Помогите, пожалуйста
Код:
#define _USE_MATH_DEFINES
#include <Windows.h>
#include <WindowsX.h>
#include <CommCtrl.h>
#include <tchar.h>
#include <stdlib.h>
#include <gdiplus.h>
#include <strsafe.h>
#include <sstream>
#include <math.h>
#include <vector>
#pragma comment (lib, "Gdiplus.lib")
#pragma comment(lib, "secur32.lib")
typedef std::ostringstream SStre;
using namespace Gdiplus;
using namespace std;
#define IDC_BUTTON 2001
TCHAR szBuffer[100] = TEXT("");
TCHAR ProgDir[MAX_PATH];
LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void OnIdle(HWND hwnd);
BOOL PreTranslateMessage(LPMSG lpMsg);
BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);
void OnDestroy(HWND hwnd);
void OnSize(HWND hwnd, UINT state, int cx, int cy);
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
void Display(HDC hdc);
void Draw();
void VichX(float t);
void VichY(float t);
class WorldWindow
{
public:
float Left;
float Top;
float Right;
float Bottom;
inline WorldWindow(float left, float top, float right, float bottom)
: Left(left), Top(top), Right(right), Bottom(bottom)
{
}
inline float Width() const
{
return (Right - Left);
}
inline float Height() const
{
return (Top - Bottom);
}
};
class Viewport : public Gdiplus::Rect
{
public:
inline Viewport(int left, int top, int right, int bottom)
: Gdiplus::Rect(left, top, right - left, bottom - top)
{
}
inline Viewport(const RECT &rect)
: Gdiplus::Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top)
{
}
inline operator RECT() const
{
RECT rect = { X, Y, X + Width, Y + Height };
return rect;
}
};
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpszCmdLIne, int nCmdShow)
{
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = MyWindowProc;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT("Класс");
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (0 == RegisterClassEx(&wcex))
{
return -1;
}
HWND hWnd = CreateWindowEx(0, TEXT("Класс"), TEXT("Программа"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 1400, 700, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
LoadLibrary(TEXT("ComCtl32.dll"));
if (NULL == hWnd)
{
return -1;
}
ShowWindow(hWnd, nCmdShow);
MSG msg;
BOOL bRet;
for (;;)
{
while (!PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
OnIdle(hWnd);
}
bRet = GetMessage(&msg, NULL, 0, 0);
if (bRet == -1)
{
}
else if (FALSE == bRet)
{
break;
}
else if (PreTranslateMessage(&msg) == FALSE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int i;
switch (uMsg)
{
HANDLE_MSG(hWnd, WM_CREATE, OnCreate);
HANDLE_MSG(hWnd, WM_DESTROY, OnDestroy);
HANDLE_MSG(hWnd, WM_SIZE, OnSize);
HANDLE_MSG(hWnd, WM_COMMAND, OnCommand);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void OnIdle(HWND hwnd)
{
}
BOOL PreTranslateMessage(LPMSG lpMsg)
{
return false;
}
BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
{
CreateWindowEx(0, WC_BUTTON, TEXT("Создать"), WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 10, 128, 32, hwnd, (HMENU)IDC_BUTTON, lpCreateStruct->hInstance, NULL);
return TRUE;
}
void OnDestroy(HWND hwnd)
{
PostQuitMessage(0);
}
void OnSize(HWND hwnd, UINT state, int cx, int cy)
{
}
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
if (BN_CLICKED == codeNotify)
{
switch (id){
case IDC_BUTTON:
{
ULONG_PTR gdiplusToken;
GdiplusStartupInput gdiplusStartupInput;
Status stRet = GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
if (Ok == stRet)
{
HDC hdc = GetDC(hwnd);
Display(hdc);
GdiplusShutdown(gdiplusToken);
}
}
}
}
}
void Display(HDC hdc)
{
Graphics g(hdc);
g.SetSmoothingMode(SmoothingModeAntiAlias);
g.Clear(Color::White);
Draw();
}
inline void WorldToViewport(const WorldWindow &w, const Viewport &vp, Gdiplus::PointF *points, unsigned int count)
{
float A = vp.Width / (w.Right - w.Left);
float B = vp.Height / (w.Bottom - w.Top);
float C = vp.X - A*w.Left;
float D = vp.Y - B*w.Top;
for (unsigned int i = 0; i < count; ++i)
{
points[i].X = A*points[i].X + C;
points[i].Y = B*points[i].Y + D;
}
}
inline void ViewportToWorld(const WorldWindow &w, const Viewport &vp, Gdiplus::PointF *points, unsigned int count)
{
float A = (w.Right - w.Left) / (float)vp.Width;
float B = (w.Bottom - w.Top) / (float)vp.Height;
float C = w.Left - A*vp.X;
float D = w.Top - B*vp.Y;
for (unsigned int i = 0; i < count; ++i)
{
points[i].X = A*points[i].X + C;
points[i].Y = B*points[i].Y + D;
}
}
float x, y;
void Draw()
{
float tMin = 0, tMax = 18.85;
int n = 20;
vector<PointF> result;
Pen pen(Color::Black, 1.f);
WorldWindow w; <----так не работает
Viewport vp;<----так не работает
if (n > 1)
{
result.reserve(n);
float dt = (tMax - tMin) / (float)(n - 1);
for (size_t i = 0; i < n; ++i)
{
float t = tMin + i*dt;
VichX(t);
VichY(t);
result.push_back(PointF(x,y));
}
}
for (size_t i = 1; i < result.size(); ++i)
{
PointF points[2] = {result[i - 1], result[i] };
bool fIsDraw = LineClip(w, points[0], points[1]); <----как создать функцию
if (fIsDraw)
{
WorldToViewport(w, vp, points, 2);
g.DrawLines(pen, points, 2);
}
}
}
void VichX(float t)
{
x = 0;
x = -2.f*cos(t) + 3.f*cos(-2.f*t / 3.f);
}
void VichY(float t)
{
y = 0;
y = -2.f*sin(t) - 3.f*sin(-2.f*t / 3.f);
}
|