Thread: Algorithm to roll a rectangle

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Algorithm to roll a rectangle

    I'm trying to draw and pivot a rectangle.
    How to get the new coordinates if I turn it 90 degree for example?

    From this:
    Algorithm to roll a rectangle-1-jpg

    To this:
    Algorithm to roll a rectangle-2-jpg

    This is not good because it will reduce it in size but this is just to give you an idea.

    Code:
     int size = 50;
    
        if(b) // draw rectangle
        {
            POINT points[4] =
               {{xCoord,      yCoord},
                {xCoord,      yCoord-size},
                {xCoord+size, yCoord-size},
                {xCoord+size, yCoord}
            };
            Polygon(hdc, points, 4);
        }
        else // turn rectangle
        {
            POINT points[4] =
               {{xCoord+(size/2),      yCoord-(size/2)},
                {xCoord+(size),        yCoord-size}, 
                {xCoord+size+(size/2), yCoord-size+(size/2)},
                {xCoord+size,          yCoord}
            };
            Polygon(hdc, points, 4);
        }
    Last edited by Ducky; 09-17-2014 at 08:18 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suspect you'd use something like a rotation matrix: Rotation matrix - Wikipedia, the free encyclopedia
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Take a look here too:

    Can someone help me to math?

    The code I posted there was from an analogue clock program, where the points are rotated around the window origin, which is set in the center of the client area.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks, yes I found that clock example from the Petzold book too, that is the answer to my question.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Ducky View Post
    Thanks, yes I found that clock example from the Petzold book too, that is the answer to my question.
    It seems as if you are needing to move the rectangle as well (for example it moves to the right when you roll it that way). I was wondering if there was a formula to determine the correct amount to move it by as well?

    The best I could think of was something like : rect.x += cos(a) * width . I thought that would move it over a width length as cos approaches 1, I haven't actually worked it out though. Let me know what you think.
    Last edited by Alpo; 09-18-2014 at 12:31 AM. Reason: Got tan and cos mixed up
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    I thought about adding the width to the rotating point every time the rectangle turns 180 degree. Seems to be easier.
    Using Windows 10 with Code Blocks and MingW.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Ducky View Post
    I thought about adding the width to the rotating point every time the rectangle turns 180 degree. Seems to be easier.
    Please state that you understand that you only rolled it 45 degrees in the posted image.
    Or, state you really did roll it 90 degrees in the posted image.

    I think it is 45 degrees and you had made a typo of 90; but, now you state 180 when I think 90 would be the value.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Quote Originally Posted by stahta01 View Post
    Please state that you understand that you only rolled it 45 degrees in the posted image.
    Or, state you really did roll it 90 degrees in the posted image.

    I think it is 45 degrees and you had made a typo of 90; but, now you state 180 when I think 90 would be the value.

    Tim S.
    The posted image is just an example to show the movement. But youre right it should be 90 degrees to make it roll on a plain surface.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dice Roll
    By seanksg in forum C Programming
    Replies: 4
    Last Post: 05-13-2011, 12:37 AM
  2. string roll
    By kryptkat in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2009, 09:51 PM
  3. Roll on 18 wheels roll on
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 05-13-2009, 09:12 PM
  4. Roll Up the Rim (Canadians)
    By @nthony in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 03-17-2009, 04:58 PM
  5. C++ dice roll need help
    By catdieselpow in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2007, 01:32 PM