Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. rectangle rotation

rectangle rotation

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 3375334
    wrote on last edited by
    #1

    Hello, please help me to write a function rotate that will rotate the rectangle 45 degrees each time button is pressed let say that rectangle values are as follows rectangle[0].x=250.0; rectangle[0].y=100.0; rectangle[1].x=550.0; rectangle[1].y=100.0; rectangle[2].x=550.0; rectangle[2].y=300.0; rectangle[3].x=250.0; rectangle[3].y=300.0; i know that it should be something like H2 = H·cosθ + W·sinθ W2 = H·sinθ + W·cosθ where H2 is the height of the rotated rectangles and W2 is the width. i need an assistance in writing this function.. please help

    S I 2 Replies Last reply
    0
    • M Member 3375334

      Hello, please help me to write a function rotate that will rotate the rectangle 45 degrees each time button is pressed let say that rectangle values are as follows rectangle[0].x=250.0; rectangle[0].y=100.0; rectangle[1].x=550.0; rectangle[1].y=100.0; rectangle[2].x=550.0; rectangle[2].y=300.0; rectangle[3].x=250.0; rectangle[3].y=300.0; i know that it should be something like H2 = H·cosθ + W·sinθ W2 = H·sinθ + W·cosθ where H2 is the height of the rotated rectangles and W2 is the width. i need an assistance in writing this function.. please help

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      1. Decide what point is to be the centre of rotation. Let's call it O, with co-ordinates (Xo, Yo). 2. Rotate each point of the rectangle, Pi, with co-ordinates (Xi, Yi), around O, producing a new point, Pi', with co-ordinates (Xi', Yi'). You do this using the following two identities:

      Xi' = (Xi-Xo)cosθ - (Yi-Yo)sinθ
      Yi' = (Xi-Xo)sinθ + (Yi-Yo)cosθ

      You now have your rotated rectangle! This page[^] may help.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      M 1 Reply Last reply
      0
      • S Stuart Dootson

        1. Decide what point is to be the centre of rotation. Let's call it O, with co-ordinates (Xo, Yo). 2. Rotate each point of the rectangle, Pi, with co-ordinates (Xi, Yi), around O, producing a new point, Pi', with co-ordinates (Xi', Yi'). You do this using the following two identities:

        Xi' = (Xi-Xo)cosθ - (Yi-Yo)sinθ
        Yi' = (Xi-Xo)sinθ + (Yi-Yo)cosθ

        You now have your rotated rectangle! This page[^] may help.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        M Offline
        M Offline
        Member 3375334
        wrote on last edited by
        #3

        umm what i have in my code is something like: px = (rectwidth/2); py = (rectheight/2); so i assume that they represent my center through which i want to rotate my rectangle (px, py) however, i am not sure how to represent all of the point of the rect to be Xi Yi considering that my rectangle representaion is as mentioned in the previous post with co-ordinates of (250, 100) is rectangle[0] (550, 100) is rectangle[1] (550, 300) is rectangle[2] (250, 300) is rectangle[3] i am new to C++ so i am having trouble seeing how your equasion can be programmed. i do understand the equasions u've provided though. Please any suggestions on the programming? like:

        void rotate (float angle)
        {
        ? = (? - px)cos(angle) - (? - py)sin(angle);
        ? = (? - px)sin(angle) + (? - py)cos(angle);
        }

        not sure what should be in there.

        S 1 Reply Last reply
        0
        • M Member 3375334

          umm what i have in my code is something like: px = (rectwidth/2); py = (rectheight/2); so i assume that they represent my center through which i want to rotate my rectangle (px, py) however, i am not sure how to represent all of the point of the rect to be Xi Yi considering that my rectangle representaion is as mentioned in the previous post with co-ordinates of (250, 100) is rectangle[0] (550, 100) is rectangle[1] (550, 300) is rectangle[2] (250, 300) is rectangle[3] i am new to C++ so i am having trouble seeing how your equasion can be programmed. i do understand the equasions u've provided though. Please any suggestions on the programming? like:

          void rotate (float angle)
          {
          ? = (? - px)cos(angle) - (? - py)sin(angle);
          ? = (? - px)sin(angle) + (? - py)cos(angle);
          }

          not sure what should be in there.

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          OK - for 'Pi', read 'rectangle[i]', for i in the range 0 to 3. So, for rectangle, assuming you're transforming rectangle into a new rectangle, rectangleNew:

          void rotate (float angle)
          {
          rectangleNew[0].x = (rectangle[0].x - px) * cos(angle) - (rectangle[0].y - py) * sin(angle);
          rectangleNew[0].y = (rectangle[0].x - px) * sin(angle) + (rectangle[0].y - py) * cos(angle);
          rectangleNew[1].x = (rectangle[1].x - px) * cos(angle) - (rectangle[1].y - py) * sin(angle);
          rectangleNew[1].y = (rectangle[1].x - px) * sin(angle) + (rectangle[1].y - py) * cos(angle);
          rectangleNew[2].x = (rectangle[2].x - px) * cos(angle) - (rectangle[2].y - py) * sin(angle);
          rectangleNew[2].y = (rectangle[2].x - px) * sin(angle) + (rectangle[2].y - py) * cos(angle);
          rectangleNew[3].x = (rectangle[3].x - px) * cos(angle) - (rectangle[3].y - py) * sin(angle);
          rectangleNew[3].y = (rectangle[3].x - px) * sin(angle) + (rectangle[3].y - py) * cos(angle);
          }

          Don't try and rotate points in-place...

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • M Member 3375334

            Hello, please help me to write a function rotate that will rotate the rectangle 45 degrees each time button is pressed let say that rectangle values are as follows rectangle[0].x=250.0; rectangle[0].y=100.0; rectangle[1].x=550.0; rectangle[1].y=100.0; rectangle[2].x=550.0; rectangle[2].y=300.0; rectangle[3].x=250.0; rectangle[3].y=300.0; i know that it should be something like H2 = H·cosθ + W·sinθ W2 = H·sinθ + W·cosθ where H2 is the height of the rotated rectangles and W2 is the width. i need an assistance in writing this function.. please help

            I Offline
            I Offline
            Iain Clarke Warrior Programmer
            wrote on last edited by
            #5

            If you want to see a VERY well written (I'm biased) article which has a lot of transformation work with matrices, look at: Warping Coordinates with Matrices[^] I like to think my diagrams will help you also. Iain.

            In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups