# Thread: Is there an easiest way to rotate a line?

1. ## Is there an easiest way to rotate a line?

Here's my attempt! but doesn't work, freezes at most and no draws solid lines (no rotation)

Code:
```case WM_PAINT:
while (cntr++ < 180)
{
DrawLine(hdc, ptCenter, SetPOINT(ptCenter, radius), 255, 128, 128);
Sleep(2000);
}
break;

.........

void DrawLine(HDC hdc, POINT ptCenter, POINT coord, int R, int G, int B)
{
HPEN pen, oldPen;

pen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
oldPen = (HPEN)SelectObject(hdc, pen);

MoveToEx(hdc, ptCenter.x,ptCenter.y, NULL);
LineTo(hdc, coord.x, coord.y);

SelectObject(hdc, oldPen);
DeleteDC(hdc);
}

/* Calculate the line points */
POINT SetPOINT(POINT ptCenter, LONG radius)
{
const float Deg2Rad = 0.017453292;
POINT coord;
static int angle=0;

coord.x = ptCenter.x + (cos(degInRad)*(float)radius); // Finds the adjacent value
coord.y = ptCenter.x - (sin(degInRad)*(float)radius);	// Finds the hypotenuse value
angle-=5;            // Prepare for the next line position (clockwise direction)

return coord;
}```

2. The value in 'hdc' will not be valid, it will have lost scope between each message flowing through the callback.
The invalidated area (which generates a WM_PAINT) will not be validated as you do not call BeginPaint() and EndPaint().

Is 'cntr' a static? If not it will have also lost scope and may not contain the value you expect.

You want to DeleteObject() on the PEN you create not the HDC. Better/much faster to create the PEN once (before the loop), use it while the loop executes and then clean up after the loop.

Try something more like....
Code:
```PAINTSTRUCT    PS;
ZeroMemory(PS,sizeof(PAINTSTRUCT));
hdc=BeginPaint(&PS);
.
.
.
.
//clean up and validate paint area
EndPaint();```

3. Originally Posted by novacain
Is 'cntr' a static? If not it will have also lost scope and may not contain the value you expect.
Regarless of where cntr is declared? would this be valid..
Code:
`for(static int cntr=0;cntr<180;cntr++)`
ZeroMemory(PS,sizeof(PAINTSTRUCT));
error C2664: 'memset' : cannot convert parameter 1 from 'PAINTSTRUCT' to 'void *'

Code:
```void ZeroMemory(
PVOID Destination, // This expects void pointer, ps doesn't seem to work
SIZE_T Length
);```
Why should i use ZeroMemory?

4. ps is a struct, so you need the address of operator (&) to make it a pointer to the struct, which is what ZeroMemory uses.

Code:
`ZeroMemory(PS,sizeof(PAINTSTRUCT));`

Code:
`for(static int cntr=0;cntr<180;cntr++)`
That is completely unnecessary, as a for-loop is never leaving (as in returning from) the function it is declared in whilst still in the loop, so static here makes absolutely no sense, although I believe it's valid.

--
Mats

5. Won't the static variable only allow the loop to execute once? Unless the function changes cntr back to 0 at the end. Because once the loop executes the first the the static is initialized, so it will not initialize again. Or am I completely wrong?

6. Originally Posted by Raigne
Won't the static variable only allow the loop to execute once? Unless the function changes cntr back to 0 at the end. Because once the loop executes the first the the static is initialized, so it will not initialize again. Or am I completely wrong?
No, the for-loop will execute the initialization step every time.

--
Mats

7. [QUOTE=matsp;745011]ps is a struct, so you need the address of operator (&) to make it a pointer to the struct, which is what ZeroMemory uses.

Code:
`ZeroMemory(PS,sizeof(PAINTSTRUCT));`
was hoping for an example!!

8. [QUOTE=csonx_p;745047]
Originally Posted by matsp
ps is a struct, so you need the address of operator (&) to make it a pointer to the struct, which is what ZeroMemory uses.

Code:
`ZeroMemory(PS,sizeof(PAINTSTRUCT));`
was hoping for an example!!
Yes, it was meant to be - typo - sorry about that:
Code:
`ZeroMemory(&PS,sizeof(PAINTSTRUCT));`
Although I think I explained what was wrong - you are passing a struct, you need a pointer.

--
Mats

9. [QUOTE=matsp;745049]
Originally Posted by csonx_p

Yes, it was meant to be - typo - sorry about that:
Code:
`ZeroMemory(&PS,sizeof(PAINTSTRUCT));`
Although I think I explained what was wrong - you are passing a struct, you need a pointer.

--
Mats
My algorithm leaves lines drawn which defiles my aim of rotating a line. Is there a way i could erase a previous line before drawing a new on on new coordinates?

10. Well, you can do one of two things:
1. "undraw" the previous line by remembering where it used to be drawn (this assumes background is known, since you need to draw that part of the background).
2. Draw the entire surface with whatever was there before the line was drawn.

--
Mats

11. Originally Posted by matsp
Well, you can do one of two things:
1. "undraw" the previous line by remembering where it used to be drawn (this assumes background is known, since you need to draw that part of the background).
2. Draw the entire surface with whatever was there before the line was drawn.

--
Mats
Both options sounds a little uneasy to me... Is there a function to undraw?

12. Originally Posted by csonx_p
Both options sounds a little uneasy to me... Is there a function to undraw?
No, that would require for the "draw" to save what it's covering - which by the way is another way to do the same thing - if you plan to draw and then undraw a region, then copy the region covered by your drawing [easier if you make it a rectangle covering the extremes of your drawn object], and to "undraw", just draw back what you previously drew.

If your surface is solid background colour, you could perhaps use "InvalidateRect(..., ..., TRUE);" to erase the background.

--
Mats

13. Originally Posted by matsp
then copy the region covered by your drawing [easier if you make it a rectangle covering the extremes of your drawn object], and to "undraw", just draw back what you previously drew.
Mats
I'm sorry for being dumb, how do you copy a window region?

14. Originally Posted by csonx_p
I'm sorry for being dumb, how do you copy a window region?
BitBlt(), using ROP SRCCOPY, perhaps?

--
Mats

15. Originally Posted by matsp
BitBlt(), using ROP SRCCOPY, perhaps?

--
Mats
Thanx Mats, will try them.. If you don't mind please look at my SetTimer thread!

Popular pages Recent additions