add resize
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user