Bladeren bron

IsVertexOverlaid

Anton 5 jaren geleden
bovenliggende
commit
a3c9d95d13
8 gewijzigde bestanden met toevoegingen van 127 en 86 verwijderingen
  1. 1 1
      DrawGraph/Algorithms.cs
  2. 2 2
      DrawGraph/DrawGraph.csproj
  3. 27 27
      DrawGraph/Edge.cs
  4. 27 1
      DrawGraph/Export.cs
  5. 19 19
      DrawGraph/Matrix.cs
  6. 45 0
      DrawGraph/Node.cs
  7. 6 6
      DrawGraph/Settings.cs
  8. 0 30
      DrawGraph/Vertex.cs

+ 1 - 1
DrawGraph/Algorithms.cs

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace DrawGraph
 {
-    public class Algorithms
+    public class Algorithm
     {
         public static int MinDistance(int[] path, bool[] includedToPath, int n)
         {

+ 2 - 2
DrawGraph/DrawGraph.csproj

@@ -45,7 +45,7 @@
     <Reference Include="WindowsBase" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Algorithms.cs" />
+    <Compile Include="Algorithm.cs" />
     <Compile Include="ArrowLine.cs" />
     <Compile Include="ArrowLineBase.cs" />
     <Compile Include="Edge.cs" />
@@ -54,7 +54,7 @@
     <Compile Include="Matrix.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Settings.cs" />
-    <Compile Include="Vertex.cs" />
+    <Compile Include="Node.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 27 - 27
DrawGraph/Edge.cs

@@ -7,40 +7,40 @@ namespace DrawGraph
     {
         public Line Line { get; }
         public ArrowLine ArrowLine { get; }
-        public Vertex StartVertex { get; }
-        public Vertex FinishVertex { get; }
+        public Node StartNode { get; }
+        public Node FinishNode { get; }
         public int? Weight { get; set; }
         public bool IsFocused { get; }
 
-        public Edge(Line line, Vertex v1, Vertex v2)
+        public Edge(Line line, Node v1, Node v2)
         {
             Line = line;
-            StartVertex = v1;
-            FinishVertex = v2;
+            StartNode = v1;
+            FinishNode = v2;
             IsFocused = false;
         }
 
-        public Edge(ArrowLine line, Vertex v1, Vertex v2)
+        public Edge(ArrowLine line, Node v1, Node v2)
         {
             ArrowLine = line;
-            StartVertex = v1;
-            FinishVertex = v2;
+            StartNode = v1;
+            FinishNode = v2;
             IsFocused = true;
         }
 
-        public Edge(Line line, Vertex v1, Vertex v2, int weight)
+        public Edge(Line line, Node v1, Node v2, int weight)
         {
             Line = line;
-            StartVertex = v1;
-            FinishVertex = v2;
+            StartNode = v1;
+            FinishNode = v2;
             Weight = weight;
             IsFocused = false;
         }
-        public Edge(ArrowLine line, Vertex v1, Vertex v2, int weight)
+        public Edge(ArrowLine line, Node v1, Node v2, int weight)
         {
             ArrowLine = line;
-            StartVertex = v1;
-            FinishVertex = v2;
+            StartNode = v1;
+            FinishNode = v2;
             IsFocused = true;
             Weight = weight;
 
@@ -53,33 +53,33 @@ namespace DrawGraph
 
         public static double GetCenterByX(Edge edge)
         {
-            var x = (edge.FinishVertex.X + edge.StartVertex.X) / 2;
+            var x = (edge.FinishNode.X + edge.StartNode.X) / 2;
             return x;
         }
 
         public static double GetCenterByY(Edge edge)
         {
-            var y = (edge.FinishVertex.Y + edge.StartVertex.Y) / 2;
+            var y = (edge.FinishNode.Y + edge.StartNode.Y) / 2;
             return y;
         }
 
-        public static bool IsCursorOnVertex(Point cursorPosition, Vertex vertex)
+        public static bool IsCursorOnNode(System.Windows.Point cursorPosition, Node Node)
         {
-                if (vertex.CenterByX - cursorPosition.X <= Settings.VertexWidth / 2 &&
-                    vertex.CenterByX - cursorPosition.X >= 0)
+                if (Node.CenterByX - cursorPosition.X <= Settings.NodeWidth / 2 &&
+                    Node.CenterByX - cursorPosition.X >= 0)
                 {
-                    if (vertex.CenterByY - cursorPosition.Y <= Settings.VertexHeight / 2 &&
-                        vertex.CenterByY - cursorPosition.Y >= 0)
+                    if (Node.CenterByY - cursorPosition.Y <= Settings.NodeHeight / 2 &&
+                        Node.CenterByY - cursorPosition.Y >= 0)
                     {
                         return true;
                     }
                 }
 
-                if (cursorPosition.X - vertex.CenterByX <= Settings.VertexWidth / 2 &&
-                    cursorPosition.X - vertex.CenterByX >= 0)
+                if (cursorPosition.X - Node.CenterByX <= Settings.NodeWidth / 2 &&
+                    cursorPosition.X - Node.CenterByX >= 0)
                 {
-                    if (cursorPosition.Y - vertex.CenterByY <= Settings.VertexWidth / 2 &&
-                        cursorPosition.Y - vertex.CenterByY >= 0)
+                    if (cursorPosition.Y - Node.CenterByY <= Settings.NodeWidth / 2 &&
+                        cursorPosition.Y - Node.CenterByY >= 0)
                     {
                         return true;
                     }
@@ -90,9 +90,9 @@ namespace DrawGraph
                 return false;
         }
 
-        public static bool IsVertexBelongToEdge(Edge edge, Vertex vertex)
+        public static bool IsNodeBelongToEdge(Edge edge, Node Node)
         {
-            if (Vertex.Compare(vertex, edge.StartVertex) || Vertex.Compare(vertex, edge.FinishVertex))
+            if (Node.Compare(Node, edge.StartNode) || Node.Compare(Node, edge.FinishNode))
                 return true;
 
             return false;

+ 27 - 1
DrawGraph/Export.cs

@@ -2,11 +2,13 @@
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Media;
+using System.Xml;
 using System.Windows.Media.Imaging;
+using Microsoft.Win32;
 
 namespace DrawGraph
 {
-    class Export
+    public class Export
     {
         public static void ToPng(Canvas canvas, string path)
         {
@@ -55,5 +57,29 @@ namespace DrawGraph
                 pd.PrintVisual(canvas, "Printed with DrawGraph");
             }
         }
+
+        public static void ToGraphML(Node[] nodes)
+        {
+            //<? xml version = "1.0" encoding = "UTF-8" ?>
+            //    < graphml xmlns = "http://graphml.graphdrawing.org/xmlns"
+            //xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
+            //xsi: schemaLocation = "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd" >
+            XmlDocument doc = new XmlDocument();
+            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
+            doc.AppendChild(declaration);
+            XmlProcessingInstruction pi =
+                doc.CreateProcessingInstruction("graphml", "xmlns=\"http://graphml.graphdrawing.org/xmlns \" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance \" xsi: schemaLocation = \"http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd \"");
+            SaveFileDialog sfd = new SaveFileDialog();
+            var root = doc.CreateElement("graph");
+            foreach (var node in nodes)
+            {
+                var XNode = doc.CreateElement("node");
+            }
+            sfd.Filter = "GraphML files (*.graphml)|*.graphml";
+            if (sfd.ShowDialog() == true)
+            {
+                doc.Save(sfd.FileName);
+            }
+        }
     }
 }

+ 19 - 19
DrawGraph/Matrix.cs

@@ -2,38 +2,38 @@
 {
     public class Matrix
     {
-        public static int[,] AdjacencyCreate(Vertex[] vertices, Edge[] edges)
+        public static int[,] AdjacencyCreate(Node[] nodes, Edge[] edges)
         {
-            int length = vertices.Length;
+            int length = nodes.Length;
             int[,] matrix = new int[length, length];
-            for (int i = 0; i < vertices.Length; i++)
+            for (int i = 0; i < nodes.Length; i++)
             {
-                var vertex1 = vertices[i];
-                for (int j = 0; j < vertices.Length; j++)
+                var Node1 = nodes[i];
+                for (int j = 0; j < nodes.Length; j++)
                 {
                     if (i != j)
                     {
-                        var vertex2 = vertices[j];
+                        var Node2 = nodes[j];
                         for (int k = 0; k < edges.Length; k++)
                         {
                             var edge = edges[k];
                             if (edge.IsFocused)
                             {
-                                if (Vertex.Compare(edge.FinishVertex, vertex1) &&
-                                    Vertex.Compare(edge.StartVertex, vertex2))
+                                if (Node.Compare(edge.FinishNode, Node1) &&
+                                    Node.Compare(edge.StartNode, Node2))
                                     if (edge.Weight > 0)
                                         matrix[i, j] = (int) edge.Weight;
                                     else
                                         matrix[i, j] = 1;
-                                else if (Vertex.Compare(edge.StartVertex, vertex1) &&
-                                         Vertex.Compare(edge.FinishVertex, vertex2))
+                                else if (Node.Compare(edge.StartNode, Node1) &&
+                                         Node.Compare(edge.FinishNode, Node2))
                                     if (edge.Weight > 0)
                                         matrix[i, j] = (int) edge.Weight;
                             }
                             else
                             {
-                                if (Vertex.Compare(edge.FinishVertex, vertex1) &&
-                                    Vertex.Compare(edge.StartVertex, vertex2))
+                                if (Node.Compare(edge.FinishNode, Node1) &&
+                                    Node.Compare(edge.StartNode, Node2))
                                     if (edge.Weight > 0)
                                     {
                                         matrix[i, j] = (int) edge.Weight;
@@ -44,8 +44,8 @@
                                         matrix[i, j] = 1;
                                         InvertElement(matrix, i, j);
                                     }
-                                else if (Vertex.Compare(edge.StartVertex, vertex1) &&
-                                         Vertex.Compare(edge.FinishVertex, vertex2))
+                                else if (Node.Compare(edge.StartNode, Node1) &&
+                                         Node.Compare(edge.FinishNode, Node2))
                                     if (edge.Weight > 0)
                                     {
                                         matrix[i, j] = (int) edge.Weight;
@@ -73,25 +73,25 @@
             matrix[indexY, indexX] = matrix[indexX, indexY];
             return matrix;
         }
-        public static int[,] IncidenceCreate(Vertex[] vertices, Edge[] edges)
+        public static int[,] IncidenceCreate(Node[] nodes, Edge[] edges)
         {
-            var rows = vertices.Length;
+            var rows = nodes.Length;
             var cols = edges.Length;
             var matrix = new int[rows, cols];
             for (var i = 0; i < rows; i++)
             {
-                var vertex = vertices[i];
+                var Node = nodes[i];
 
                 for (var j = 0; j < cols; j++)
                 {
                     var edge = edges[j];
 
 
-                    if (Vertex.Compare(vertex, edge.FinishVertex) && edge.IsFocused)
+                    if (Node.Compare(Node, edge.FinishNode) && edge.IsFocused)
                         matrix[i, j] = -1;
                     else
                     {
-                        if (Vertex.Compare(vertex, edge.FinishVertex) || Vertex.Compare(vertex, edge.StartVertex))
+                        if (Node.Compare(Node, edge.FinishNode) || Node.Compare(Node, edge.StartNode))
                             if (edge.Weight > 0)
                                 matrix[i, j] = (int)edge.Weight;
                             else

+ 45 - 0
DrawGraph/Node.cs

@@ -0,0 +1,45 @@
+namespace DrawGraph
+{
+    public class Node
+    {
+        public int X { get; set; }
+        public int Y { get; set; }
+
+        public Node(int x, int y)
+        {
+            X = x;
+            Y = y;
+        }
+
+        public int CenterByX => (X * 2 + (Settings.NodeWidth/2)) / 2;
+
+        public int CenterByY => (Y * 2 + (Settings.NodeHeight / 2)) / 2;
+
+        public static bool Compare(Node v1, Node v2)
+        {
+            if (Equals(v1, v2))
+                return true;
+            if (v1 == v2)
+                return true;
+            if (v1.X == v2.X && v1.Y == v2.Y)
+                return true;
+            return false;
+        }
+
+        public static bool IsNodeOverlaid(Node v1, Node v2)
+        {
+            var x = v1.X - v2.X;
+            var y = v1.Y - v2.Y;
+            if(x<=Settings.NodeWidth/2 && x>=0)
+                if (y <= Settings.NodeHeight / 2 && y > 0)
+                    return true;
+            x = v2.X - v1.X;
+            y = v2.Y - v1.Y;
+            if (x <= Settings.NodeWidth / 2 && x >= 0)
+                if (y <= Settings.NodeHeight / 2 && y > 0)
+                    return true;
+            return false;
+        }
+
+    }
+}

+ 6 - 6
DrawGraph/Settings.cs

@@ -7,18 +7,18 @@ namespace DrawGraph
 {
     public class Settings
     {
-        public static int VertexWidth = 10;
-        public static int VertexHeight = 10;
+        public static int NodeWidth = 10;
+        public static int NodeHeight = 10;
         public static int StrokeThickness = 2;
         public static int FontSize = 16;
-        public static Color FillColor = Color.FromRgb(0, 0, 0);
+        public static Brush FillColor = Brushes.Black;
 
-        public static void ClearCanvas(Edge[] edges, Vertex[] vertices)
+        public static void ClearCanvas(Edge[] edges, Node[] nodes)
         {
-            if (edges != null && vertices != null)
+            if (edges != null && nodes != null)
             {
                 Array.Clear(edges, 0, edges.Length);
-                Array.Clear(vertices, 0, vertices.Length);
+                Array.Clear(nodes, 0, nodes.Length);
             }
             else
             {

+ 0 - 30
DrawGraph/Vertex.cs

@@ -1,30 +0,0 @@
-namespace DrawGraph
-{
-    public class Vertex
-    {
-        public int X { get; set; }
-        public int Y { get; set; }
-
-        public Vertex(int x, int y)
-        {
-            X = x;
-            Y = y;
-        }
-
-        public int CenterByX => (X * 2 + 10) / 2;
-
-        public int CenterByY => (Y * 2 + 10) / 2;
-
-        public static bool Compare(Vertex v1, Vertex v2)
-        {
-            if (Equals(v1, v2))
-                return true;
-            if (v1 == v2)
-                return true;
-            if (v1.X == v2.X && v1.Y == v2.Y)
-                return true;
-            return false;
-        }
-
-    }
-}