# Resizing a triangle. Why is my code not working?

• 01-20-2007
gozu
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.

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; }```
• 01-20-2007
Ken Fitlike
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.
• 01-20-2007
gozu
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.