|
@@ -1,4 +1,5 @@
|
|
|
-using System;
|
|
|
+using Microsoft.Win32;
|
|
|
+using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
@@ -22,58 +23,91 @@ namespace GraphDrawer
|
|
|
public GraphByClick()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
+
|
|
|
+ VertexArray.vertexCount = 0;
|
|
|
}
|
|
|
|
|
|
- private void backBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ private void backBtn_Click(object sender, RoutedEventArgs e) // Выбор режима рисования
|
|
|
{
|
|
|
ChoiseRegime regime = new ChoiseRegime();
|
|
|
regime.Show();
|
|
|
Close();
|
|
|
}
|
|
|
|
|
|
- private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
+ Vertex[] buffer = new Vertex[2];
|
|
|
private void canvas_MouseUp(object sender, MouseButtonEventArgs e)
|
|
|
{
|
|
|
- var point = e.GetPosition(canvas);
|
|
|
- int x = Convert.ToInt32(point.X);
|
|
|
+
|
|
|
+ var point = e.GetPosition(canvas); // Получение позиции курсора
|
|
|
+ int x = Convert.ToInt32(point.X); // Перевод в координаты
|
|
|
int y = Convert.ToInt32(point.Y);
|
|
|
if (vertexRb.IsChecked == true)
|
|
|
{
|
|
|
- Ellipse ellipse = new Ellipse();
|
|
|
- Vertex vertex = new Vertex(x, y);
|
|
|
- DrawVertex(ellipse, canvas, vertex);
|
|
|
- test.Content = vertex.X;
|
|
|
+ VertexArray.AddVertex(canvas, x, y);
|
|
|
+ }
|
|
|
+ if(edgeRb.IsChecked == true)
|
|
|
+ {
|
|
|
+ if (GraphState.focused)
|
|
|
+ {
|
|
|
+ var point1 = e.GetPosition(canvas);
|
|
|
+ int X = Convert.ToInt32(point1.X);
|
|
|
+ int Y = Convert.ToInt32(point1.Y);
|
|
|
+ for (int i = 0; i <= VertexArray.vertexCount - 1; i++)// Проверка на принадлежность точки к какой-либо вершине
|
|
|
+ {
|
|
|
+ if (((X - VertexArray.vertex[i].X <= 8) && (X - VertexArray.vertex[i].X >= 0)) ||
|
|
|
+ ((VertexArray.vertex[i].X - X <= 8) && (VertexArray.vertex[i].X - X >= 0)))
|
|
|
+ {
|
|
|
+ if (((Y - VertexArray.vertex[i].Y <= 8) && (Y - VertexArray.vertex[i].Y >= 0)) ||
|
|
|
+ ((VertexArray.vertex[i].Y - Y <= 8) && (VertexArray.vertex[i].Y - Y >= 0)))
|
|
|
+ {
|
|
|
+ if (VertexArray.buffer[0] == null)
|
|
|
+ {
|
|
|
+ VertexArray.buffer[0] = VertexArray.vertex[i];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ VertexArray.buffer[1] = VertexArray.vertex[i];
|
|
|
+ ArrowedEdgeArray.AddArrowedEdge(canvas, VertexArray.buffer[0], VertexArray.buffer[1]);
|
|
|
+ VertexArray.buffer[0] = null;
|
|
|
+ VertexArray.buffer[1] = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } // Конец проверки
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var point1 = e.GetPosition(canvas);
|
|
|
+ int X = Convert.ToInt32(point1.X);
|
|
|
+ int Y = Convert.ToInt32(point1.Y);
|
|
|
+ for (int i = 0; i <= VertexArray.vertexCount - 1; i++)// Проверка на принадлежность точки к какой-либо вершине
|
|
|
+ {
|
|
|
+ if (((X - VertexArray.vertex[i].X <= 8) && (X - VertexArray.vertex[i].X >= 0)) ||
|
|
|
+ ((VertexArray.vertex[i].X - X <= 8) && (VertexArray.vertex[i].X - X >= 0)))
|
|
|
+ {
|
|
|
+ if (((Y - VertexArray.vertex[i].Y <= 8) && (Y - VertexArray.vertex[i].Y >= 0)) ||
|
|
|
+ ((VertexArray.vertex[i].Y - Y <= 8) && (VertexArray.vertex[i].Y - Y >= 0)))
|
|
|
+ {
|
|
|
+ if (VertexArray.buffer[0] == null)
|
|
|
+ {
|
|
|
+ VertexArray.buffer[0] = VertexArray.vertex[i];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ VertexArray.buffer[1] = VertexArray.vertex[i];
|
|
|
+ EdgeArray.AddEdge(canvas, VertexArray.buffer[0], VertexArray.buffer[1]);
|
|
|
+ VertexArray.buffer[0] = null;
|
|
|
+ VertexArray.buffer[1] = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } // Конец проверки
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void DrawEdge(Canvas canvas, Line line, Edge edge)
|
|
|
- {
|
|
|
- line = new Line();
|
|
|
- line.Stroke = GetBrush();
|
|
|
- line.StrokeThickness = 2;
|
|
|
- line.X1 = edge.startVertex.X + 5;
|
|
|
- line.Y1 = edge.startVertex.Y + 5;
|
|
|
- line.X2 = edge.finishVertex.X + 5;
|
|
|
- line.Y2 = edge.finishVertex.Y + 5;
|
|
|
- canvas.Children.Add(line);
|
|
|
- }
|
|
|
|
|
|
- public void DrawVertex(Ellipse ellipse, Canvas canvas, Vertex vertex)
|
|
|
- {
|
|
|
- SolidColorBrush brush = new SolidColorBrush(Color.FromRgb(0, 0, 0));
|
|
|
- ellipse = new Ellipse();
|
|
|
- ellipse.Height = 10;
|
|
|
- ellipse.Width = 10;
|
|
|
- ellipse.StrokeThickness = 2;
|
|
|
- ellipse.Stroke = brush;
|
|
|
- ellipse.Fill = brush;
|
|
|
- Canvas.SetLeft(ellipse, vertex.X);
|
|
|
- Canvas.SetTop(ellipse, vertex.Y);
|
|
|
- canvas.Children.Add(ellipse);
|
|
|
- }
|
|
|
+ // Получение рандомной кисти из заданных цветов
|
|
|
Random rand = new Random((DateTime.Now.Millisecond * DateTime.Now.Second) % DateTime.Now.Hour);
|
|
|
public SolidColorBrush GetBrush()
|
|
|
{
|
|
@@ -90,9 +124,156 @@ namespace GraphDrawer
|
|
|
return brush[rand.Next(0, 8)];
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ // Очистка канваса
|
|
|
private void clearBtn_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
canvas.Children.Clear();
|
|
|
+ VertexArray.vertexCount = 0;
|
|
|
+ EdgeArray.edgeCount = 0;
|
|
|
+ Array.Clear(VertexArray.edge, 0, VertexArray.edge.Length);
|
|
|
+ Array.Clear(VertexArray.ellipse, 0, VertexArray.ellipse.Length);
|
|
|
+ Array.Clear(EdgeArray.edge, 0, EdgeArray.edge.Length);
|
|
|
+ Array.Clear(EdgeArray.line, 0, EdgeArray.line.Length);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // Дебаг-фича
|
|
|
+ private void Grid_MouseMove(object sender, MouseEventArgs e)
|
|
|
+ {
|
|
|
+ var point = e.GetPosition(canvas);
|
|
|
+ coords.Content = point.X.ToString() + "; " + point.Y.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // Кнопки меню
|
|
|
+ private void pseudoGraphBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ GraphState.supergraph = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void multiGraphBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ GraphState.supergraph = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void unfocusedGraphBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ GraphState.focused = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void focusedGraphBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ GraphState.focused = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void exportPngBtn_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ SaveFileDialog sfg = new SaveFileDialog();
|
|
|
+ sfg.Filter = "PNG (*.png) | *.png";
|
|
|
+ sfg.ShowDialog();
|
|
|
+ string filename = sfg.FileName;
|
|
|
+ Rect rect = new Rect(canvas.Margin.Left, canvas.Margin.Top, canvas.ActualWidth, canvas.ActualHeight);
|
|
|
+
|
|
|
+ double dpi = 96d;
|
|
|
+
|
|
|
+ RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right, (int)rect.Bottom, dpi, dpi, System.Windows.Media.PixelFormats.Default);
|
|
|
+
|
|
|
+ rtb.Render(canvas);
|
|
|
+
|
|
|
+ BitmapEncoder pngEncoder = new PngBitmapEncoder();
|
|
|
+ pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
|
|
+
|
|
|
+ pngEncoder.Save(ms);
|
|
|
+ ms.Close();
|
|
|
+
|
|
|
+ System.IO.File.WriteAllBytes(filename, ms.ToArray());
|
|
|
+ }
|
|
|
+ catch (Exception err)
|
|
|
+ {
|
|
|
+ MessageBox.Show(err.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class VertexArray
|
|
|
+ {
|
|
|
+ public static int vertexCount;
|
|
|
+ public static Vertex[] vertex = new Vertex[vertexCount];
|
|
|
+ public static Ellipse[] ellipse = new Ellipse[vertexCount];
|
|
|
+ public static Edge[] edge = new Edge[vertexCount];
|
|
|
+ public static Vertex[] buffer = new Vertex[2];
|
|
|
+
|
|
|
+ public static int AddVertex(Canvas canvas, int x, int y)
|
|
|
+ {
|
|
|
+
|
|
|
+ vertexCount++;
|
|
|
+ Array.Resize(ref vertex, vertexCount);
|
|
|
+ Array.Resize(ref ellipse, vertexCount);
|
|
|
+ vertex[vertexCount-1] = new Vertex(x, y);
|
|
|
+ ellipse[vertexCount-1] = new Ellipse();
|
|
|
+ ellipse[vertexCount-1].Height = 10;
|
|
|
+ ellipse[vertexCount-1].Width = 10;
|
|
|
+ ellipse[vertexCount-1].StrokeThickness = 2;
|
|
|
+ ellipse[vertexCount-1].Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
|
|
|
+ ellipse[vertexCount-1].Fill = ellipse[vertexCount-1].Stroke;
|
|
|
+ Canvas.SetTop(ellipse[vertexCount-1], vertex[vertexCount-1].Y);
|
|
|
+ Canvas.SetLeft(ellipse[vertexCount-1], vertex[vertexCount-1].X);
|
|
|
+ canvas.Children.Add(ellipse[vertexCount-1]);
|
|
|
+ return vertexCount-1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class EdgeArray
|
|
|
+ {
|
|
|
+ public static int edgeCount;
|
|
|
+ public static Edge[] edge = new Edge[edgeCount];
|
|
|
+ public static Line[] line = new Line[edgeCount];
|
|
|
+
|
|
|
+ public static void AddEdge(Canvas canvas, Vertex v1, Vertex v2)
|
|
|
+ {
|
|
|
+ if (edgeCount == 0) edgeCount++;
|
|
|
+
|
|
|
+ edgeCount++;
|
|
|
+ Array.Resize(ref edge, edgeCount);
|
|
|
+ Array.Resize(ref line, edgeCount);
|
|
|
+ line[edgeCount-1] = new Line();
|
|
|
+ edge[edgeCount-1] = new Edge(line[edgeCount-1], v1, v2);
|
|
|
+ line[edgeCount - 1].Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
|
|
|
+ line[edgeCount - 1].StrokeThickness = 2;
|
|
|
+ line[edgeCount - 1].X1 = v1.X+5;
|
|
|
+ line[edgeCount - 1].Y1 = v1.Y+5;
|
|
|
+ line[edgeCount - 1].X2 = v2.X+5;
|
|
|
+ line[edgeCount - 1].Y2 = v2.Y+5;
|
|
|
+ canvas.Children.Add(line[edgeCount - 1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class ArrowedEdgeArray
|
|
|
+ {
|
|
|
+ public static ArrowLine[] line = new ArrowLine[EdgeArray.edgeCount];
|
|
|
+ public static Edge[] edge = new Edge[EdgeArray.edgeCount];
|
|
|
+
|
|
|
+ public static void AddArrowedEdge(Canvas canvas, Vertex v1, Vertex v2)
|
|
|
+ {
|
|
|
+ int edgeCount = EdgeArray.edgeCount;
|
|
|
+ edgeCount++;
|
|
|
+ Array.Resize(ref edge, edgeCount);
|
|
|
+ Array.Resize(ref line, edgeCount);
|
|
|
+ line[edgeCount - 1] = new ArrowLine();
|
|
|
+ edge[edgeCount - 1] = new Edge(line[edgeCount - 1], v1, v2);
|
|
|
+ line[edgeCount - 1].Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
|
|
|
+ line[edgeCount - 1].StrokeThickness = 2;
|
|
|
+ line[edgeCount - 1].X1 = v1.X + 5;
|
|
|
+ line[edgeCount - 1].Y1 = v1.Y + 5;
|
|
|
+ line[edgeCount - 1].X2 = v2.X + 5;
|
|
|
+ line[edgeCount - 1].Y2 = v2.Y + 5;
|
|
|
+ canvas.Children.Add(line[edgeCount - 1]);
|
|
|
+ EdgeArray.edgeCount = edgeCount;
|
|
|
}
|
|
|
}
|
|
|
}
|