From 111e8056a4f58eddf7342f830b6332defa7d0453 Mon Sep 17 00:00:00 2001 From: Luis Frey Date: Wed, 15 Jan 2025 16:18:54 +0100 Subject: [PATCH] refactor(Projection): Unify SetPlacement overloads with Rectangle and RectangleF Removing duplicate code and moving `if (placementFactor == 0.0)` check to SetPlacement RectangleF overload. --- CADability/Projection.cs | 45 ++++++++-------------------------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/CADability/Projection.cs b/CADability/Projection.cs index ea23e778..ab508472 100644 --- a/CADability/Projection.cs +++ b/CADability/Projection.cs @@ -620,6 +620,12 @@ public double Precision precision = value; } } + + internal BoundingRect GetPlacement(Rectangle Destination) + { + return new BoundingRect(DrawingPlanePoint(new System.Drawing.Point(Destination.Left, Destination.Bottom)).To2D(), DrawingPlanePoint(new System.Drawing.Point(Destination.Right, Destination.Top)).To2D()); + } + /// /// Stellt die Platzierung im Zweidimensionalen ein: Das Quellrechteck /// soll in das Zielrechteck passen. @@ -628,42 +634,7 @@ public double Precision /// Das Quellrechteck in 2-dimensionalen Weltkoordinaten public void SetPlacement(Rectangle Destination, BoundingRect Source) { - // Höhe und Breite==0 soll einen Fehler liefern! - if (isPerspective) - { // Source ist im 2d system der projektionsebene - // Destination ist i.A. das ClientRect - GeoPoint2D ll = World2DToWindow(new GeoPoint2D(Source.Left, Source.Bottom)); - GeoPoint2D ur = World2DToWindow(new GeoPoint2D(Source.Right, Source.Top)); - GeoPoint center = ProjectionPlane.ToGlobal(Source.GetCenter()); - double width = ur.x - ll.x; - double height = ll.y - ur.y; - double f = Math.Min(Destination.Width / width, Destination.Height / height); - placementFactor *= f; - SetCoefficients(); - GeoPoint2D c2d = WorldToWindow(center); - placementX += (Destination.Left + Destination.Right) / 2.0 - c2d.x; - placementY += (Destination.Bottom + Destination.Top) / 2.0 - c2d.y; - SetCoefficients(); - return; - } - else - { - if (Source.Height == 0.0) placementFactor = Destination.Width / Source.Width; - else if (Source.Width == 0.0) placementFactor = Destination.Height / Source.Height; - else - { - placementFactor = Math.Min(Destination.Width / Source.Width, Destination.Height / Source.Height); - } - if (placementFactor == 0.0) placementFactor = 1.0; // this happens, when the Destination is 0 in one direction - // wie ist das mit der Y-Richtung in Destination? - placementX = (Destination.Right + Destination.Left) / 2.0 - (Source.Right + Source.Left) / 2.0 * placementFactor; - placementY = (Destination.Top + Destination.Bottom) / 2.0 + (Source.Top + Source.Bottom) / 2.0 * placementFactor; - SetCoefficients(); - } - } - internal BoundingRect GetPlacement(Rectangle Destination) - { - return new BoundingRect(DrawingPlanePoint(new System.Drawing.Point(Destination.Left, Destination.Bottom)).To2D(), DrawingPlanePoint(new System.Drawing.Point(Destination.Right, Destination.Top)).To2D()); + SetPlacement(new RectangleF(Destination.X, Destination.Y, Destination.Width, Destination.Height), Source); } public void SetPlacement(RectangleF Destination, BoundingRect Source) @@ -694,12 +665,14 @@ public void SetPlacement(RectangleF Destination, BoundingRect Source) { placementFactor = Math.Min(Destination.Width / Source.Width, Destination.Height / Source.Height); } + if (placementFactor == 0.0) placementFactor = 1.0; // wie ist das mit der Y-Richtung in Destination? placementX = (Destination.Right + Destination.Left) / 2.0 - (Source.Right + Source.Left) / 2.0 * placementFactor; placementY = (Destination.Top + Destination.Bottom) / 2.0 + (Source.Top + Source.Bottom) / 2.0 * placementFactor; SetCoefficients(); } } + public void SetPlacement(double factor, double dx, double dy) { placementFactor = factor;