Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 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 GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
  9. {
  10. double coss;
  11. double xx, yy;
  12. xx = bx - ax;
  13. yy = by - ay;
  14. double x1, y1;
  15. x1 = x - ax;
  16. y1 = y - ay;
  17. double a, b;
  18. a = Math.Sqrt(xx * xx + yy * yy);
  19. b = Math.Sqrt(x1 * x1 + y1 * y1);
  20. if (by == ay)
  21. {
  22. if (x >= bx)
  23. return Math.Sqrt(Math.Pow(x - bx, 2) + Math.Pow(y - by, 2));
  24. else if (x <= ax)
  25. return Math.Sqrt(Math.Pow(x - ax, 2) + Math.Pow(y - ay, 2));
  26. else
  27. {
  28. coss = Math.Abs(x1 * xx + y1 * yy) / (Math.Sqrt(x1 * x1 + y1 * y1) * Math.Sqrt(xx * xx + yy * yy));
  29. double sinn;
  30. sinn = Math.Sqrt(1 - Math.Pow(coss, 2));
  31. double S = a * b * sinn;
  32. return S / a;
  33. }
  34. }
  35. else if (ax == bx)
  36. {
  37. if ((by >= ay && y >= by) || (by <= ay && y <= by))
  38. return Math.Sqrt(Math.Pow(x - bx, 2) + Math.Pow(y - by, 2));
  39. else if ((ay >= by && y >= ay) || (ay <= by && y <= ay))
  40. return Math.Sqrt(Math.Pow(x - ax, 2) + Math.Pow(y - ay, 2));
  41. else
  42. {
  43. coss = Math.Abs(x1 * xx + y1 * yy) / (Math.Sqrt(x1 * x1 + y1 * y1) * Math.Sqrt(xx * xx + yy * yy));
  44. double sinn;
  45. sinn = Math.Sqrt(1 - Math.Pow(coss, 2));
  46. double S = a * b * sinn;
  47. return S / a;
  48. }
  49. }
  50. else
  51. {
  52. coss = Math.Abs(x1 * xx + y1 * yy) / (Math.Sqrt(x1 * x1 + y1 * y1) * Math.Sqrt(xx * xx + yy * yy));
  53. if ((coss <= 0 && bx > ax && x > bx) || (coss <= 0 && ax > bx && x < bx))
  54. return Math.Sqrt(Math.Pow(x - bx, 2) + Math.Pow(y - by, 2));
  55. else if ((coss <= 0 && ax < bx&&x<ax) || (coss <= 0 && ax > bx && x > ax))
  56. return Math.Sqrt(Math.Pow(x - ax, 2) + Math.Pow(y - ay, 2));
  57. else if (Math.Sqrt(x1 * x1 + y1 * y1) * Math.Sqrt(xx * xx + yy * yy) != 0)
  58. {
  59. coss = Math.Abs(x1 * xx + y1 * yy) / (Math.Sqrt(x1 * x1 + y1 * y1) * Math.Sqrt(xx * xx + yy * yy));
  60. double sinn;
  61. sinn = Math.Sqrt(1 - Math.Pow(coss, 2));
  62. double S = a * b * sinn;
  63. return S / a;
  64. }
  65. else
  66. return 0;
  67. }
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement