Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <ctime>
- #include <vector>
- TCHAR CLNAME[] = TEXT("MojaKlasa");
- TCHAR APPNAME[] = TEXT("Szalone koło - F1 Help");
- struct figura
- {
- POINT wsp[3];
- COLORREF kolor_brush;
- COLORREF kolor_pen;
- int ksztalt;
- };
- COLORREF kolory[18] = {
- RGB(0, 64, 0), RGB(128, 0, 0), RGB(0, 128, 0),
- RGB(128, 128, 64), RGB(0, 0, 160), RGB(128, 0, 128),
- RGB(64, 128, 128), RGB(128, 128, 128), RGB(192, 192, 192),
- RGB(255, 0, 0), RGB(0, 255, 0), RGB(255, 255, 0),
- RGB(0, 0, 255), RGB(255, 0, 128), RGB(0, 255, 255),
- RGB(255, 255, 255), RGB(0, 0, 0), RGB(218, 218, 218)
- };
- // algorytm z neta http://www.geeksforgeeks.org/check-whether-a-given-point-lies-inside-a-triangle-or-not/ jedyna trudnosc w tym zadaniu imho.
- float area(int x1, int y1, int x2, int y2, int x3, int y3)
- {
- return abs((x1*(y2 - y3) + x2*(y3 - y1) + x3*(y1 - y2)) / 2.0);
- }
- bool isInside(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
- {
- float A = area(x1, y1, x2, y2, x3, y3);
- float A1 = area(x, y, x2, y2, x3, y3);
- float A2 = area(x1, y1, x, y, x3, y3);
- float A3 = area(x1, y1, x2, y2, x, y);
- return (A == A1 + A2 + A3);
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- srand(time(0));
- static RECT rect;
- static COLORREF kolor_brush[20];
- static COLORREF kolor_pen[20];
- static std::vector <figura> shape(20);
- static unsigned short int rand_ilosc = 0;
- static unsigned short int ilosc_figur = 0;
- static unsigned short int kwadrat = 0;
- static unsigned short int kolo = 0;
- static unsigned short int trojkat = 0;
- static COLORREF brush_color = NULL;
- static COLORREF pen_color = NULL;
- static int left[20], right[20], top[20], down[20];
- static HBRUSH brush = (HBRUSH)GetStockObject(DC_BRUSH);
- static HPEN pen = (HPEN)GetStockObject(DC_PEN);
- switch (msg) {
- case WM_CREATE: {
- HDC hdc = GetDC(hwnd);
- RECT r;
- GetClientRect(hwnd, &r);
- rand_ilosc = rand() % 10 + 10;
- ilosc_figur = rand_ilosc;
- int ilosc = rand_ilosc;
- kwadrat = rand() % ilosc;
- ilosc -= kwadrat;
- kolo = rand() % ilosc;
- ilosc -= kolo;
- trojkat = ilosc;
- for (unsigned int i = 0, r = rand() % 255 + 1, g = rand() % 255 + 1, b = rand() % 255 + 1; i <= rand_ilosc; i++, r = rand() % 255 + 1, g = rand() % 255 + 1, b = rand() % 255 + 1){
- kolor_brush[i] = RGB(r, g, b);
- }
- for (unsigned int i = 0, r = rand() % 255 + 1, g = rand() % 255 + 1, b = rand() % 255 + 1; i <= rand_ilosc; i++, r = rand() % 255 + 1, g = rand() % 255 + 1, b = rand() % 255 + 1) {
- kolor_pen[i] = RGB(r, g, b);
- }
- for (unsigned int i = 0,
- wspX_1 = rand() % r.right*0.75, wspY_1 = rand() % r.bottom, wspX_2 = rand() % r.right*0.75, wspY_2 = rand() % r.bottom, wspX_3 = rand() % r.right*0.75, wspY_3 = rand() % r.bottom;
- i < rand_ilosc; i++,
- wspX_1 = rand() % r.right*0.75, wspY_1 = rand() % r.bottom, wspX_2 = rand() % r.right*0.75, wspY_2 = rand() % r.bottom, wspX_3 = rand() % r.right*0.75, wspY_3 = rand() % r.bottom)
- {
- if (wspX_1 > wspX_2){
- int temp = wspX_1;
- wspX_1 = wspX_2;
- wspX_2 = temp;
- }
- if (wspY_1 > wspY_2){
- int temp = wspY_1;
- wspY_1 = wspY_2;
- wspY_2 = temp;
- }
- shape.push_back(figura());
- shape[i].wsp[0].x = wspX_1;
- shape[i].wsp[0].y = wspY_1;
- shape[i].wsp[1].x = wspX_2;
- shape[i].wsp[1].y = wspY_2;
- shape[i].wsp[2].x = wspX_3;
- shape[i].wsp[2].y = wspY_3;
- shape[i].kolor_brush = kolor_brush[i];
- shape[i].kolor_pen = kolor_pen[i];
- if (i < kwadrat) {
- shape[i].ksztalt = 1;
- }
- if (i >= kwadrat && i < kwadrat + kolo) {
- shape[i].ksztalt = 2;
- }
- if (i >= kwadrat + kolo && i < ilosc_figur) {
- shape[i].ksztalt = 3;
- }
- }
- ReleaseDC(hwnd, hdc);
- }break;
- case WM_LBUTTONDOWN: {
- int x = LOWORD(lParam);
- int y = HIWORD(lParam);
- HDC hdc = GetDC(hwnd);
- if (x<rect.right*0.75){
- if (brush_color != NULL || pen_color != NULL)
- {
- if (GetPixel(hdc, x, y) != RGB(254, 254, 254)) {
- COLORREF temp_kolor = GetPixel(hdc, x, y);
- for (int i = rand_ilosc; i >= 0; i--) {
- if (temp_kolor == shape[i].kolor_brush)
- {
- if (x >= shape[i].wsp[0].x && x <= shape[i].wsp[1].x && y >= shape[i].wsp[0].y && y <= shape[i].wsp[1].y && shape[i].ksztalt != 3) {
- shape[i].kolor_brush = brush_color;
- if (pen_color != NULL)
- shape[i].kolor_pen = pen_color;
- break;
- }
- if (shape[i].ksztalt == 3) {
- if (isInside(shape[i].wsp[0].x, shape[i].wsp[0].y, shape[i].wsp[1].x, shape[i].wsp[1].y, shape[i].wsp[2].x, shape[i].wsp[2].y, x, y))
- {
- shape[i].kolor_brush = brush_color;
- if (pen_color != NULL)
- shape[i].kolor_pen = pen_color;
- break;
- }
- }
- }
- }
- }
- }
- }
- else if (x>rect.right*0.75 && y<rect.bottom*0.70){
- if (GetPixel(hdc, x, y) != RGB(180, 180, 180)){
- brush_color = GetPixel(hdc, x, y);
- }
- }
- InvalidateRect(hwnd, &rect, TRUE);
- ReleaseDC(hwnd, hdc);
- }break;
- case WM_RBUTTONDOWN: {
- int x = LOWORD(lParam);
- int y = HIWORD(lParam);
- HDC hdc = GetDC(hwnd);
- if (x < rect.right*0.75) {
- if (GetPixel(hdc, x, y) != RGB(255, 255, 255)) {
- COLORREF temp_kolor = GetPixel(hdc, x, y);
- for (int i = rand_ilosc; i >= 0; i--) {
- if (shape[i].kolor_brush == temp_kolor) {
- if (shape[i].ksztalt != 3 && x >= shape[i].wsp[0].x && x <= shape[i].wsp[1].x && y >= shape[i].wsp[0].y && y <= shape[i].wsp[1].y) {
- ilosc_figur--;
- if (shape[i].ksztalt == 1) kwadrat--;
- if (shape[i].ksztalt == 2) kolo--;
- shape.erase(shape.begin() + i);
- break;
- }
- if (shape[i].ksztalt == 3) {
- if (isInside(shape[i].wsp[0].x, shape[i].wsp[0].y, shape[i].wsp[1].x, shape[i].wsp[1].y, shape[i].wsp[2].x, shape[i].wsp[2].y, x, y))
- {
- trojkat--;
- ilosc_figur--;
- shape.erase(shape.begin() + i);
- break;
- }
- }
- }
- }
- }
- }
- else if (x > rect.right*0.75 && y < rect.bottom*0.70) {
- if (GetPixel(hdc, x, y) != RGB(180, 180, 180)) {
- pen_color = GetPixel(hdc, x, y);
- }
- }
- InvalidateRect(hwnd, &rect, TRUE);
- ReleaseDC(hwnd, hdc);
- }break;
- case WM_PAINT: {
- PAINTSTRUCT ps;
- GetClientRect(hwnd, &rect);
- HDC hdc = BeginPaint(hwnd, &ps);
- SetDCBrushColor(hdc, RGB(254, 254, 254));
- FillRect(hdc, &rect, brush);
- SetDCBrushColor(hdc, RGB(180, 180, 180));
- SelectObject(hdc, brush);
- Rectangle(hdc, rect.right*0.75, rect.top, rect.right, rect.bottom);
- for (UINT i = 0, przerwa = 0; i <8; i++, przerwa += rect.bottom*0.08)
- {
- SetDCBrushColor(hdc, kolory[i]);
- SelectObject(hdc, brush);
- Rectangle(hdc, rect.right*0.8, (rect.bottom*0.001) + (rect.bottom*0.05) + przerwa, rect.right - (0.15*rect.right), (rect.bottom*0.12) + przerwa);
- }
- for (UINT i = 0, przerwa = 0; i < 8; i++, przerwa += rect.bottom*0.08)
- {
- SetDCBrushColor(hdc, kolory[i + 8]);
- SelectObject(hdc, brush);
- Rectangle(hdc, rect.right*0.9, (rect.bottom*0.001) + (rect.bottom*0.05) + przerwa, rect.right - (0.05*rect.right), (rect.bottom*0.12) + przerwa);
- }
- WCHAR text1[32];
- wsprintf(text1, TEXT("Ilość figur : %d"), ilosc_figur);
- SetBkColor(hdc, RGB(180, 180, 180));
- SetTextColor(hdc, RGB(0, 0, 0));
- TextOut(hdc, rect.right*0.80, rect.bottom*0.68, text1, wcslen(text1));
- wsprintf(text1, TEXT("Kwadrat : %d"), kwadrat);
- TextOut(hdc, rect.right*0.80, rect.bottom*0.71, text1, wcslen(text1));
- wsprintf(text1, TEXT("Elipsa : %d"), kolo);
- TextOut(hdc, rect.right*0.80, rect.bottom*0.74, text1, wcslen(text1));
- wsprintf(text1, TEXT("Trojkat : %d"), trojkat);
- TextOut(hdc, rect.right*0.80, rect.bottom*0.77, text1, wcslen(text1));
- TextOut(hdc, rect.right*0.80, rect.bottom*0.82, TEXT("Aktualny pędzel"), 20);
- SetDCBrushColor(hdc, brush_color);
- Rectangle(hdc, rect.right*0.80, rect.bottom*0.86, rect.right*0.95, rect.bottom*0.90);
- TextOut(hdc, rect.right*0.80, rect.bottom*0.91, TEXT("Akualny długopis"), 17);
- SetDCBrushColor(hdc, pen_color);
- Rectangle(hdc, rect.right*0.80, rect.bottom*0.95, rect.right*0.95, rect.bottom*0.99);
- for (short int i = 0; i <= rand_ilosc; i++)
- {
- SetDCBrushColor(hdc, shape[i].kolor_brush);
- SelectObject(hdc, brush);
- SetDCPenColor(hdc, shape[i].kolor_pen);
- SelectObject(hdc, pen);
- if (shape[i].ksztalt == 1)
- Rectangle(hdc, shape[i].wsp[0].x, shape[i].wsp[0].y, shape[i].wsp[1].x, shape[i].wsp[1].y);
- else if (shape[i].ksztalt == 2)
- Ellipse(hdc, shape[i].wsp[0].x, shape[i].wsp[0].y, shape[i].wsp[1].x, shape[i].wsp[1].y);
- else if (shape[i].ksztalt == 3)
- Polygon(hdc, shape[i].wsp, 3);
- }
- EndPaint(hwnd, &ps);
- }break;
- case WM_CLOSE: {
- DeleteObject(brush);
- DeleteObject(pen);
- DestroyWindow(hwnd);
- }break;
- case WM_DESTROY: {
- PostQuitMessage(0);
- }break;
- default: return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
- WNDCLASSEX wc;
- wc.cbClsExtra = 0;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.cbWndExtra = 0;
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hIcon = LoadIcon(0, IDI_APPLICATION);
- wc.hIconSm = LoadIcon(0, IDI_APPLICATION);
- wc.hInstance = hInstance;
- wc.lpfnWndProc = WndProc;
- wc.lpszClassName = CLNAME;
- wc.lpszMenuName = 0;
- wc.style = CS_VREDRAW | CS_HREDRAW;
- if (!RegisterClassEx(&wc)) {
- MessageBox(0, TEXT("Nie mogę rejestrować klasy!"), TEXT("ERROR"), MB_OK | MB_ICONERROR);
- return 1;
- }
- HWND hwnd = 0;
- hwnd = CreateWindowEx(0, CLNAME, APPNAME, WS_OVERLAPPED | WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, 0, 0, 800, 600, 0, 0, hInstance, 0);
- if (hwnd == NULL) {
- MessageBox(0, TEXT("Nie mogę utworzyć okna!"), TEXT("ERROR"), MB_OK | MB_ICONERROR);
- UnregisterClass(CLNAME, hInstance);
- return 1;
- }
- MSG msg;
- while (GetMessage(&msg, 0, 0, 0) > 0) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- UnregisterClass(CLNAME, hInstance);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement