fork(1) download
  1. import java.io.*;
  2. import java.util.*;
  3. class CLMNTR{
  4. static class Sphere{
  5. int x, y, z, r;
  6. Sphere(int a, int b, int c, int d){
  7. x = a;
  8. y = b;
  9. z = c;
  10. r = d;
  11. }
  12.  
  13. static double getDist(Sphere a, Sphere b){
  14. return Math.sqrt(Math.pow(a.x-b.x,2)+Math.pow(a.y-b.y,2)+Math.pow(a.z-b.z,2));
  15. }
  16. }
  17.  
  18. public static void main(String args[])throws Exception{
  19. Scanner sc = new Scanner(System.in);
  20. int T = sc.nextInt();
  21. for(; T>0; T--){
  22. Sphere s = new Sphere(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
  23. Sphere a = new Sphere(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
  24. Sphere b = new Sphere(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
  25.  
  26. if(check(s, a, b))
  27. System.out.println("Yes");
  28. else
  29. System.out.println("No");
  30. }
  31. }
  32.  
  33. static boolean check(Sphere s, Sphere a, Sphere b){
  34. // Scale to 2d with star at (0,0), body A on +ve Y-axis at (0,sa), body B at (bx,by)
  35. double sa = Sphere.getDist(s, a);
  36. double sb = Sphere.getDist(s, b);
  37. double ab = Sphere.getDist(a, b);
  38. double by = (sa*sa-ab*ab+sb*sb)/(2*sa);
  39. double bx = Math.sqrt(sb*sb-by*by);
  40. // If B is under X axis or B is closer to S than A no way there is eclipse
  41. if(by<0 || sb<sa)
  42. return false;
  43. // B always lies in the first quadrant!!
  44.  
  45. // Exceptional case when star radius = radius of A, tangent is vertical
  46. if(s.r==a.r)
  47. if(bx<s.r || b.x-s.r<b.r)
  48. return true;
  49. else
  50. return false;
  51.  
  52. // Find the external tangent y = mx+c
  53. // Internal tangent c = s.r*s.a/(s.r+a.r)
  54. double c = s.r*sa/(s.r-a.r);
  55. double m = Math.sqrt((c*c/s.r/s.r)-1);
  56. // If A is smaller than star, c is +ve and we need tangent with -ve slope
  57. if(c>0)
  58. m = -m;
  59.  
  60. // If A is smaller than star, this check eliminates false +ves from antumbra cases
  61. if(c>0 && c<sb-b.r)
  62. return false;
  63.  
  64. // Then B surely lies between tangent and Y-axis
  65. double lx = (by-c)/m;
  66. if(bx<lx)
  67. return true;
  68.  
  69. // If dist from tangent < radius then body intersects tangent
  70. double d = Math.abs(m*bx-by+c)/Math.sqrt(m*m+1);
  71. if(d<b.r)
  72. return true;
  73. return false;
  74. }
  75. }
Success #stdin #stdout 0.15s 321280KB
stdin
1
0 0 0 10
50 0 0 5
65 0 0 8
stdout
Yes