Edge.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Drawing;
  2. using System.Windows.Shapes;
  3. namespace DrawGraph
  4. {
  5. public class Edge
  6. {
  7. public Line Line { get; }
  8. public ArrowLine ArrowLine { get; }
  9. public Node StartNode { get; }
  10. public Node FinishNode { get; }
  11. public int? Weight { get; set; }
  12. public bool IsFocused { get; }
  13. public Edge(Line line, Node v1, Node v2)
  14. {
  15. Line = line;
  16. StartNode = v1;
  17. FinishNode = v2;
  18. IsFocused = false;
  19. }
  20. public Edge(ArrowLine line, Node v1, Node v2)
  21. {
  22. ArrowLine = line;
  23. StartNode = v1;
  24. FinishNode = v2;
  25. IsFocused = true;
  26. }
  27. public Edge(Line line, Node v1, Node v2, int weight)
  28. {
  29. Line = line;
  30. StartNode = v1;
  31. FinishNode = v2;
  32. Weight = weight;
  33. IsFocused = false;
  34. }
  35. public Edge(ArrowLine line, Node v1, Node v2, int weight)
  36. {
  37. ArrowLine = line;
  38. StartNode = v1;
  39. FinishNode = v2;
  40. IsFocused = true;
  41. Weight = weight;
  42. ArrowLine.X1 = v1.CenterByX;
  43. ArrowLine.X2 = v2.CenterByX;
  44. ArrowLine.Y1 = v1.CenterByY;
  45. ArrowLine.Y2 = v2.CenterByY;
  46. ArrowLine.StrokeThickness = 2;
  47. }
  48. public static double GetCenterByX(Edge edge)
  49. {
  50. var x = (edge.FinishNode.X + edge.StartNode.X) / 2;
  51. return x;
  52. }
  53. public static double GetCenterByY(Edge edge)
  54. {
  55. var y = (edge.FinishNode.Y + edge.StartNode.Y) / 2;
  56. return y;
  57. }
  58. public static bool IsCursorOnNode(System.Windows.Point cursorPosition, Node Node)
  59. {
  60. if (Node.CenterByX - cursorPosition.X <= Settings.NodeWidth / 2 &&
  61. Node.CenterByX - cursorPosition.X >= 0)
  62. {
  63. if (Node.CenterByY - cursorPosition.Y <= Settings.NodeHeight / 2 &&
  64. Node.CenterByY - cursorPosition.Y >= 0)
  65. {
  66. return true;
  67. }
  68. }
  69. if (cursorPosition.X - Node.CenterByX <= Settings.NodeWidth / 2 &&
  70. cursorPosition.X - Node.CenterByX >= 0)
  71. {
  72. if (cursorPosition.Y - Node.CenterByY <= Settings.NodeWidth / 2 &&
  73. cursorPosition.Y - Node.CenterByY >= 0)
  74. {
  75. return true;
  76. }
  77. return false;
  78. }
  79. return false;
  80. }
  81. public static bool IsNodeBelongToEdge(Edge edge, Node Node)
  82. {
  83. if (Node.Compare(Node, edge.StartNode) || Node.Compare(Node, edge.FinishNode))
  84. return true;
  85. return false;
  86. }
  87. }
  88. }