12,348,536 members (68,187 online)
alternative version

138K views
45 bookmarked
Posted

# Using SetWorldTransform() to rotate basic shapes by any angle

, 24 Feb 2002
 Rate this:
How to use SetWorldTransform() to draw basic shapes rotated by any angle

## 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.

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

I love this place!

## Comments and Discussions

 First Prev Next
 Helping me eleven years later! Alasdair Craig4-Sep-13 8:44 Alasdair Craig 4-Sep-13 8:44
 Outstanding! Good suppliment to the MFC docs Bruff16-Mar-11 5:02 Bruff 16-Mar-11 5:02
 Rotating bitmap with any angle with out usinf MFC swethakiran3-Feb-08 17:31 swethakiran 3-Feb-08 17:31
 Amazing, but isn't it a little obvious ArtiBen25-Jun-07 3:52 ArtiBen 25-Jun-07 3:52
 code to change orientation. chinesepatriotleague30-May-07 19:33 chinesepatriotleague 30-May-07 19:33
 Thank you so much! freeman86827-Mar-07 21:17 freeman868 27-Mar-07 21:17
 What is the Logic behind the API CreateRoundRectRgn() or code prabhashmandal18-Aug-06 3:10 prabhashmandal 18-Aug-06 3:10
 Combine with scaling... RYU^^9-Oct-05 4:37 RYU^^ 9-Oct-05 4:37
 Re: Combine with scaling... ArtiBen25-Jun-07 3:54 ArtiBen 25-Jun-07 3:54
 WinCE Equivalent Meenu_Sachdeva23-May-05 5:26 Meenu_Sachdeva 23-May-05 5:26
 Very good! andystone6-May-05 19:48 andystone 6-May-05 19:48
 Print bitmensch13-Feb-05 22:51 bitmensch 13-Feb-05 22:51
 Series of circles Jolyn13-Dec-03 14:37 Jolyn 13-Dec-03 14:37
 It can't work for Windows 95/98/Me Danney15-Apr-03 22:26 Danney 15-Apr-03 22:26
 Re: It can't work for Windows 95/98/Me jdunlap26-Sep-03 10:06 jdunlap 26-Sep-03 10:06
 I need to rotate the whole form (A graphical editor) Dhirendra10-Mar-03 2:32 Dhirendra 10-Mar-03 2:32
 Re: I need to rotate the whole form (A graphical editor) Barty791-Jul-05 5:03 Barty79 1-Jul-05 5:03
 Re: I need to rotate the whole form (A graphical editor) Dhirendra3-Jul-05 20:00 Dhirendra 3-Jul-05 20:00
 resizing and moving images as in Word Art sreemail13-Dec-02 23:24 sreemail 13-Dec-02 23:24
 Re: resizing and moving images as in Word Art ArtiBen25-Jun-07 3:58 ArtiBen 25-Jun-07 3:58
 Nice! But show the posibilities! :) Lars [Large] Werner13-Dec-02 9:41 Lars [Large] Werner 13-Dec-02 9:41
 Nice description and demonstration Shog95-Mar-02 6:08 Shog9 5-Mar-02 6:08
 Last Visit: 31-Dec-99 18:00     Last Update: 24-Jun-16 6:42 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.