Results 1 to 5 of 5

Thread: [RESOLVED] Rotate Rectangle positioning maths

  1. #1

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Resolved [RESOLVED] Rotate Rectangle positioning maths

    Hello, I was hoping to solve this one all by myself but I am stumped. Made loads of attempts at changing the code and cant work out for the life of me how to do it (Probably really simple)


    I am rotating a rectangle to draw to the screen. At the moment I want to it rotate around its center so it spins on the spot. I have the code to rotate the rectangle I just cant center, it rotates around its top left corner

    Code:
    	
    /*StartX, StartY, Width and Height are passed in as the grid coords for the start of the rectangle and its size. I want to rotate it around its StartX + (Width / 2) as this will be the center of the drawn rectangle
    */
    int x;
    int y;
    int NewX;
    int NewY;
    
    for (x=0; x<Width; x++)
       {
       for (y=0; y < Height;y++)
       {
    	
                //Rotate Points
    	NewX = (x * cos(Angle)) - (y * sin(Angle));
    	NewY = (x * sin(Angle)) +  (y * cos(Angle));
    				
    				
    	//Write to buffer
    	GRIFFIN_DrawPixel(NewX + StartX, NewY + StartY, Color);
        }	            
    }
    Anyone know what I am doing wrong

    Thanks for looking
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Rotate Rectangle positioning maths

    Have you tried looping for x between -Width/2 and Width/2, and similarly for y between -Height/2 and Height/2?
    Last edited by jemidiah; Apr 29th, 2009 at 12:38 AM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Re: Rotate Rectangle positioning maths

    Woohooo thank you. You dont know how many times I have comiled and tested that code to no avail. In fact I think the only code I didnt change was the for loops lol

    Thankyou )
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [RESOLVED] Rotate Rectangle positioning maths

    lol, no problem, glad it's working so quickly
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  5. #5
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Rotate Rectangle positioning maths

    The general recipe for rotating any polygon around a specific point is to move the coordinate system origin to this point, do the rotation and then restore the coordinate origin.
    Namely, you first subtract the coordinates of the point from the coordinates of the polygon vertices, apply the rotation and add the point coordinates to those of the resulting vertices.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width