Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the angle from a QTransform variable?

How to get the angle from a QTransform variable?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 2.0k Views 2 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.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by
    #1

    Hi,

    I am working with QGraphicsItem objects and I need to get the angle from one item to set it to another one. In the context of my requirement, I can't get it directly from the QGraphicsItem variable (using the rotation() method) but from a QTransform object that I stored previously.

    Now, is it possible to extract the angle value from a QTransform variable?

    Thanks! :)


    Qt Developer

    kshegunovK 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      I don't think you can disentangle the rotation element from the other operations you performed. So, unless all you did to your QTransform is apply a rotation then it's impossible to calculate (some math genius might point out I'm wrong here btw)

      The math-illiterate answer is transform.map(QLineF(0.0,0.0,1.0,0.0)).angle();

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • X xtingray

        Hi,

        I am working with QGraphicsItem objects and I need to get the angle from one item to set it to another one. In the context of my requirement, I can't get it directly from the QGraphicsItem variable (using the rotation() method) but from a QTransform object that I stored previously.

        Now, is it possible to extract the angle value from a QTransform variable?

        Thanks! :)

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        Depends on what you put in your transform. Did you do any scaling, reflection, translation? If you did not, then:

        qreal angle = std::acos(transform.m11());
        

        will give you your angle (in radians).

        Read and abide by the Qt Code of Conduct

        O 1 Reply Last reply
        1
        • kshegunovK kshegunov

          Depends on what you put in your transform. Did you do any scaling, reflection, translation? If you did not, then:

          qreal angle = std::acos(transform.m11());
          

          will give you your angle (in radians).

          O Offline
          O Offline
          ofmrew
          wrote on last edited by
          #4

          @kshegunov
          You are correct, but even if there were other operations, all is not lost. Recall that the inverse of matrix A multiplied by the matrix A yields, I, the identity matrix. So if you have RS, a rotation matrix multiplied by a scale matrix, then RSS(inverse) yields R, from which you can derive the angle. (Note, however, that S(inverse)RS will not yield R.) You can repeat the inverse process until you obtain R.

          VRoninV 1 Reply Last reply
          1
          • O ofmrew

            @kshegunov
            You are correct, but even if there were other operations, all is not lost. Recall that the inverse of matrix A multiplied by the matrix A yields, I, the identity matrix. So if you have RS, a rotation matrix multiplied by a scale matrix, then RSS(inverse) yields R, from which you can derive the angle. (Note, however, that S(inverse)RS will not yield R.) You can repeat the inverse process until you obtain R.

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            @ofmrew That assumes you know S in isolation which is probably not the case if you just have a QTransform object.

            For the men-in-the-street the post above means that if you know what operations you did (scale in the example) you can always revert them (scale back in the example) until you are left with just the rotation.

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved