Edge.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Xml.Serialization;
  3. namespace DrawGraph
  4. {
  5. public class Edge
  6. {
  7. [XmlAttribute("source")]
  8. public Node SourceNode { get; }
  9. [XmlAttribute("target")]
  10. public Node TargetNode { get; }
  11. [XmlAttribute("weight")]
  12. public int? Weight { get; set; }
  13. public bool IsFocused { get; }
  14. public Edge(Node v1, Node v2, bool IsFocused)
  15. {
  16. SourceNode = v1;
  17. TargetNode = v2;
  18. this.IsFocused = IsFocused;
  19. }
  20. public Edge(Node sourceNode, Node targetNode, int weight, bool IsFocused)
  21. {
  22. SourceNode = sourceNode;
  23. TargetNode = targetNode;
  24. Weight = weight;
  25. this.IsFocused = IsFocused;
  26. }
  27. public static double GetCenterByX(Edge edge)
  28. {
  29. var x = (edge.TargetNode.X + edge.SourceNode.X) / 2;
  30. return x;
  31. }
  32. public static double GetCenterByY(Edge edge)
  33. {
  34. var y = (edge.TargetNode.Y + edge.SourceNode.Y) / 2;
  35. return y;
  36. }
  37. public static bool IsCursorOnNode(System.Windows.Point cursorPosition, Node Node)
  38. {
  39. try
  40. {
  41. if (Node.CenterByX - cursorPosition.X <= Settings.NodeWidth / 2 &&
  42. Node.CenterByX - cursorPosition.X >= 0)
  43. {
  44. if (Node.CenterByY - cursorPosition.Y <= Settings.NodeHeight / 2 &&
  45. Node.CenterByY - cursorPosition.Y >= 0)
  46. {
  47. return true;
  48. }
  49. }
  50. if (cursorPosition.X - Node.CenterByX <= Settings.NodeWidth / 2 &&
  51. cursorPosition.X - Node.CenterByX >= 0)
  52. {
  53. if (cursorPosition.Y - Node.CenterByY <= Settings.NodeWidth / 2 &&
  54. cursorPosition.Y - Node.CenterByY >= 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. }
  74. }