You are on page 1of 8

#include <windows.

h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include "resource.h"
#include "math.h"

static TCHAR szWindowClass[] = _T("DesktopApp");


static TCHAR szTitle[] = _T("Programarea Pilotata de Evenimente lab_3 Vrabie Aliona");
POINT apt[4]; // definim variabila globala
RECT r;
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void InitBezierParams(); // Initializam punctele pentru Bezier
int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_
LPSTR lpCmdLine, _In_ int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(hInst, L"Icon.ico");
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
hInst = hInstance;
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW, 500, 500, 500, 500,
//CW_USEDEFAULT, CW_USEDEFAULT,
//CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
InitBezierParams(); // Chemam functia care initializeaza pentru prima data punctele
curbei Bezier
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int)msg.wParam;
}
// functia care initializeaza punctele
void InitBezierParams()
{

apt[0].x = 220; apt[0].y = 110;


apt[1].x = 260; apt[1].y = 20;
apt[2].x = 360; apt[2].y = 190;
apt[3].x = 420; apt[3].y = 110;
}
void RotateRight(POINT pb_Key[]) {
POINT o = pb_Key[3]; //pnt de origine

for (int i = 0; i < 3; i++)


{
int temp = pb_Key[i].x;
pb_Key[i].x = (pb_Key[i].y - o.y) + o.x;
pb_Key[i].y = -1 * (temp - o.x) + o.y;

}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("Curba Besier prin functii GDI");
TCHAR greeting_1[] = _T("Curba Besier, prin formule matematice");
bool fDraw = FALSE;
POINT ptPrevious = { 0,0 };

switch (message)
{

case WM_KEYDOWN:

//deplasarea punctului de control


if (GetAsyncKeyState(VK_CONTROL))
{
switch (wParam)
{
case VK_LEFT:
apt[2].x -= 10;
break;
case VK_RIGHT:
apt[2].x += 6;
break;
case VK_UP:
apt[2].y -= 10;
break;
case VK_DOWN:
apt[2].y += 6;
break;
}
}
else
{
if (GetAsyncKeyState(VK_SPACE))
{
switch (wParam)
{
case VK_LEFT:
{
for (size_t i = 0; i < 4; i++)
{
apt[i].x--;
}
}
break;
case VK_RIGHT:
{
for (size_t i = 0; i < 4; i++)
{
apt[i].x++;
}
}
break;
case VK_UP:
{
for (size_t i = 0; i < 4; i++)
{
apt[i].y--;
}
}
break;
case VK_DOWN:
{
for (size_t i = 0; i < 4; i++)
{
apt[i].y++;
}
}
break;
}
}
else
{
if (GetAsyncKeyState(VK_SHIFT)) {
switch (wParam)
{
case VK_LEFT:
GetClientRect(hWnd, &r);
apt[0].x = r.right / 4; apt[0].y = r.bottom / 2;
apt[1].x = r.right / 2; apt[1].y = r.bottom / 4;
apt[2].x = r.right / 2; apt[2].y = 3 * r.bottom / 4;
apt[3].x = 3 * r.right / 4; apt[3].y = r.bottom / 2;
break;
case VK_RIGHT:
GetClientRect(hWnd, &r);
apt[0].x = r.right / 2; apt[0].y = r.bottom / 4;
apt[1].x = 3 * r.right / 4; apt[1].y = r.bottom / 2;
apt[2].x = r.right / 4; apt[2].y = r.bottom / 2;
apt[3].x = r.right / 2; apt[3].y = 3 * r.bottom / 4;
break;

}
}
else {
switch (wParam)
{
case VK_LEFT:
apt[1].x -= 10;
break;
case VK_RIGHT:
apt[1].x += 6;
break;
case VK_UP:
apt[1].y -= 10;
break;
case VK_DOWN:
apt[1].y += 6;
break;
}
}
}
}
InvalidateRect(hWnd, NULL, TRUE);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
SetTextColor(hdc, RGB(0, 30, 70));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, 20, 20, greeting, _tcslen(greeting));
TextOut(hdc, 20, 150, greeting_1, _tcslen(greeting_1));

{//desenare curba bezier dupa formula


int x[4] = { 30, 80, 260, 320 };
int y[4] = { 310, 50, 290, 210 };
double xu, yu;
for (double u = 0.0; u <= 1.0; u += 0.0001)
{
xu = (pow(1 - u, 3) * x[0] + 3 * u * pow(1 - u, 2) * x[1] + 3 * pow(u, 2)
* (1 - u) * x[2] + pow(u, 3) * x[3]);
yu = pow(1 - u, 3) * y[0] + 3 * u * pow(1 - u, 2) * y[1] + 3 * pow(u, 2)
* (1 - u) * y[2] + pow(u, 3) * y[3];
SetPixel(hdc, (int)xu, (int)yu, RGB(25, 0, 235));
}
}
// Desenarea curbei prin funtie GDI
//InvalidateRect(hWnd, NULL, TRUE);
PolyBezier(hdc, apt, 4);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}

You might also like