static Transform CreateNormalizeTransform(Rect bounds, Point location, Size size, double angle) { var t = new TransformGroup(); var b = bounds; // Move center of the geometry to origin t.Children.Add(new TranslateTransform(-b.X - b.Width / 2, -b.Y - b.Height / 2)); // Rotate around origin t.Children.Add(new RotateTransform(angle)); b = t.TransformBounds(bounds); // Scale to requested size t.Children.Add(new ScaleTransform(size.Width / b.Width, size.Height / b.Height)); b = t.TransformBounds(bounds); // Move to requested position t.Children.Add(new TranslateTransform(-b.X + location.X, -b.Y + location.Y)); t.Freeze(); b = t.TransformBounds(bounds); Debug.Assert(Math.Abs(b.X - location.X) < 0.0001); Debug.Assert(Math.Abs(b.Y - location.Y) < 0.0001); Debug.Assert(Math.Abs(b.Width - size.Width) < 0.0001); Debug.Assert(Math.Abs(b.Height - size.Height) < 0.0001); return(t); }
public void TransformBounds_When_Nested() { var rect = new Rect(1, 2, 20, 10); var transform = new TransformGroup(); transform.Children.Add(new TranslateTransform() { X = 1, Y = 9 }); transform.Children.Add(new MatrixTransform(MatrixTest.GetIncrementalMatrix(3, 1))); var nestedTransform = new TransformGroup(); nestedTransform.Children.Add(new TranslateTransform() { X = -1, Y = -9 }); nestedTransform.Children.Add(new ScaleTransform() { ScaleX = -0.5, ScaleY = 1.0 / 6.0 }); transform.Children.Add(nestedTransform); var result = transform.TransformBounds(rect); result.X.Should().Be(-88.5); result.Y.Should().BeInRange(12.166666, 12.166667); result.Width.Should().Be(55); result.Height.Should().BeInRange(23.333333, 23.333334); }
private void PrepareBackgroundCanvas() { var size = GetBackgroundCanvasSize(); var content = GetContent(); if (content != null) { // Build covering canvas' surrounding the content var group = new TransformGroup(); group.Children.Add(content.TransformToVisual(null) as Transform); if (Application.Current.Host.Settings.EnableAutoZoom) { var scale = 1d / Application.Current.Host.Content.ZoomFactor; group.Children.Add(new ScaleTransform { ScaleX = scale, ScaleY = scale }); } Rect bounds = group.TransformBounds(new Rect(0d, 0d, content.ActualWidth, content.ActualHeight)); AddCanvas(0d, bounds.Top, bounds.Left, bounds.Height); // Side left AddCanvas(0d, 0d, size.Width, bounds.Top); // Entire top AddCanvas(bounds.Right, bounds.Top, size.Width - bounds.Right, bounds.Height); // Side right AddCanvas(0d, bounds.Bottom, size.Width, size.Height - bounds.Bottom); // Entire bottom } else { // No content, so cover entire host area AddCanvas(0d, 0d, size.Width, size.Height); } _backgroundCanvas.Width = size.Width; _backgroundCanvas.Height = size.Height; }
public void TransformBounds() { var rect = new Rect(0, 0, 100, 100); var transform = new TransformGroup(); transform.Children.Add(new TranslateTransform() { X = 1, Y = 9 }); transform.Children.Add(new MatrixTransform(MatrixTest.GetIncrementalMatrix(3, 1))); var result = transform.TransformBounds(rect); result.X.Should().Be(55); result.Y.Should().Be(66); result.Width.Should().Be(800); result.Height.Should().Be(1000); }
void Viewbox_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) { _previousTransform.Matrix = _transformGroup.Value; var center = _previousTransform.TransformPoint(e.Position); _deltaTransform.CenterX = center.X; _deltaTransform.CenterY = center.Y; _deltaTransform.Rotation = e.Delta.Rotation; _deltaTransform.ScaleX = e.Delta.Scale; _deltaTransform.ScaleY = e.Delta.Scale; _deltaTransform.TranslateX = e.Delta.Translation.X; _deltaTransform.TranslateY = e.Delta.Translation.Y; var control = e.Container as FrameworkElement; var controlRect = new Rect(0, 0, control.DesiredSize.Width, control.DesiredSize.Height); BoundingRect = _transformGroup.TransformBounds(controlRect); }
private void LayoutGraph(Size size) { PathGeometry path = LineGraph.Data as PathGeometry; if (path != null && data != null && points != null) { Rect bounds = this.bounds; double yw = YAxis.RenderSize.Width; var w = size.Width - yw - 20; var h = size.Height - 80; if (zoomToFit) { zoom = w / bounds.Width; } var ymargin = TopMargin; if (bounds != Rect.Empty && bounds.Height != 0) { TransformGroup g = new TransformGroup(); g.Children.Add(new TranslateTransform(0, -bounds.Top)); g.Children.Add(new ScaleTransform(zoom, -h / bounds.Height)); g.Children.Add(new TranslateTransform(LeftMargin, h + ymargin)); List <Point> pts = new List <Point>(); foreach (Point p in points) { Point t = g.Transform(p); pts.Add(t); } scaled = pts; LineGraph.Data = CreateGeometry(pts); Rect newBounds = g.TransformBounds(bounds); Border.Width = newBounds.Width; Border.Height = newBounds.Height; double xMean = Mean(from p in pts select p.X); double yMean = Mean(from p in pts select p.Y); double xVariance = Variance(from p in pts select p.X); double yVariance = Variance(from p in pts select p.Y); double covariance = Covariance(pts); double b = covariance / xVariance; double a = yMean - (b * xMean); TrendLine.X1 = 0; TrendLine.X2 = w; TrendLine.Y1 = a; TrendLine.Y2 = a + (b * w); // now run on the raw data xMean = Mean(from p in points select p.X); yMean = Mean(from p in points select p.Y); xVariance = Variance(from p in points select p.X); yVariance = Variance(from p in points select p.Y); covariance = Covariance(points); double realb = covariance / xVariance; double reala = yMean - (b * xMean); LineLabel.Content = string.Format("y = {0} + {1}x", reala.ToString("N0"), realb.ToString("N0")); g = new TransformGroup(); g.Children.Add(new RotateTransform(Math.Atan((TrendLine.Y2 - TrendLine.Y1) / (TrendLine.X2 - TrendLine.X2)))); g.Children.Add(new TranslateTransform(0, a - 20)); LineLabel.RenderTransform = g; } } }
private static void CorrectBounds(Rect elementRect, Size availableSize, Thickness scaleOffsets, TransformGroup transform) { var bounds = transform.TransformBounds(elementRect); var extendedRect = GetExtendedAvaliableRect(scaleOffsets, new Size(bounds.Width, bounds.Height), availableSize); var widthRatio = bounds.Width / extendedRect.Width; var heightRatio = bounds.Height / extendedRect.Height; var correctionX = 0d; var correctionY = 0d; if (widthRatio > 1) { if (bounds.Left > extendedRect.Left) { correctionX = extendedRect.Left - bounds.Left; } if (bounds.Right < extendedRect.Right) { correctionX = extendedRect.Right - bounds.Right; } } else { if (bounds.Left < extendedRect.Left && bounds.Right < extendedRect.Right) { correctionX = extendedRect.Left - bounds.Left; } if (bounds.Left > extendedRect.Left && bounds.Right > extendedRect.Right) { correctionX = extendedRect.Right - bounds.Right; } } if (heightRatio > 1) { if (bounds.Top > extendedRect.Top) { correctionY = extendedRect.Top - bounds.Top; } if (bounds.Bottom < extendedRect.Bottom) { correctionY = extendedRect.Bottom - bounds.Bottom; } } else { if (bounds.Top > extendedRect.Top && bounds.Bottom > extendedRect.Bottom) { correctionY = extendedRect.Bottom - bounds.Bottom; } if (bounds.Top < extendedRect.Top && bounds.Bottom < extendedRect.Bottom) { correctionY = extendedRect.Top - bounds.Top; } } if (correctionX != 0 || correctionY != 0) { AddTranslation(new Point(correctionX, correctionY), transform); } }