I have a problem by rotating the rectangle and placing it on the canvas in a certain way. I am trying to achieve this:
The big rectangle in the picture is my canvas. A small rectangle is my rectangle, which I want to rotate. When I rotate the rectangular (dotted rectangle), then it will be cut at all. To avoid this, I want to change the rectangular like a picture on the right.
In this way I have tried to do this so far:
rect rect = new rectangle (); Rect.Width = 100; Rect.Height = 50; Int angle = 30; Rect.RenderTransform = New RotateTransform (angle, rect.Width / 2, rect.Height / 2); Canvas.Children.Add (Rect); Double X = Math Cos (30) * (Rect. Width / 2) + Math Sign (30) * (Rect.Hight / 2) - Rect. With / 2; Double y = Math.Syn (30) * (-cith. With / 2) + Math Cos (30) * (Rect. High / 2) - Rect. Height / 2; Canvas SetLeft (rect, x); Canvas .Settop (Rect, Y); I thought that the best way to do this is to calculate the x and y offset and rectangle canvas SetLeft and canvas.SetTop . But I have problems in finding out how to do mathematics. (Y calculation seems to work). Actually, I want to keep several rectangles on the canvas at random posts. Rotation angle can be a value between -45 and 45 degrees, and rectangular shapes can also have random values. But the rectangle should always be fully visible in the canvas so I should know the offset for the limit of coordinates of the position. (The rectangular ones can overlap themselves.)
I hope you understand my problem. If you can help me then it would be nice.
The solution is actually guessed by you very easy instead of RenderTransform Instead of using, use LayoutTransform . It takes the same type of changes, but instead of applying the rendered output of the element during the rendering pass, it changes the layout position of the element during the layout pass. Then the following XAML produces the result shown in your screen shot in the second example.
& lt; Canvas width = "640" height = "480" & gt; & Lt; Fill Rectangle = "Blue" width = "200" height = "80" & gt; & Lt; Rectangle.LayoutTransform & gt; & Lt; RotateTransform angle = "-45" /> & Lt; /Rectangle.LayoutTransform> & Lt; / Rectangles & gt; & Lt; / Canvas & gt; Note that this does not work in Silverlight because Silverlight Layout does not support Transaction.
Comments
Post a Comment