Edge.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Runtime.Serialization;
  3. using System.Xml.Serialization;
  4. namespace DrawGraph
  5. {
  6. [DataContract]
  7. public class Edge
  8. {
  9. [XmlAttribute("source")]
  10. public Node SourceNode { get; }
  11. [XmlAttribute("target")]
  12. public Node TargetNode { get; }
  13. [XmlAttribute("weight")]
  14. public int? Weight { get; set; }
  15. public bool IsFocused { get; }
  16. public Edge(Node v1, Node v2, bool IsFocused)
  17. {
  18. SourceNode = v1;
  19. TargetNode = v2;
  20. this.IsFocused = IsFocused;
  21. }
  22. public Edge(Node sourceNode, Node targetNode, int weight, bool IsFocused)
  23. {
  24. SourceNode = sourceNode;
  25. TargetNode = targetNode;
  26. Weight = weight;
  27. this.IsFocused = IsFocused;
  28. }
  29. public double GetCenterByX()
  30. {
  31. return (TargetNode.X + SourceNode.X) / 2;
  32. }
  33. public double GetCenterByY()
  34. {
  35. return (TargetNode.Y + SourceNode.Y) / 2;
  36. }
  37. public static bool IsCursorOnNode(System.Windows.Point cursorPosition, Node Node)
  38. {
  39. try
  40. {
  41. if (Node.GetCenterByX() - cursorPosition.X <= Settings.NodeWidth / 2 &&
  42. Node.GetCenterByX() - cursorPosition.X >= 0)
  43. {
  44. if (Node.GetCenterByY() - cursorPosition.Y <= Settings.NodeHeight / 2 &&
  45. Node.GetCenterByY() - cursorPosition.Y >= 0)
  46. {
  47. return true;
  48. }
  49. }
  50. if (cursorPosition.X - Node.GetCenterByX() <= Settings.NodeWidth / 2 &&
  51. cursorPosition.X - Node.GetCenterByX() >= 0)
  52. {
  53. if (cursorPosition.Y - Node.GetCenterByY() <= Settings.NodeWidth / 2 &&
  54. cursorPosition.Y - Node.GetCenterByY() >= 0)
  55. {
  56. return true;
  57. }
  58. return false;
  59. }
  60. return false;
  61. }
  62. catch(Exception ex)
  63. {
  64. throw new Exception(ex.Message);
  65. }
  66. }
  67. public static bool IsNodeBelongToEdge(Edge edge, Node Node)
  68. {
  69. if (Node.Compare(Node, edge.SourceNode) || Node.Compare(Node, edge.TargetNode))
  70. return true;
  71. return false;
  72. }
  73. public static bool Compare(Edge edge1, Edge edge2)
  74. {
  75. if (Equals(edge1, edge2))
  76. return true;
  77. if (Node.Compare(edge1.SourceNode, edge2.SourceNode) && Node.Compare(edge1.TargetNode, edge2.TargetNode))
  78. return true;
  79. return false;
  80. }
  81. public static bool Exists(Edge[] edges, Edge edge)
  82. {
  83. foreach(var e in edges)
  84. {
  85. if (Compare(e, edge))
  86. return true;
  87. }
  88. return false;
  89. }
  90. }
  91. }