# How to use "SetWorldTransform"

Hi!
I'm trying to rotate a metafile with the SetWorldTransform.
But it rotates around the upper left corner of my picture.
I want it to rotate around the center.
How do i do that ???
###### Who is Participating?

Commented:
I've tried to correct type error and made another one... :)

If you calculate the center point of the metafile correctly, the code in my answer will rotate metafile around it's center without any problem.

Suppose, you playing metafile in rect 20,30,100,150. And, as I understood, you want to rotate it around the center of this rectangle.
If so, the center you must use in code is 60,90 ( [100-20]/2+20, [150-30]/2+30 ). And not the center of the metafile itself
(the center of the metafile is width/2,height/2 - 40,60 in our case).

Next, if you want to resize metafile, say, twice, use SetViewportExtEx(2,2). Then metafile appears rotated around its center in rect 40,60,200,300. In this case the center of the resized metafile on screen will be in 120,180 and the metafile will seem shifted to right and down (and twice size, of course).

If you want to keep the center of the metafile on screen in first point, 60,90, then use SetViewportOrgEx(60-120,90-180).

I assume the size of window is 1,1 and its origin is 0,0.

If you have more question, feel free to write me directly to nick@rtzi.ru

By the way, the second CombineTransform is not necessary. It's enough to set x2.eDX=Pivot.x, x2.eDY=Pivot.y. Moreover, the rotation may be done in one step. But this is no so simple - it's need to apply some mathematics to fill XFORM.
0

Commented:
how about setting the XFORM eDx and eDy to the point about which rotation occurs?
0

Author Commented:
Maybe some code could help ...

hDC = BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );

/* Rita kursskivan som bakgrund till kompassen */
SetMapMode( hDC, MM_ANISOTROPIC );

SetViewportOrgEx( hDC, ClientRect.left, ClientRect.top, NULL );

SetViewportExtEx( hDC, XSize, YSize, NULL );
PlayMetaFile( hDC, hMFBackGround );

if( Compass[ CpIx ]->Active ){
SelectObject( hDC, YellowBrush );
}
else SelectObject( hDC, BlackBrush );

SelectObject( hDC, GetStockObject( NULL_PEN ) );
SetMapMode( hDC, MM_TEXT );

/* Rita pie:n */
Start.x, Start.y, Stop.x, Stop.y );

/* Rita kompass nal */
NeedleWidth = NeedleRect.right - NeedleRect.left;
NeedleHight = NeedleRect.bottom - NeedleRect.top;

SetMapMode( hDC, MM_ANISOTROPIC );

SetViewportOrgEx( hDC, (int)(Origo.x - NeedleWidth/2.0),
(int)(Origo.y - NeedleHight/2.0), NULL );

SetViewportExtEx( hDC, NeedleWidth, NeedleHight, NULL );

Rotation = (float)(Compass[ CpIx ]->ComCourse * DEGTORAD);

NeedleXForm.eM11 = (float)cos( Rotation );
NeedleXForm.eM12 = (float)sin( Rotation );
NeedleXForm.eM21 = (float)-sin( Rotation );
NeedleXForm.eM22 = (float)cos( Rotation );

OldGraphMode = SetGraphicsMode( hDC, GM_ADVANCED );

SetWorldTransform( hDC, &NeedleXForm );

PlayMetaFile( hDC, hMFNeedle );

SetGraphicsMode( hDC, OldGraphMode );
EndPaint( hWnd, (LPPAINTSTRUCT)&ps );

0

Author Commented:
Thanks danny_pav

. but i've tried that and it doesnt work

any other suggestions ?
0

Commented:
AndersCarlsson,
do you keep in mind that SetWorldTransform does not supported on Win 95 ?
0

Commented:
Use the eDx and eDy members of NeedleXForm to perform the horizontal and vertical translation (how much you want to move by).

danny_pav had this ...what's the effect of this
0

Author Commented:
NickRepin: I work on a NT machine.

Answers2000: I don't want to move the picture just rotate it.

If i set some values in eDx and eDy the picture moves to that position it seems (upper left corner). I want it to stay in center and rotate around the center of the metafile.
0

