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 @@
-
-
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Main/MainWindow.xaml.cs b/Main/MainWindow.xaml.cs
index 2835851..7736be1 100644
--- a/Main/MainWindow.xaml.cs
+++ b/Main/MainWindow.xaml.cs
@@ -304,6 +304,38 @@ namespace Main
}
}
+ private void ClickResizeSelected(object sender, RoutedEventArgs e)
+ {
+ try
+ {
+ var fig = GetSelectedFigureOrThrow();
+
+ if (!TryReadInt(ResizeW.Text, "W", 1, CanvasW, out int newW)) return;
+ if (!TryReadInt(ResizeH.Text, "H", 1, CanvasH, out int newH)) return;
+
+ if (fig is SquareFigure || fig is CircleFigure)
+ {
+ if (newW != newH)
+ {
+ MessageBox.Show("Для квадрата/круга W должен быть равен H.");
+ return;
+ }
+ }
+
+ if (!fig.ResizeToChecked(newW, newH))
+ {
+ MessageBox.Show("Фигура не помещается в область рисования.");
+ return;
+ }
+
+ Redraw();
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(ex.Message);
+ }
+ }
+
public MainWindow()
{
InitializeComponent();