我创建了一个帮助程序来演示我的问题。从c中的操作事件计算位置#
我有一个填充图像画笔的矩形,这个画笔可以使用手势操纵在矩形内进行变形。
我正在确定矩形左上角的图像左上角的位置。当(仅)翻译图像时,我得到正确的值,但在使用捏手势时得到错误的值。如果放大太多并翻译图像,则画笔朝相反的方向移动。
下面是如何重现我的问题与下面附加的帮手应用程序: 运行该应用程序,只需移动(不捏)图像,直到您获取的位置值图像和矩形的左上角作为(0,0)。 下一页捏和移动图像,并一起返回左上角,现在你可以看到该值不是(0,0)。
这里是我的操纵增量事件:
public virtual void Brush_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
if (e.PinchManipulation != null)
{
// Rotate
_currentAngle = previousAngle + AngleOf(e.PinchManipulation.Original) - AngleOf(e.PinchManipulation.Current);
// Scale
_currentScale *= e.PinchManipulation.DeltaScale;
// Translate according to pinch center
double deltaX = (e.PinchManipulation.Current.SecondaryContact.X + e.PinchManipulation.Current.PrimaryContact.X)/2 -
(e.PinchManipulation.Original.SecondaryContact.X + e.PinchManipulation.Original.PrimaryContact.X)/2;
double deltaY = (e.PinchManipulation.Current.SecondaryContact.Y + e.PinchManipulation.Current.PrimaryContact.Y)/2 -
(e.PinchManipulation.Original.SecondaryContact.Y + e.PinchManipulation.Original.PrimaryContact.Y)/2;
_currentPos.X = previousPos.X + deltaX;
_currentPos.Y = previousPos.Y + deltaY;
}
else
{
// Translate
previousAngle = _currentAngle;
_currentPos.X += e.DeltaManipulation.Translation.X;
_currentPos.Y += e.DeltaManipulation.Translation.Y;
previousPos.X = _currentPos.X;
previousPos.Y = _currentPos.Y;
}
e.Handled = true;
ProcesstTransform();
}
void ProcesstTransform()
{
CompositeTransform gestureTransform = new CompositeTransform();
gestureTransform.CenterX = _currentPos.X;
gestureTransform.CenterY = _currentPos.Y;
gestureTransform.TranslateX = _currentPos.X - outputSize.Width/2.0;
gestureTransform.TranslateY = _currentPos.Y - outputSize.Height/2.0;
gestureTransform.Rotation = _currentAngle;
gestureTransform.ScaleX = gestureTransform.ScaleY = _currentScale;
brush.Transform = gestureTransform;
}