Commented:
Try to call SetViewportExt and SetViewportOrg again after calling SetGraphicsMode.
Also, I think it would be better to call SetWindowExt and SetWindowOrg along with viewport functions after changing map/graphics mode. At least, that's how I did it, and all worked fine.
0

Author Commented:
NickRepin
I´ve tried to call SetViewportExt and Org again after SetGraphicsMode and to set XFORM eDx and eDy to the center of my metafile. But that doesn´t help at all, it still rotates around the upper left corner. Probably i´ve made some misstake somewhere, but i can´t find it. I´ve increased the points to 150 cause this is important for me.
0

Commented:
World transformation doesn't depend on Window and Viewport settings. You should rotate like this:

XFORM x1,x2,x3,result1,result2;

// At first, move the center of the metafile in the center
// of the world space
x1.eDx=-center.x;  // Center of your metafile
x1.eDy=-center.y;
x1.eM11=1;x1.eM12=0;x1.eM21=0;x1.eM22=1;

// Next, rotate around center of the world space
x2.eDx=0;
x2.eDy=0;
x2.eM11=cos(Angle);
x2.eM12=sin(Angle);
x2.eM21=-sin(Angle);
x2.eM22=cos(Angle);

// The last, return metafile back in its place
x3.eDx=center.x;
x3.eDy=center.y;
x3.eM11=1;x3.eM12=0;x3.eM21=0;x3.eM22=1;

// Now do eral job
CombineTransform(result1,&x1,&x2);
CombineTransform(result2,&result1,&x3);
SetWorldTransform(dc,&result2);

Good luck!

0

Commented:
Type errors...

// Now do REAL job....
..

Minus is so little on screen:
x1.eDX= - center.x
x2.eDY= - center.y
0

Author Commented:
It didn´t work NickRepin.
My MetaFile still rotates around the upper left corner
My code is as follows:

OldGraphMode = SetGraphicsMode( hDC, GM_ADVANCED );

SetViewportOrgEx( hDC, (int)(Origo.x - NeedleWidth/2.0),
(int)(Origo.y - NeedleHight/2.0), NULL );
SetViewportExtEx( hDC, NeedleWidth, NeedleHight, NULL );

Pivot.x = (int)(NeedleWidth/2.0);
Pivot.y = (int)(NeedleHight/2.0);

// At first move the center of the metafile
// in the center of the world space
x1.eDx = -Pivot.x;
x1.eDy = -Pivot.y;
x1.eM11 = 1;
x1.eM12 = 0;
x1.eM21 = 0;
x1.eM22 = 1;

// Next rotate around center of world space
x2.eDx = 0;
x2.eDy = -Pivot.y;
x2.eM11 = (float)cos( Rotation );
x2.eM12 = (float)sin( Rotation );
x2.eM21 = (float)-sin( Rotation );
x2.eM22 = (float)cos( Rotation );

// The last, return metafile back in its place
x3.eDx = Pivot.x;
x3.eDy = Pivot.y;
x3.eM11 = 1;
x3.eM12 = 0;
x3.eM21 = 0;
x3.eM22 = 1;

// Now do real job
CombineTransform(&result1, &x1, &x2 );
CombineTransform(&result2, &result1, &x3 );

SetWorldTransform( hDC, &result2 );

PlayMetaFile( hDC, hMFNeedle );

SetGraphicsMode( hDC, OldGraphMode );

/* Avsluta uppdatering av klientarean */
EndPaint( hWnd, (LPPAINTSTRUCT)&ps );

Whats wrong i think you can help me if you can ther´s another 100 points there for you.

0

Commented:
1) x2.eDY must be 0
-------------------
0

Author Commented:
OK!
1) I just followed your "Type error" comment.
2) SetGraphicsMode result is nonzero.

I tried to move the centerpoint step by step, and finally i succeeded with center.x = 252 and center.y = 640.
I don´t understand why, cause my windowsize is 125*125.
Must i set windowextension or what ?
0

Commented:
Another words, if I understood, instead of NeedleSize/2 use
Pivot=Origo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.