fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5. #define sz(s) (int)(s).size()
  6. #define all(s) s.begin(),s.end()
  7.  
  8. void Speed() {
  9. ios_base::sync_with_stdio(false);
  10. cin.tie(NULL);
  11. }
  12.  
  13. using point = complex<double>;
  14. istream& operator>>(istream& is, point& p) {
  15. int x, y; // check this
  16. is >> x >> y;
  17. p = point(x, y);
  18. return is;
  19. }
  20.  
  21. #define PI acos(-1.0L)
  22. #define EPS 1e-8
  23. #define X real()
  24. #define Y imag()
  25. #define length(a) (abs(a))
  26. #define dp(a, b) ((conj(a) * (b)).X)
  27. #define cp(a, b) ((conj(a) * (b)).Y)
  28. #define vec(a,b) ((b)-(a))
  29. #define norm(a) ((a)/length(a))
  30. #define reflectO(v, m) (conj((v) / (m)) * (m))
  31. #define reflect(v, p1, p2) (reflectO(v - p1, p2 - p1) + p1)
  32. #define rotateO(p, ang) ((p) * exp(point(0, ang)))
  33. #define rotate(p, ang, about) (rotateO(p - about, ang) + about)
  34.  
  35. inline int dcomp(double a, double b) {
  36. return fabs(a - b) < EPS ? 0 : (a > b ? 1 : -1);
  37. }
  38.  
  39. double angle(const point& a) {
  40. double theta = arg(a);
  41. return (theta < 0 ? (theta + 2 * PI) : theta);
  42. }
  43.  
  44.  
  45.  
  46. bool intersectSegments(const point& a, const point& b, const point& c, const point& d) {
  47. double d1 = cp(vec(b, a), vec(c, d)), d2 = cp(vec(c, a), vec(c, d)), d3 = cp(vec(b, a), vec(c, a));
  48. if (fabs(d1) < EPS)
  49. return false; // Parllel || identical
  50. double t1 = d2 / d1, t2 = d3 / d1;
  51. // point inter = point(a, (b - a) * t1);
  52. if (t1 < -EPS || t1 > 1 + EPS || t2 < -EPS || t2 > 1 + EPS)
  53. return 0;
  54. return 1;
  55. }
  56.  
  57. bool intersectLines(double A1, double B1, double C1, double A2, double B2, double C2){
  58. double s = A1 * B2 - A2 * B1;
  59. if (fabs(s) < EPS) return 0;
  60. // double x = (-(B2 * C1 - B1 * C2) / (A1 * B2 - A2 * B1));
  61. // double y = ((A2 * C1 - A1 * C2) / (A1 * B2 - A2 * B1));
  62. // inter = point(x, y);
  63. return 1;
  64. }
  65.  
  66. bool intersectRays(const point& a, const point& b, const point& c, const point& d) {
  67. double d1 = cp(vec(b, a), vec(c, d)), d2 = cp(vec(c, a), vec(c, d)), d3 = cp(vec(b, a), vec(c, a));
  68. if (fabs(d1) < EPS)
  69. return 0; // Parllel || identical
  70. double t1 = d2 / d1, t2 = d3 / d1;
  71. // point inter = a + (b - a) * t1);
  72. if (t1 < -EPS || t2 < -EPS)
  73. return 0;
  74. return 1;
  75. }
  76.  
  77.  
  78.  
  79. double distToLine(const point& v, const point& p1, const point& p2) {
  80. return fabs(cp(vec(v, p1), vec(v, p2)) / length(vec(p1, p2)));
  81. }
  82.  
  83. double distToRay(const point& v, const point& p1, const point& p2){
  84. if(dp(vec(p1, v), vec(p1, p2)) < 0) return length(vec(v, p1));
  85. return distToLine(v, p1, p2);
  86. }
  87.  
  88. double distToSeg(const point& v, const point& p1, const point& p2) {
  89. double d1, d2;
  90. const point& v1 = vec(p1, p2), v2 = vec(p1, v);
  91. if ((d1 = dp(v1, v2)) <= 0) return length(vec(v, p1));
  92. if ((d2 = dp(v1, v1)) <= d1) return length(vec(v, p2));
  93. double t = d1 / d2;
  94. return length(vec((p1 + v1 * t), v));
  95. }
  96.  
  97. double distSegments(const point& a, const point& b, const point& c, const point& d) {
  98. if(intersectSegments(a,b,c,d)) return 0;
  99. return min({distToSeg(a,c,d), distToSeg(b,c,d), distToSeg(c,a,b), distToSeg(d,a,b)});
  100. }
  101.  
  102. double distRays(const point& a, const point& b, const point& c, const point& d) {
  103. if(intersectRays(a,b,c,d)) return 0;
  104. return min(distToRay(a, c, d), distToRay(c, a, b));
  105. }
  106.  
  107. array<double, 3> coffLine(const point& a, const point& b){
  108. double A = a.Y - b.Y;
  109. double B = b.X - a.X;
  110. double C = cp(a, b);
  111. if(A < 0) A *= -1, B *= -1, C *= -1;
  112. return {A, B, C};
  113. }
  114.  
  115. array<point, 2> get_points_on_line(double A, double B, double C){
  116. if(dcomp(A, 0) == 0){
  117. double y = -C / B;
  118. return {point({0, y}), point({1, y})};
  119. }
  120. return {point({-C / A, 0}), point({-(C + B) / A, 1})};
  121. }
  122.  
  123. bool isCollinear(const point& a, const point& b, const point& p) {
  124. return fabs(cp(vec(a, b), vec(a, p))) < EPS;
  125. }
  126.  
  127. bool isPointOnRay(const point& a, const point& b, const point& p) {
  128. if(length(vec(a, p)) < EPS) return 1;
  129. return isCollinear(a, b, p) && dcomp(dp(vec(a, b), vec(a, p)), 0) == 1;
  130. }
  131.  
  132. bool isPointOnSegment(const point& a, const point& b, const point& p) {
  133. return isPointOnRay(a, b, p) && isPointOnRay(b, a, p);
  134. }
  135.  
  136. void solve(){
  137. point a, b, c, d; cin >> a >> b >> c >> d;
  138. cout << distRays(a, b, c, d) << '\n';
  139. }
  140.  
  141. int main(){
  142. freopen("raydist.in", "r", stdin);
  143. freopen("raydist.out", "w", stdout);
  144. cout << fixed << setprecision(10);
  145. Speed();
  146. int tc = 1;
  147. //cin >> tc;
  148. while (tc--) {
  149. solve();
  150. }
  151. return 0;
  152. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty