LineSegment2D.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace bbiwarg.Utility
  7. {
  8. /// <summary>
  9. /// Class to represent a segment of a line, a line between two points.
  10. /// </summary>
  11. public class LineSegment2D
  12. {
  13. /// <summary>
  14. /// first point of the lineSegment
  15. /// </summary>
  16. public Vector2D P1 { get; private set; }
  17. /// <summary>
  18. /// second point of the lineSegment
  19. /// </summary>
  20. public Vector2D P2 { get; private set; }
  21. /// <summary>
  22. /// Direction Vector of the line which contains the lineSegment
  23. /// </summary>
  24. public Vector2D Direction { get { return Line.Direction; } }
  25. /// <summary>
  26. /// Line which contains the lineSegment
  27. /// </summary>
  28. public Line2D Line { get; private set; }
  29. /// <summary>
  30. /// Length of the LineSegment
  31. /// </summary>
  32. public float Length { get; private set; }
  33. /// <summary>
  34. /// Standard contructor which sets the essential attributes
  35. /// </summary>
  36. /// <param name="p1">first point of LineSegment<see cref="P1"/></param>
  37. /// <param name="p2">second point of LineSegment<see cref="P2"/></param>
  38. public LineSegment2D(Vector2D p1, Vector2D p2)
  39. {
  40. //endpoints
  41. P1 = p1;
  42. P2 = p2;
  43. Line = new Line2D(P1, P2 - P1);
  44. Length = P1.getDistanceTo(P2);
  45. }
  46. /// <summary>
  47. /// Computes the intersection of two LineSegments, iff they do not intersect returns null
  48. /// </summary>
  49. /// <param name="ls">second LineSegment</param>
  50. /// <returns>Intersection iff its defined, else null</returns>
  51. public bool intersectsWith(LineSegment2D ls) {
  52. Vector2D intersection = Line.getIntersection(ls.Line);
  53. return (intersection != null && intersection.isInBox(P1, P2) && intersection.isInBox(ls.P1, ls.P2));
  54. }
  55. /// <summary>
  56. /// Computes the Distance of two parallel LineSegments, iff they are not parallel it returns 0
  57. /// </summary>
  58. /// <param name="line">second LineSegment</param>
  59. /// <returns>Distance between the LineSegment</returns>
  60. public float getParallelDistanceTo(LineSegment2D line)
  61. {
  62. if (Line.onSide(line.P1) != Line.onSide(line.P2)) return 0;
  63. Vector2D a1 = Line.projectToLine(line.P1);
  64. Vector2D a2 = Line.projectToLine(line.P2);
  65. float distanceA1 = a1.getDistanceTo(line.P1);
  66. float distanceA2 = a2.getDistanceTo(line.P2);
  67. return Math.Min(distanceA1, distanceA2);
  68. }
  69. /// <summary>
  70. /// Computes the vertical distance between two lineSegments. the vertical distance is the minimal distance between the LineSegments after they are projected on a horizontal line.
  71. /// </summary>
  72. /// <param name="line">second LineSegment</param>
  73. /// <returns>the vertical distance</returns>
  74. public float getVerticalDistanceTo(LineSegment2D line)
  75. {
  76. Vector2D a1 = Line.projectToLine(line.P1);
  77. Vector2D a2 = Line.projectToLine(line.P2);
  78. if (P1.isInBox(a1, a2) || P2.isInBox(a1, a2)) return 0;
  79. float distanceP1A1 = P1.getDistanceTo(a1);
  80. float distanceP1A2 = P1.getDistanceTo(a2);
  81. float distanceP2A1 = P2.getDistanceTo(a1);
  82. float distanceP2A2 = P2.getDistanceTo(a2);
  83. return Math.Min(Math.Min(distanceP1A1, distanceP1A2), Math.Min(distanceP2A1, distanceP2A2));
  84. }
  85. /// <summary>
  86. /// Computes the shortest distance of a point to the LineSegment
  87. /// </summary>
  88. /// <param name="point">the point for distance calculation</param>
  89. /// <returns>the distance</returns>
  90. public float getDistanceTo(Vector2D point)
  91. {
  92. // http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
  93. float l2 = (P1 - P2).dotProduct(P1 - P2); // i.e. |w-v|^2 - avoid a sqrt
  94. if (l2 == 0.0)
  95. return point.getDistanceTo(P1); // v == w case
  96. // Consider the line extending the segment, parameterized as v + t (w - v).
  97. // We find projection of point p onto the line.
  98. // It falls where t = [(p-v) . (w-v)] / |w-v|^2
  99. float t = (point - P1).dotProduct(P2 - P1) / l2;
  100. if (t < 0.0)
  101. return point.getDistanceTo(P1); // Beyond the 'v' end of the segment
  102. else if (t > 1.0)
  103. return point.getDistanceTo(P2); // Beyond the 'w' end of the segment
  104. Vector2D projection = P1 + t * (P2 - P1); // Projection falls on the segment
  105. return point.getDistanceTo(projection);
  106. }
  107. public override string ToString()
  108. {
  109. return (int)P1.X + "|" + (int)P1.Y + " --- " + (int)P2.X + "|" + (int)P2.Y;
  110. }
  111. }
  112. }