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

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    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;
    	float degInRad;
    
    	degInRad = angle*Deg2Rad	   // Convert degrees to radians
    	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. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    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);
    //your code
    .
    .
    .
    .
    //clean up and validate paint area
    EndPaint();
    Last edited by novacain; 04-22-2008 at 11:45 PM. Reason: Point out not correct code [not got a IDE here]
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by novacain View Post
    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. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Raigne View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    [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. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    [QUOTE=csonx_p;745047]
    Quote Originally Posted by matsp View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    [QUOTE=matsp;745049]
    Quote Originally Posted by csonx_p View Post

    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. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    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. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    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. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    I'm sorry for being dumb, how do you copy a window region?
    BitBlt(), using ROP SRCCOPY, perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    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 subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  3. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM