通过 UIElement.TranslatePoint 计算子元素中心偏离正确值

Posted

技术标签:

【中文标题】通过 UIElement.TranslatePoint 计算子元素中心偏离正确值【英文标题】:Calculation of childelement center via UIElement.TranslatePoint deviates from correct value 【发布时间】:2019-06-30 05:48:12 【问题描述】:

我有一个矩形,它周围有一些元素,用户可以通过鼠标旋转。这样的旋转适用于 RotateTransform,但旋转中心总是有轻微的偏移。 我这样计算中心:

this.rotationCenter = this.cornerA.CalculatePointOnLine(this.cornerC, 0.5);

CornerA 和 CornerC 是矩形的角,构成对角线。 现在我想设置所有 RotateTransforms 相对于相应元素的中心。

Logger.Logger.Write("coord.log", "center : " + this.rotationCenter.X + " ; " + this.rotationCenter.Y);
this.rotateSurroundingRectangle.CenterX = this.ParentLayer.TranslatePoint(this.rotationCenter, this.suroundingRectangle).X;
this.rotateSurroundingRectangle.CenterY = this.ParentLayer.TranslatePoint(this.rotationCenter, this.suroundingRectangle).Y;

Logger.Logger.Write("coord.log", "relative center : " + this.rotateSurroundingRectangle.CenterX + " ; " + this.rotateSurroundingRectangle.CenterY);

this.rotateResizeCorner1.CenterX = this.ParentLayer.TranslatePoint(this.rotationCenter, this.resizeCorner1).X;
this.rotateResizeCorner1.CenterY = this.ParentLayer.TranslatePoint(this.rotationCenter, this.resizeCorner1).Y;
this.rotateResizeCorner2.CenterX = this.ParentLayer.TranslatePoint(this.rotationCenter, this.resizeCorner2).X;
this.rotateResizeCorner2.CenterY = this.ParentLayer.TranslatePoint(this.rotationCenter, this.resizeCorner2).Y;
this.rotateResizeCorner3.CenterX = this.ParentLayer.TranslatePoint(this.rotationCenter, this.resizeCorner3).X;
this.rotateResizeCorner3.CenterY = this.ParentLayer.TranslatePoint(this.rotationCenter, this.resizeCorner3).Y;
this.rotateResizeCorner4.CenterX = this.ParentLayer.TranslatePoint(this.rotationCenter, this.resizeCorner4).X;
this.rotateResizeCorner4.CenterY = this.ParentLayer.TranslatePoint(this.rotationCenter, this.resizeCorner4).Y;
this.rotateRotationSign.CenterX = this.ParentLayer.TranslatePoint(this.rotationCenter, this.rotationSign).X;
this.rotateRotationSign.CenterY = this.ParentLayer.TranslatePoint(this.rotationCenter, this.rotationSign).Y;

根据角度,矩形周围所有元素的中心应该改变,但矩形本身的中心应该始终是 32,32。 但值存在偏差,X 和 Y 的值都在 29 到 35 之间。 我已经检查了rotationCenter的计算,它是正确的。 为什么会出现这种偏差,我该如何解决? 谢谢...

【问题讨论】:

【参考方案1】:

好的,由于似乎没有人知道,我决定绕过这个问题而不是实际解决它。所以现在我“手动”计算相对旋转中心。我不确定这是否会帮助任何人,但它对我有用。

this.rotateSurroundingRectangle.CenterX = (this.surroundingRectangle.Width * 0.5);
this.rotateSurroundingRectangle.CenterY = (this.surroundingRectangle.Height * 0.5);
this.rotateResizeCorner1.CenterX = this.rotationCenter.X - Canvas.GetLeft(this.resizeCornerA);
this.rotateResizeCorner1.CenterY = this.rotationCenter.Y - Canvas.GetTop(this.resizeCornerA);
this.rotateResizeCorner2.CenterX = this.rotationCenter.X - Canvas.GetLeft(this.resizeCornerB);
this.rotateResizeCorner2.CenterY = this.rotationCenter.Y - Canvas.GetTop(this.resizeCornerB);
this.rotateResizeCorner3.CenterX = this.rotationCenter.X - Canvas.GetLeft(this.resizeCornerC);
this.rotateResizeCorner3.CenterY = this.rotationCenter.Y - Canvas.GetTop(this.resizeCornerC);
this.rotateResizeCorner4.CenterX = this.rotationCenter.X - Canvas.GetLeft(this.resizeCornerD);
this.rotateResizeCorner4.CenterY = this.rotationCenter.Y - Canvas.GetTop(this.resizeCornerD);
this.rotateRotationSign.CenterX = this.rotationCenter.X - Canvas.GetLeft(this.rotationSign);
this.rotateRotationSign.CenterY = this.rotationCenter.Y - Canvas.GetTop(this.rotationSign);

【讨论】:

以上是关于通过 UIElement.TranslatePoint 计算子元素中心偏离正确值的主要内容,如果未能解决你的问题,请参考以下文章

下拉框多选框单选框 通过TagHelper绑定数据

酶:测试孩子通过安装渲染失败,但通过浅时通过

java是通过值传递,也就是通过拷贝传递——通过方法操作不同类型的变量加深理解

通过代码进行 Spring 配置与通过注释进行配置

如何理解“不要通过共享内存来通信,而应该通过通信来共享内存”?

通过邮递员通过 API 使用 Rails 主动存储上传文件(.pdf、.jpg 等)? (不通过 Rails 视图)