Advertisement
gmusya

Untitled

Mar 14th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <iostream>
  5.  
  6. using i64 = long long;
  7. using i128 = __int128;
  8. using ld = long double;
  9.  
  10. const i128 INF = 1e9;
  11.  
  12. namespace {
  13. i128 Abs(i128 x) {
  14. if (x < 0) {
  15. x *= -1;
  16. }
  17. return x;
  18. }
  19. }// namespace
  20.  
  21. struct Point {
  22. i128 x, y;
  23.  
  24. friend std::istream& operator>>(std::istream& is, Point& point) {
  25. i64 a, b;
  26. is >> a >> b;
  27. point.x = a;
  28. point.y = b;
  29. return is;
  30. }
  31.  
  32. Point operator-(const Point& point) const {
  33. return {x - point.x, y - point.y};
  34. }
  35.  
  36. Point operator+(const Point& point) const {
  37. return {x + point.x, y + point.y};
  38. }
  39.  
  40. Point operator*(i128 k) const {
  41. return {x * k, y * k};
  42. }
  43.  
  44. i128 operator*(const Point& point) const {
  45. return x * point.x + y * point.y;
  46. }
  47.  
  48. i128 operator%(const Point& point) const {
  49. return x * point.y - y * point.x;
  50. }
  51. };
  52.  
  53. ld Distance_Point_Point(Point a, Point b) {
  54. return sqrt((a - b) * (a - b));
  55. }
  56.  
  57. ld Distance_Point_Segment(Point a, Point b1, Point b2) {
  58. if ((a - b1) * (b2 - b1) >= 0 && (a - b2) * (b1 - b2) >= 0) {
  59. return Abs((b1 - a) % (b1 - b2)) / Distance_Point_Point(b1, b2);
  60. }
  61. return std::min(Distance_Point_Point(a, b1), Distance_Point_Point(a, b2));
  62. }
  63.  
  64. ld Distance_Point_Ray(Point a, Point b1, Point b2) {
  65. b2 = b2 + (b2 - b1) * INF;
  66. return Distance_Point_Segment(a, b1, b2);
  67. }
  68.  
  69. ld Distance_Point_Line(Point a, Point b1, Point b2) {
  70. b2 = b2 + (b2 - b1) * INF;
  71. b1 = b2 + (b1 - b2) * 2;
  72. return Distance_Point_Segment(a, b1, b2);
  73. }
  74.  
  75. bool Intersect_Segment_Segment(Point a, Point b, Point c, Point d) {
  76. if ((a - d) % (c - d) == 0 && (b - d) % (c - d) == 0) {
  77. i128 l1, r1, l2, r2;
  78. if (a.x == b.x && a.x == c.x && a.x == d.x) {
  79. l1 = a.y;
  80. r1 = b.y;
  81. l2 = c.y;
  82. r2 = d.y;
  83. } else {
  84. l1 = a.x;
  85. r1 = b.x;
  86. l2 = c.x;
  87. r2 = d.x;
  88. }
  89. i128 l3 = std::max(l1, l2);
  90. i128 r3 = std::min(r1, r2);
  91. return l3 <= r3;
  92. }
  93. i128 value1 = (a - d) % (c - d);
  94. i128 value2 = (b - d) % (c - d);
  95. i128 value3 = (c - b) % (a - b);
  96. i128 value4 = (d - b) % (a - b);
  97. if ((value1 >= 0 && value2 <= 0) || (value1 <= 0 && value2 >= 0)) {
  98. if ((value3 >= 0 && value4 <= 0) || (value3 <= 0 && value4 >= 0)) {
  99. return true;
  100. }
  101. return false;
  102. }
  103. return false;
  104. }
  105.  
  106. ld Distance_Segment_Segment(Point a, Point b, Point c, Point d) {
  107. if (Intersect_Segment_Segment(a, b, c, d)) {
  108. return 0;
  109. }
  110. ld value1 = Distance_Point_Segment(a, c, d);
  111. ld value2 = Distance_Point_Segment(b, c, d);
  112. ld value3 = Distance_Point_Segment(c, a, b);
  113. ld value4 = Distance_Point_Segment(d, a, b);
  114. return std::min({value1, value2, value3, value4});
  115. }
  116.  
  117. ld Distance_Segment_Ray(Point a, Point b, Point c, Point d) {
  118. d = d + (d - c) * INF;
  119. return Distance_Segment_Segment(a, b, c, d);
  120. }
  121.  
  122. ld Distance_Segment_Line(Point a, Point b, Point c, Point d) {
  123. d = d + (d - c) * INF;
  124. c = d + (c - d) * 2;
  125. return Distance_Segment_Segment(a, b, c, d);
  126. }
  127.  
  128. ld Distance_Ray_Ray(Point a, Point b, Point c, Point d) {
  129. d = d + (d - c) * INF;
  130. b = b + (b - a) * INF;
  131. return Distance_Segment_Segment(a, b, c, d);
  132. }
  133.  
  134. ld Distance_Ray_Line(Point a, Point b, Point c, Point d) {
  135. b = b + (b - a) * INF;
  136. d = d + (d - c) * INF;
  137. c = d + (c - d) * 2;
  138. return Distance_Segment_Segment(a, b, c, d);
  139. }
  140.  
  141. ld Distance_Line_Line(Point a, Point b, Point c, Point d) {
  142. d = d + (d - c) * INF;
  143. c = d + (c - d) * 2;
  144. b = b + (b - a) * INF;
  145. a = b + (a - b) * 2;
  146. return Distance_Segment_Segment(a, b, c, d);
  147. }
  148. // 5.65685
  149.  
  150.  
  151. int main() {
  152. std::cout << std::fixed << std::setprecision(15);
  153. Point a, b, c, d;
  154. std::cin >> a >> b >> c >> d;
  155. std::cout << Distance_Point_Point(a, c) << std::endl;
  156. std::cout << Distance_Point_Segment(a, c, d) << std::endl;
  157. std::cout << Distance_Point_Ray(a, c, d) << std::endl;
  158. std::cout << Distance_Point_Line(a, c, d) << std::endl;
  159. std::cout << Distance_Point_Segment(c, a, b) << std::endl;
  160. std::cout << Distance_Segment_Segment(a, b, c, d) << std::endl;
  161. std::cout << Distance_Segment_Ray(a, b, c, d) << std::endl;
  162. std::cout << Distance_Segment_Line(a, b, c, d) << std::endl;
  163. std::cout << Distance_Point_Ray(c, a, b) << std::endl;
  164. std::cout << Distance_Segment_Ray(c, d, a, b) << std::endl;
  165. std::cout << Distance_Ray_Ray(a, b, c, d) << std::endl;
  166. std::cout << Distance_Ray_Line(a, b, c, d) << std::endl;
  167. std::cout << Distance_Point_Line(c, a, b) << std::endl;
  168. std::cout << Distance_Segment_Line(c, d, a, b) << std::endl;
  169. std::cout << Distance_Ray_Line(c, d, a, b) << std::endl;
  170. std::cout << Distance_Line_Line(a, b, c, d) << std::endl;
  171. return 0;
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement