Results 1 to 8 of 8

Thread: Rotate QPixmap around an arbitrary point

  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Rotate QPixmap around an arbitrary point

    I'm trying to do a thing where I rotate an image around an arbitrary point (generally within the bounds of the image itself), but I want to retain the point that corresponds to the untransformed image's origin.

    Thus far, I'm using the QTransform approach to rotate the image:

    Qt Code:
    1. image.transformed(QTransform()
    2. .translate(-point.x(), -point.y())
    3. .rotateRadians(rot)
    4. .translate(point.x(), point.y()));
    To copy to clipboard, switch view to plain text mode 

    This returns an image that is "rotated" around the point, but it is drawn into a new image object whose origin is respective to the transformed image, not the original, so painting the image makes it have the correct angle but is translated somewhere way out of position. Is there a way to either A] calculate the original origin's location in the transformed image, or B] rotate an image without altering its origin.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Rotate QPixmap around an arbitrary point

    You should translate the resulting image along the vector obtained by mapping the new origin point to the old origin point. QTransform has a family of map() methods to help you with that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2014
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Rotate QPixmap around an arbitrary point

    Using the Transform.map method on the origin point only applies the transformations to the image on the origin point as well, which tells me where the origin point is in the image's local space. It doesn't tell me where the origin point is with respect to the transformed image's new origin point.

    Anyway, I've already solved this problem by calculating the distance between the original image's center and the transformed image's center, and adding that offset to the transformed origin point.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Rotate QPixmap around an arbitrary point

    Quote Originally Posted by Abion47 View Post
    It doesn't tell me where the origin point is with respect to the transformed image's new origin point.
    Sure it does.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2014
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Rotate QPixmap around an arbitrary point

    Quote Originally Posted by wysota View Post
    Sure it does.
    Thanks for the detailed answer.

    I'm not getting the map() method to give me anywhere near the right values. I'm using a QTransform to translate the origin by the inverse of the rotation point, rotating, translating back, then offsetting by the aforementioned difference in the image centers. I then compare the values to the ones from the ones coming out of my existing method that I know works, and its not even in the same ballpark.

    Maybe its some error in my code, but like I said, I already have a method that works, so I don't really see the point in investigating further.

    Qt Code:
    1. QPixmap getTransformedImage(QPixmap image, QPointF pos, QPointF anchor, qreal rot, QPointF &origin)
    2. {
    3. QPointF center = QPointF(image.width() / 2, image.height() / 2);
    4. qreal dist = QLineF(anchor, center).length();
    5. qreal a = qAtan2(anchor.y() - center.y(), anchor.x() - center.x());
    6. QPointF rotAnchor(qCos(rot + a) * dist, qSin(rot + a) * dist);
    7. rotAnchor += center;
    8.  
    9. QPixmap rotImage = image.transformed(QTransform().rotateRadians(rot));
    10.  
    11. QPointF rotCenter = QPointF(rotImage.width() / 2, rotImage.height() / 2);
    12. QPointF offset = rotCenter - center;
    13.  
    14. origin = pos - (rotAnchor + offset);
    15.  
    16. return rotImage;
    17. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Rotate QPixmap around an arbitrary point

    The QTransform object you have can be used to transform points from the "old" coordinate system to the "new" coordinate system. If you want to do a mapping the other way, you have to invert the transform (QTransform::inverted()). If you map the new origin to a point in the old coordinate system and then calculate the difference vector, you'll know how much to move the new coordinate system so that the two points match.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2014
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Rotate QPixmap around an arbitrary point

    That seems overly complex though. What's wrong with just using raw math functions to rotate the origin around the same pivot point as the image then offset by the difference in centers?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Rotate QPixmap around an arbitrary point

    I didn't say there was something wrong with your approach. You don't even have to use QTransform at all if you want to do all the math by yourself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 13th May 2013, 22:29
  2. Replies: 6
    Last Post: 11th November 2009, 11:42
  3. Rotate QPixmap set on QLabel
    By Qt Coder in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2009, 13:08
  4. Replies: 2
    Last Post: 20th January 2009, 08:13
  5. Can I use arbitrary constants in a connect()?
    By WinchellChung in forum Newbie
    Replies: 3
    Last Post: 18th February 2008, 23:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.