计算翻译后的旋转中心 [英] Calculating the center of rotation after translation

查看:124
本文介绍了计算翻译后的旋转中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够围绕给定点旋转图像,以便图像的任何部分出现在我的容器中心是旋转中心。

I need to be able to rotate an image around a given point so that what ever part of the image appears in the center of my container is the center of rotation.

要计算中心点,我目前只是采用应用于图像的平移的倒数:

To calculate the center points, I am currently just taking the inverse of the translation applied to the image:

Rotate.CenterX = Translate.X * -1;
Rotate.CenterY = Translate.Y * -1;

然而,我正在使用的当前计算是不够的,因为它不起作用如果图像有已被翻译 后被旋转。

However, the current calculation i'm using is not sufficient as it does not work if the image has been translated after being rotated.

我确定这是一个相当直接的触发功能,我只是想不出它是什么!

I'm sure it's a reasonably straight forward trig function, I just can't think what it is!

干杯

推荐答案

如果您正在使用GDI +,请使用以下:

If you are working with GDI+ then use the following:

double ImWidth = (double)Im.Width;
double ImHeight = (double)Im.Height;
double XTrans = -(ImWidth * X);
double YTrans = -(ImHeight * Y);

g.TranslateTransform((float)XTrans, (float)YTrans);    
g.TranslateTransform((float)(ImWidth / 2.0 - XTrans), (float)(ImHeight / 2.0 - YTrans));
g.RotateTransform((float)Angle);
g.TranslateTransform(-((float)(ImWidth / 2.0 - XTrans)), -((float)(ImHeight / 2.0 - YTrans)));

如果您正在使用WPF图形对象,请使用以下转换组:

If you are working with WPF graphic objects, use the following transform group:

TransformGroup TC = new TransformGroup();
RotateTransform RT = new RotateTransform(Angle);
RT.CenterX = Im.Width / 2.0;
RT.CenterY = Im.Height / 2.0;
TranslateTransform TT = new TranslateTransform(-X * Im.PixelWidth, -Y * Im.PixelHeight);
TC.Children.Add(TT);
TC.Children.Add(RT);

X& Y是要转换图像的百分比值(如果图像为1000像素,X为0.1,则图像将转换为100像素)。这就是我需要这个功能的方法,但你可以轻松地改变它。

X & Y are the percent values you want to translate the image in (if the image is 1000 pixels and X is 0.1 then the image will be translated 100 pixels). This is how I needed the function to work but you can easily change it otherwise.

这篇关于计算翻译后的旋转中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