Node.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Xml.Serialization;
  2. using System.Windows.Shapes;
  3. using System.Windows.Controls;
  4. using System.Collections.Generic;
  5. using System.Runtime.Serialization;
  6. using System;
  7. namespace DrawGraph
  8. {
  9. public class Node
  10. {
  11. [XmlAttribute("positionX")]
  12. public int X { get; set; }
  13. [XmlAttribute("positionY")]
  14. public int Y { get; set; }
  15. public Node(int x, int y)
  16. {
  17. X = x;
  18. Y = y;
  19. }
  20. /// <summary>
  21. /// Identifier of node (numeric)
  22. /// Returns identifier "NXX"
  23. /// Where XX - established numeric value
  24. /// </summary>
  25. /// <summary>
  26. /// Get center of node by X-coordinate
  27. /// </summary>
  28. /// <returns>Center of node by X</returns>
  29. public int GetCenterByX()
  30. {
  31. return (X * 2 + (Settings.NodeWidth / 2)) / 2;
  32. }
  33. /// <summary>
  34. /// Get center of node by Y-coordinate
  35. /// </summary>
  36. /// <returns>Center of node by Y</returns>
  37. public int GetCenterByY()
  38. {
  39. return (Y * 2 + (Settings.NodeHeight / 2)) / 2;
  40. }
  41. /// <summary>
  42. /// Compare two nodes
  43. /// </summary>
  44. /// <param name="node1">First node to compare</param>
  45. /// <param name="node2">Second node to compare</param>
  46. /// <returns>True if nodes are equal, false if not equal</returns>
  47. public static bool Compare(Node node1, Node node2)
  48. {
  49. if (Equals(node1, node2))
  50. return true;
  51. if (node1 == node2)
  52. return true;
  53. if (node1.X == node2.X && node1.Y == node2.Y)
  54. return true;
  55. return false;
  56. }
  57. /// <summary>
  58. /// Compare nodes and returns true if they are overlaid
  59. /// </summary>
  60. /// <param name="node1"></param>
  61. /// <param name="node2"></param>
  62. /// <returns>True if overlaid, false if not</returns>
  63. public static bool IsNodeOverlaid(Node node1, Node node2)
  64. {
  65. var x = node1.X - node2.X;
  66. var y = node1.Y - node2.Y;
  67. if(x<=Settings.NodeWidth/2 && x>=0)
  68. if (y <= Settings.NodeHeight / 2 && y > 0)
  69. return true;
  70. x = node2.X - node1.X;
  71. y = node2.Y - node1.Y;
  72. if (x <= Settings.NodeWidth / 2 && x >= 0)
  73. if (y <= Settings.NodeHeight / 2 && y > 0)
  74. return true;
  75. return false;
  76. }
  77. /// <summary>
  78. /// Check if node has neighbors
  79. /// </summary>
  80. /// <param name="node">Node to check</param>
  81. /// <param name="edges">Edges array</param>
  82. /// <returns></returns>
  83. public static bool HasNeighbors(Node node, Edge[] edges)
  84. {
  85. foreach(var edge in edges)
  86. {
  87. if (Compare(node, edge.SourceNode) || Compare(node, edge.TargetNode))
  88. return true;
  89. }
  90. return false;
  91. }
  92. public static bool HasNeighbors(Node node, List<Edge> edges)
  93. {
  94. var a = edges.ToArray();
  95. return HasNeighbors(node, a);
  96. }
  97. }
  98. }