Sunday, November 12, 2023

N-point Star in Microsoft Visual Studio Console App

#include <windows.h>
#include <cmath>
#include <iostream>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int main() {
    int numberOfPoints;
    std::cout << "Enter the number of points for the star: ";
    std::cin >> numberOfPoints;

    const wchar_t CLASS_NAME[] = L"StarWindowClass";

    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = GetModuleHandle(nullptr);
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0,
        CLASS_NAME,
        L"Star Window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        nullptr,
        nullptr,
        GetModuleHandle(nullptr),
        nullptr
    );

    if (hwnd == nullptr) {
        return 0;
    }

    SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(&numberOfPoints));

    ShowWindow(hwnd, SW_SHOWNORMAL);

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

    return 0;
}

void DrawStar(HDC hdc, int centerX, int centerY, int radius, int numPoints) {
    const double PI = 3.14159265358979323846;

    double angle = 2 * PI / numPoints;


    // Create a yellow pen for lines
    HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 255, 0));
    SelectObject(hdc, hPen);

    // Calculate the points of the star
    POINT points[1000]; // Assuming a maximum of 10 points for simplicity
    for (int i = 0; i < 1000; i++) {
        points[i].x = static_cast<int>(centerX + radius);
        points[i].y = static_cast<int>(centerY);
    }

    for (int i = 0; i < 3*numPoints; i+=3) {
        points[i].x = static_cast<int>(centerX + radius / 3. * cos(i * angle));
        points[i].y = static_cast<int>(centerY + radius / 3. * sin(i * angle));

        points[i+1].x = static_cast<int>(centerX + radius  * cos((i+0.5) * angle));
        points[i+1].y = static_cast<int>(centerY + radius  * sin((i+0.5) * angle));

        points[i + 2].x = static_cast<int>(centerX + radius/3. * cos((i + 1) * angle));
        points[i + 2].y = static_cast<int>(centerY + radius/3. * sin((i + 1) * angle));

    }

    // Draw the star outline
    Polygon(hdc, points, 3*numPoints);

    // Cleanup: delete created objects
    DeleteObject(hPen);
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        // Fill the background with blue color
        SetDCBrushColor(hdc, RGB(0, 0, 255));
        FillRect(hdc, &ps.rcPaint, (HBRUSH)GetStockObject(DC_BRUSH));

        int numberOfPoints = *reinterpret_cast<int*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));

        DrawStar(hdc, 400, 300, 100, numberOfPoints);

        EndPaint(hwnd, &ps);
    }
    return 0;

    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

No comments:

Post a Comment

N-point Star in Microsoft Visual Studio Console App

#include <windows.h> #include <cmath> #include <iostream> LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,...