5,696,576 members and growing! (15,489 online)
Email Password   helpLost your password?
Multimedia » GDI » Beginners     Beginner

Using SetWorldTransform() to rotate basic shapes by any angle

By lucy

How to use SetWorldTransform() to draw basic shapes rotated by any angle
VC6, C++Windows, Win2K, MFC, GDI, VS6, Visual Studio, Dev

Posted: 24 Feb 2002
Updated: 24 Feb 2002
Views: 74,020
Bookmarked: 28 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
22 votes for this Article.
Popularity: 5.69 Rating: 4.24 out of 5
2 votes, 11.8%
1
0 votes, 0.0%
2
1 vote, 5.9%
3
3 votes, 17.6%
4
11 votes, 64.7%
5

Introduction

MFC provides many functions to draw basic shapes, such as rectangle, ellipse, polygon, etc. It’s just a piece of cake. Also, a rectangle rotated 45 degrees can be drawn easily after doing a little math. But what if we want to rotate these shapes to a weird angle, say 11 degrees? Or we want to rotate an ellipse by 37 degrees?

Use SetWorldTransform() can free you from all the mathematics.

Details

First of all, let’s review the definition of rotation: the rotation performs a geometric transform which maps the position (x1, y1) of a picture element in an input image onto a position (x2, y2) in an output image by rotating it through a user-specified angle q about an origin (x0, y0).

The formula is:

    x2 = cos(q)*(x1-x0) – sin(q)*(y1-y0) + x0;
    y2 = sin(q)*(x1-x0) + cos(q)*(y1-y0) + y0; // ------ (1)

where (x0, y0) are the coordinates of the center of rotation (in the input image) and q is the angle of rotation with clockwise rotations having positive angles.

Note here that we are working in image coordinates, so the y axis goes downward. With above formulas in mind, let’s check what SetWorldTransform() can do for us.

From MSDN, it’s said that using SetWorldTransform(), for any coordinates (x, y) in world space, the transformed coordinates in page space (x’, y’) can be determined by the following algorithm:

    x’ = x * eM11 + y * eM12 + eDx;
    y’ = x * eM12 + y * eM22 + eDy; // ------ (2)

Compare these two groups of formulas, we can get the correct values for parameter xform:

    xform.eM11 = cos(q);
    xform.eM12 = sin(q);
    xform.eM21 = -sin(q);
    xform.eM22 = cos(q);
    xform.eDx = x0 – cos(q)*x0 + sin(q)*y0;
    xform.eDy = y0 – cos(q)*y0 - sin(q)*x0;

That’s all the myth. Included is a simple project to demonstrate it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

lucy


I love this place!
Occupation: Software Developer
Location: Canada Canada

Other popular GDI articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
GeneralRotating bitmap with any angle with out usinf MFCmemberswethakiran18:31 3 Feb '08  
GeneralAmazing, but isn't it a little obviousmemberArtiBen4:52 25 Jun '07  
Generalcode to change orientation.memberchinesepatriotleague20:33 30 May '07  
GeneralThank you so much!memberfreeman86822:17 27 Mar '07  
QuestionWhat is the Logic behind the API CreateRoundRectRgn() or codememberprabhashmandal4:10 18 Aug '06  
GeneralCombine with scaling...memberRYU^^5:37 9 Oct '05  
GeneralRe: Combine with scaling...memberArtiBen4:54 25 Jun '07  
GeneralWinCE EquivalentmemberMeenu_Sachdeva6:26 23 May '05  
GeneralVery good!memberandystone20:48 6 May '05  
GeneralPrintmemberbitmensch23:51 13 Feb '05  
GeneralSeries of circlesmemberJolyn15:37 13 Dec '03  
GeneralIt can't work for Windows 95/98/MememberDanney23:26 15 Apr '03  
GeneralRe: It can't work for Windows 95/98/Mememberjdunlap11:06 26 Sep '03  
GeneralI need to rotate the whole form (A graphical editor)memberDhirendra3:32 10 Mar '03  
GeneralRe: I need to rotate the whole form (A graphical editor)memberBarty796:03 1 Jul '05  
GeneralRe: I need to rotate the whole form (A graphical editor)memberDhirendra21:00 3 Jul '05  
Generalresizing and moving images as in Word Artmembersreemail0:24 14 Dec '02  
GeneralRe: resizing and moving images as in Word ArtmemberArtiBen4:58 25 Jun '07  
GeneralNice! But show the posibilities! :)memberLars [Large] Werner10:41 13 Dec '02  
GeneralNice description and demonstrationmemberShog97:08 5 Mar '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Feb 2002
Editor: Nishant Sivakumar
Copyright 2002 by lucy
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project