You are on page 1of 6

Ploaia Dan TI-181

#include <windows.h>

#include <math.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)

TCHAR szAppName[] = L"Bezier";

HWND hwnd;

MSG msg;

WNDCLASSEX wndclass;

wndclass.cbSize = sizeof(wndclass);

wndclass.style = CS_HREDRAW | CS_VREDRAW;

wndclass.lpfnWndProc = WndProc;

wndclass.cbClsExtra = 0;

wndclass.cbWndExtra = 0;

wndclass.hInstance = hInstance;

wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);

wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

wndclass.lpszMenuName = NULL;

wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

RegisterClassEx(&wndclass);

hwnd = CreateWindow(szAppName, L"Curbe Bezier",

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT,

CW_USEDEFAULT, CW_USEDEFAULT,

NULL, NULL, hInstance, NULL);

ShowWindow(hwnd, iCmdShow);

UpdateWindow(hwnd);

while (GetMessage(&msg, NULL, 0, 0))

TranslateMessage(&msg);

DispatchMessage(&msg);

return msg.wParam;

void DrawBezier(HDC hdc, POINT a[])

PolyBezier(hdc, a, 4);

MoveToEx(hdc, a[0].x, a[0].y, NULL);

LineTo(hdc, a[1].x, a[1].y);

MoveToEx(hdc, a[2].x, a[2].y, NULL);

LineTo(hdc, a[3].x, a[3].y);


}

double t;

double x, y;

void DrawBezier1(HDC hdc, POINT b[])

for (t = 0; t <= 1; t = t + 0.01)

x = pow((1 - t), 3) * b[0].x + 3 * t * pow((1 - t), 2) * b[1].x + 3 * pow(t, 2) * (1 - t) * b[2].x


+ pow(t, 3) * b[3].x;

y = pow((1 - t), 3) * b[0].y + 3 * t * pow((1 - t), 2) * b[1].y + 3 * pow(t, 2) * (1 - t) * b[2].y


+ pow(t, 3) * b[3].y;

if (t == 0)

MoveToEx(hdc, b[0].x, b[0].y, NULL);

LineTo(hdc, (int)x, (int)y);

MoveToEx(hdc, b[0].x, b[0].y, NULL);

LineTo(hdc, b[1].x, b[1].y);

MoveToEx(hdc, b[2].x, b[2].y, NULL);

LineTo(hdc, b[3].x, b[3].y);

}
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

static POINT a[4], b[4];

HDC hdc;

int cx, cy;

PAINTSTRUCT ps;

switch (iMsg)

case WM_SIZE:

cx = LOWORD(lParam);

cy = HIWORD(lParam);

a[0].x = cx / 4; b[0].x = cx / 4;

a[0].y = cy / 2; b[0].y = cy / 2 + 100;

a[1].x = cx / 2; b[1].x = cx / 2 + 50;

a[1].y = cy / 4; b[1].y = cy / 4 + 50;

a[2].x = cx / 2; b[2].x = cx / 2 + 50;

a[2].y = 3 * cy / 4; b[2].y = 3 * cy / 4 + 50;

a[3].x = 3 * cx / 4; b[3].x = 3 * cx / 4;

a[3].y = cy / 2; b[3].y = cy / 2 + 100;

return 0;
case WM_MOUSEMOVE:

if (wParam & MK_LBUTTON || wParam & MK_RBUTTON)

hdc = GetDC(hwnd);

SelectObject(hdc, GetStockObject(WHITE_PEN));

DrawBezier1(hdc, b);

SelectObject(hdc, GetStockObject(WHITE_PEN));

DrawBezier(hdc, a);

if (wParam & MK_LBUTTON)

a[1].x = LOWORD(lParam);

a[1].y = HIWORD(lParam);

b[1].x = LOWORD(lParam) + 50;

b[1].y = HIWORD(lParam) + 50;

if (wParam & MK_RBUTTON)

a[2].x = LOWORD(lParam);

a[2].y = HIWORD(lParam);

b[2].x = LOWORD(lParam) + 50;

b[2].y = HIWORD(lParam) + 50;

SelectObject(hdc, GetStockObject(BLACK_PEN));
DrawBezier1(hdc, b);

SelectObject(hdc, CreatePen(PS_SOLID, 1, RGB(255, 0, 0)));

DrawBezier(hdc, a);

ReleaseDC(hwnd, hdc);

return 0;

case WM_PAINT:

InvalidateRect(hwnd, NULL, TRUE);

hdc = BeginPaint(hwnd, &ps);

DrawBezier(hdc, a);

DrawBezier1(hdc, b);

EndPaint(hwnd, &ps);

return 0;

case WM_DESTROY:

PostQuitMessage(0);

return 0;

return DefWindowProc(hwnd, iMsg, wParam, lParam);

You might also like