Search code examples
c#uielement

Calculation of childelement center via UIElement.TranslatePoint deviates from correct value


I have a rectangle with some elements around it that the user may rotate by mouse. The rotation as such works with RotateTransform, but there is always a slight offset in the rotation center. I calculate the center like this:

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

CornerA and CornerC are the corners of the rectangle and build a diagonal. Now i want to set the center of all RotateTransforms relative to the corresponding elements.

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;

Depending on the angle, the center for all the elements around the rectangle should change, but the center of the rectangle itself should always be 32,32. But the values deviate and the values for both X and Y range from 29 to 35. I already checked the calculation of rotationCenter and it is correct. Why is this deviation ocuring and how can i fix it? Thank you...


Solution

  • OK, since no one seems to have any idea, i decided to circumvent the problem instead of actually solving it. So now i calculate the relative rotation centers "manually". I'm not sure if this will help anyone, but it worked for me.

    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);