From 5cd56135b5ecfdf22bf2486a26ce41f85cf9647f Mon Sep 17 00:00:00 2001 From: Debug_pro Date: Thu, 26 Feb 2026 12:12:38 +0300 Subject: [PATCH] add resize --- DrawFigureLibrary/Figure.cs | 27 +++++++++++- DrawFigureLibrary/Figures/PolygonFigure.cs | 49 +++++++++++++++++++--- Main/MainWindow.xaml | 25 +++++++++-- Main/MainWindow.xaml.cs | 32 ++++++++++++++ 4 files changed, 122 insertions(+), 11 deletions(-) diff --git a/DrawFigureLibrary/Figure.cs b/DrawFigureLibrary/Figure.cs index b56f8f8..a9f7ded 100644 --- a/DrawFigureLibrary/Figure.cs +++ b/DrawFigureLibrary/Figure.cs @@ -42,7 +42,7 @@ namespace DrawFigureLibrary } public virtual bool HitTest(Point p) => p.X >= x && p.X <= x + w && p.Y >= y && p.Y <= y + h; - + public virtual bool MoveByChecked(int dX, int dY) { if (Init.DrawingImage == null) return false; @@ -51,5 +51,28 @@ namespace DrawFigureLibrary MoveBy(dX, dY); return true; } + + public virtual void ResizeTo(int newW, int newH) + { + if (newW <= 0 || newH <= 0) throw new ArgumentException("Размеры должны быть > 0"); + w = newW; + h = newH; + } + + public virtual bool ResizeToChecked(int newW, int newH) + { + if (Init.DrawingImage == null) return false; + if (newW <= 0 || newH <= 0) return false; + + long nr = (long)x + newW; + long nb = (long)y + newH; + + if (x < 0 || y < 0) return false; + if (nr > (long)Init.DrawingImage.ActualWidth) return false; + if (nb > (long)Init.DrawingImage.ActualHeight) return false; + + ResizeTo(newW, newH); + return true; + } } -} +} \ No newline at end of file diff --git a/DrawFigureLibrary/Figures/PolygonFigure.cs b/DrawFigureLibrary/Figures/PolygonFigure.cs index 870e608..75061dd 100644 --- a/DrawFigureLibrary/Figures/PolygonFigure.cs +++ b/DrawFigureLibrary/Figures/PolygonFigure.cs @@ -1,4 +1,5 @@ -using System.Windows; +using System; +using System.Windows; using System.Windows.Media; namespace DrawFigureLibrary.Figures @@ -9,13 +10,14 @@ namespace DrawFigureLibrary.Figures public PolygonFigure(string name, Point[] points) : base(name, 0, 0, 0, 0) { - if (points == null || points.Length < 3) throw new ArgumentException("Многоугольник должен иметь минимум 3 точки."); + if (points == null || points.Length < 3) + throw new ArgumentException("Многоугольник должен иметь минимум 3 точки."); Points = points; RecalcBounds(); } - private void RecalcBounds() + protected void RecalcBounds() { double minX = Points.Min(p => p.X); double minY = Points.Min(p => p.Y); @@ -45,10 +47,47 @@ namespace DrawFigureLibrary.Figures if (Check(this, deltaX, deltaY, Init.DrawingImage)) { - for (int i = 0; i < Points.Length; i++) Points[i] = new Point( Points[i].X + deltaX, Points[i].Y + deltaY ); + for (int i = 0; i < Points.Length; i++) + Points[i] = new Point(Points[i].X + deltaX, Points[i].Y + deltaY); RecalcBounds(); } } + + public override void ResizeTo(int newW, int newH) + { + if (newW <= 0 || newH <= 0) throw new ArgumentException("Размеры должны быть > 0"); + + double oldW = w; + double oldH = h; + + if (oldW <= 0 || oldH <= 0) + { + RecalcBounds(); + oldW = w; + oldH = h; + + if (oldW <= 0 || oldH <= 0) throw new ArgumentException("Нельзя изменить размер: вырожденный многоугольник."); + } + + double sx = newW / oldW; + double sy = newH / oldH; + + double ox = x; + double oy = y; + + for (int i = 0; i < Points.Length; i++) + { + double px = Points[i].X; + double py = Points[i].Y; + + double nx = ox + (px - ox) * sx; + double ny = oy + (py - oy) * sy; + + Points[i] = new Point(nx, ny); + } + + RecalcBounds(); + } } -} +} \ No newline at end of file diff --git a/Main/MainWindow.xaml b/Main/MainWindow.xaml index b4fac30..f9398ba 100644 --- a/Main/MainWindow.xaml +++ b/Main/MainWindow.xaml @@ -355,11 +355,28 @@ - - + + + + -