LineSegment2D.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. class LineSegment2D
  9. {
  10. public Vector2D P1 { get; private set; }
  11. public Vector2D P2 { get; private set; }
  12. public Vector2D Direction { get { return Line.Direction; } }
  13. public Line2D Line { get; private set; }
  14. public float Length { get; private set; }
  15. public LineSegment2D(Vector2D p1, Vector2D p2)
  16. {
  17. //endpoints
  18. P1 = p1;
  19. P2 = p2;
  20. Line = new Line2D(P1, P2 - P1);
  21. Length = P1.getDistanceTo(P2);
  22. }
  23. public bool intersectsWith(LineSegment2D ls) {
  24. Vector2D intersection = Line.getIntersection(ls.Line);
  25. return (intersection != null && intersection.isInBox(P1, P2) && intersection.isInBox(ls.P1, ls.P2));
  26. }
  27. public float getParallelDistanceTo(LineSegment2D line)
  28. {
  29. if (Line.onSide(line.P1) != Line.onSide(line.P2)) return 0;
  30. Vector2D a1 = Line.projectToLine(line.P1);
  31. Vector2D a2 = Line.projectToLine(line.P2);
  32. float distanceA1 = a1.getDistanceTo(line.P1);
  33. float distanceA2 = a2.getDistanceTo(line.P2);
  34. return Math.Min(distanceA1, distanceA2);
  35. }
  36. public float getVerticalDistanceTo(LineSegment2D line)
  37. {
  38. Vector2D a1 = Line.projectToLine(line.P1);
  39. Vector2D a2 = Line.projectToLine(line.P2);
  40. if (P1.isInBox(a1, a2) || P2.isInBox(a1, a2)) return 0;
  41. float distanceP1A1 = P1.getDistanceTo(a1);
  42. float distanceP1A2 = P1.getDistanceTo(a2);
  43. float distanceP2A1 = P2.getDistanceTo(a1);
  44. float distanceP2A2 = P2.getDistanceTo(a2);
  45. return Math.Min(Math.Min(distanceP1A1, distanceP1A2), Math.Min(distanceP2A1, distanceP2A2));
  46. }
  47. public float getDistanceTo(Vector2D point)
  48. {
  49. // http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
  50. float l2 = (P1 - P2).dotProduct(P1 - P2); // i.e. |w-v|^2 - avoid a sqrt
  51. if (l2 == 0.0)
  52. return point.getDistanceTo(P1); // v == w case
  53. // Consider the line extending the segment, parameterized as v + t (w - v).
  54. // We find projection of point p onto the line.
  55. // It falls where t = [(p-v) . (w-v)] / |w-v|^2
  56. float t = (point - P1).dotProduct(P2 - P1) / l2;
  57. if (t < 0.0)
  58. return point.getDistanceTo(P1); // Beyond the 'v' end of the segment
  59. else if (t > 1.0)
  60. return point.getDistanceTo(P2); // Beyond the 'w' end of the segment
  61. Vector2D projection = P1 + t * (P2 - P1); // Projection falls on the segment
  62. return point.getDistanceTo(projection);
  63. }
  64. public override string ToString()
  65. {
  66. return (int)P1.X + "|" + (int)P1.Y + " --- " + (int)P2.X + "|" + (int)P2.Y;
  67. }
  68. }
  69. }