add resize
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -355,11 +355,28 @@
|
||||
|
||||
<Separator Margin="0,10,0,10" />
|
||||
|
||||
<TextBlock Text="Смещение dX" /> <TextBox x:Name="MoveDx" />
|
||||
<TextBlock Text="Смещение dY" /> <TextBox x:Name="MoveDy" />
|
||||
<TextBlock Text="Смещение dX" />
|
||||
<TextBox x:Name="MoveDx" />
|
||||
<TextBlock Text="Смещение dY" />
|
||||
<TextBox x:Name="MoveDy" />
|
||||
|
||||
<Button Content="Переместить" Margin="0,10,0,0" Click="ClickMoveSelected" />
|
||||
<Button Content="Удалить" Margin="0,10,0,0" Click="ClickDeleteSelected" />
|
||||
<Button Content="Переместить" Margin="0,10,0,0" Click="ClickMoveSelected" />
|
||||
<Button Content="Удалить" Margin="0,10,0,0" Click="ClickDeleteSelected" />
|
||||
|
||||
<Separator />
|
||||
|
||||
<TextBlock Text="Новый размер (W/H)" />
|
||||
<TextBox x:Name="ResizeW" />
|
||||
<TextBox x:Name="ResizeH" />
|
||||
|
||||
<TextBlock
|
||||
Text="Для квадрата/круга W должен равняться H. Для многоугольника меняется bounding box."
|
||||
Foreground="{StaticResource TextBrushHint}"
|
||||
TextWrapping="Wrap"
|
||||
Margin="0,6,0,0"
|
||||
/>
|
||||
|
||||
<Button Content="Изменить размер" Margin="0,10,0,0" Click="ClickResizeSelected" />
|
||||
</StackPanel>
|
||||
</TabItem>
|
||||
</TabControl>
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user