Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DistanceTask
  4. {
  5. public static class DistanceTask
  6. {
  7. // Расстояние от точки (x, y) до отрезка AB с координатами A(ax, ay), B(bx, by)
  8. public static double Distance(double x1, double x2, double y1, double y2)
  9. {
  10. return Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  11. }
  12.  
  13.  
  14. public static double GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
  15. {
  16. if (ax == ay && bx == by)
  17. {
  18. return Distance(x, ax, y, ay);
  19. }
  20. double ab = Distance(bx, ax, by, ay);
  21. double ac = Distance(x, ax, y, ay);
  22. double bc = Distance(x, bx, y, by);
  23. if (Math.Pow(ab, 2) + Math.Pow(ac, 2) <= Math.Pow(bc, 2) || Math.Pow(ab, 2) + Math.Pow(bc, 2) <= Math.Pow(ac, 2))
  24. return Math.Min(ac, bc);
  25. double p = (ab + ac + bc) / 2;
  26. double h = (2 * Math.Sqrt(p * (p - ab) * (p - ac) * (p - bc))) / ab;
  27. return h;
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement