Edge.cs 3.0 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 Vertex StartVertex { get; }
  10. public Vertex FinishVertex { get; }
  11. public int? Weight { get; set; }
  12. public bool IsFocused { get; }
  13. public Edge(Line line, Vertex v1, Vertex v2)
  14. {
  15. Line = line;
  16. StartVertex = v1;
  17. FinishVertex = v2;
  18. IsFocused = false;
  19. }
  20. public Edge(ArrowLine line, Vertex v1, Vertex v2)
  21. {
  22. ArrowLine = line;
  23. StartVertex = v1;
  24. FinishVertex = v2;
  25. IsFocused = true;
  26. }
  27. public Edge(Line line, Vertex v1, Vertex v2, int weight)
  28. {
  29. Line = line;
  30. StartVertex = v1;
  31. FinishVertex = v2;
  32. Weight = weight;
  33. IsFocused = false;
  34. }
  35. public Edge(ArrowLine line, Vertex v1, Vertex v2, int weight)
  36. {
  37. ArrowLine = line;
  38. StartVertex = v1;
  39. FinishVertex = 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.FinishVertex.X + edge.StartVertex.X) / 2;
  51. return x;
  52. }
  53. public static double GetCenterByY(Edge edge)
  54. {
  55. var y = (edge.FinishVertex.Y + edge.StartVertex.Y) / 2;
  56. return y;
  57. }
  58. public static bool IsCursorOnVertex(Point cursorPosition, Vertex vertex)
  59. {
  60. if (vertex.CenterByX - cursorPosition.X <= Settings.VertexWidth / 2 &&
  61. vertex.CenterByX - cursorPosition.X >= 0)
  62. {
  63. if (vertex.CenterByY - cursorPosition.Y <= Settings.VertexHeight / 2 &&
  64. vertex.CenterByY - cursorPosition.Y >= 0)
  65. {
  66. return true;
  67. }
  68. }
  69. if (cursorPosition.X - vertex.CenterByX <= Settings.VertexWidth / 2 &&
  70. cursorPosition.X - vertex.CenterByX >= 0)
  71. {
  72. if (cursorPosition.Y - vertex.CenterByY <= Settings.VertexWidth / 2 &&
  73. cursorPosition.Y - vertex.CenterByY >= 0)
  74. {
  75. return true;
  76. }
  77. return false;
  78. }
  79. return false;
  80. }
  81. public static bool IsVertexBelongToEdge(Edge edge, Vertex vertex)
  82. {
  83. if (Vertex.Compare(vertex, edge.StartVertex) || Vertex.Compare(vertex, edge.FinishVertex))
  84. return true;
  85. return false;
  86. }
  87. }
  88. }