Resizing a triangle. Why is my code not working?

This is a discussion on Resizing a triangle. Why is my code not working? within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I'm trying to create an equilateral triangle that can be resized and rotated at the press of a button. ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    14

    Resizing a triangle. Why is my code not working?

    Hello,

    I'm trying to create an equilateral triangle that can be resized and rotated at the press of a button. To do so, I define the coordinates of the 3 points in the polar coordinate system, such that their angles from the origin are separated by 120 degrees each and that their lenght is r. In theory, whenever r is incremented, the triangle does too while remaining equilateral and the same for when it is decremented.

    My code does produce an equilateral triangle but pressing PAGEUP/PAGEDOWN (which increments/decrements r ) does not seem to do anything. The same goes for rotating the triangle. I've used similar syntax to move the square using the arrow keys and change the colors of all objects with the space key so I'm stomped. I have no idea why it's not working.

    I took the liberty to comment out irrelevant code and to bold the relevant parts. Any help would be appreciated.

    Thank you for your time,

    Gozulin

    Code:
    #include <windows.h>
    #include <math.h>
    
    #define PI        3.14159265
    
    /* Square moves and changes colors */
    const char        g_szClassName[] = "myWindowClass";
    
    /*
     =======================================================================================================================
        Step 4: the Window Procedure
     =======================================================================================================================
     */
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
            /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
            static double	ang = 0;/* This is a displacement angle, when increased or decreased, it should rotate the triangle accordingly. */
            static int	r = 50;
            static int              red = 255;
            static int              green = 25;
            static int              blue = 5;
            /*
             * static int recsideH = 200;
             * static int recsideV = 200;
             * static int recdispH = 50;
             * static int recdispV = 50;
             * static int circsideH = 200;
             * static int circsideV = 200;
             * static int circdispH = 50;
             * static int circdispV = 50;
             * ;
             
             
    //Each of these points is "imagined" in polar coordinates such that it is on the circle of radius r
    //and the 3 points are separated by 120 degrees. These two conditions give us an equilateral triangle
    //easily manipulated by changing the radius (increment or decrement r) and the angles of each point.
    //I then convert their coordinates to cartesian using the X = cos(theta)*r . I multiply by PI/180 to
    //obtain the angle in radians since that's what the cos() function takes as argument. I add
    //300 to all coordinates to displace the triangle to the middle of the screen.*/
    
            static int                X1 = 300 + cos((ang + 270) * PI / 180) * r;
            static int                Y1 = 300 + sin((ang + 270) * PI / 180) * r;
            static int                X2 = 300 + cos((ang + 30) * PI / 180) * r;
            static int                Y2 = 300 + sin((ang + 30) * PI / 180) * r;
            static int                X3 = 300 + cos((ang + 150) * PI / 180) * r;
            static int                Y3 = 300 + sin((ang + 150) * PI / 180) * r;
            LPPOINT                        dumb;
            POINT                        point;
            dumb = &point;
    
            switch(msg)
            {
            case WM_PAINT:
                    HDC hDC;
    
                    PAINTSTRUCT Ps;
                    HPEN                hPen;
                    hDC = BeginPaint(hwnd, &Ps);
                    hPen = CreatePen(PS_SOLID, 2, RGB(red, green, blue));
                    SelectObject(hDC, hPen);
                    //Rectangle(hDC, recsideH, recsideV, recdispH, recdispV);
                    //Ellipse(hDC, circsideH + 500, circsideV, circdispH + 500, circdispV);
                    MoveToEx(hDC, X1, Y1, dumb);
                    LineTo(hDC, X2, Y2);
                    LineTo(hDC, X3, Y3);
                    LineTo(hDC, X1, Y1);
    
    
                    DeleteObject(hPen);
                    EndPaint(hwnd, &Ps);
                    break;
    
            case WM_KEYDOWN:
                    /*if((char) wParam == VK_UP && recdispV > 5)
                    {
                            recdispV -= 5;
                            recsideV -= 5;
                    }
                    else if((char) wParam == VK_DOWN && recdispV < 410)
                    {
                            recdispV += 5;
                            recsideV += 5;
                    }
                    else if((char) wParam == VK_LEFT && recdispH > 5)
                    {
                            recdispH -= 5;
                            recsideH -= 5;
                    }
                    else if((char) wParam == VK_RIGHT && recdispH < 635)
                    {
                            recdispH += 5;
                            recsideH += 5;
                    }*/
    
                    if((char) wParam == VK_PRIOR)
                    {
                            r -= 5;
                    }
    
                    if((char) wParam == VK_NEXT)
                    {
                            r -= 5;
                    }
    
                    InvalidateRect(hwnd, NULL, TRUE);
                    break;
    
            case WM_CHAR:
                    if((char) wParam == 'g') MessageBox(hwnd, "You pressed a 'g'!", "WM_CHAR", MB_OK | MB_ICONINFORMATION);
                    if((char) wParam == ' ')
                    {
                            red = rand() % 256;
                            green = rand() % 256;
                            blue = rand() % 256;
                            InvalidateRect(hwnd, NULL, TRUE);
                    }
                    break;
    
            case WM_CLOSE:
                    DestroyWindow(hwnd);
                    break;
    
            case WM_DESTROY:
                    PostQuitMessage(0);
                    break;
    
            default:
                    return DefWindowProc(hwnd, msg, wParam, lParam);
            }
    
            return 0;
    }
    
    /*
     =======================================================================================================================
        FIXME:
     =======================================================================================================================
     */
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
            WNDCLASSEX        wc;
            HWND                hwnd;
            MSG                        Msg;
    
            /* Step 1: Registering the Window Class */
            wc.cbSize = sizeof(WNDCLASSEX);
            wc.style = 0;
            wc.lpfnWndProc = WndProc;
            wc.cbClsExtra = 0;
            wc.cbWndExtra = 0;
            wc.hInstance = hInstance;
            wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
            wc.hCursor = LoadCursor(NULL, IDC_ARROW);
            wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
            wc.lpszMenuName = NULL;
            wc.lpszClassName = g_szClassName;
            wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
            if(!RegisterClassEx(&wc))
            {
                    MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
                    return 0;
            }
    
            /* Step 2: Creating the Window */
            hwnd = CreateWindowEx
                    (
                            WS_EX_CLIENTEDGE,
                            g_szClassName,
                            "The title of my window",
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            800,
                            600,
                            NULL,
                            NULL,
                            hInstance,
                            NULL
                    );
    
            if(hwnd == NULL)
            {
                    MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
                    return 0;
            }
    
            ShowWindow(hwnd, nCmdShow);
            UpdateWindow(hwnd);
    
            /* Step 3: The Message Loop */
            while(GetMessage(&Msg, NULL, 0, 0) > 0)
            {
                    TranslateMessage(&Msg);
                    DispatchMessage(&Msg);
            }
    
            return (int) Msg.wParam;
    }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Assuming that 'r-=5' is a typo for one of the active WM_KEYDOWN conditions, it probably has something to do with where the calculations(X1,Y1 etc.) are done. Follow the example of how you set the colours before invalidating the window ie. shift the triangle vertex calculations to the WM_KEYDOWN handler, just before the call to InvalidateRect.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    14
    Thank you! That fixed it. My code was good afterall, just not in the right place.

    Really, thank you for taking the time to help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-24-2009, 07:49 PM
  2. Help Code Is Not Working
    By srivatsan in forum C++ Programming
    Replies: 23
    Last Post: 11-12-2008, 11:30 AM
  3. Replies: 8
    Last Post: 01-18-2008, 03:06 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21