fork(2) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define mp(a,b) make_pair(a,b)
  4. #define ff first
  5. #define setp(x) setprecision(x)<<fixed
  6. #define ss second
  7. #define fori(v) for(lli i=0; i<v; i++)
  8. #define forj(v) for(int j=0; j<v; j++)
  9. #define fork(v) for(int k=0; k<v; k++)
  10. #define forl(v) for(int l=0; l<v; l++)
  11. #define fort(v) for(int t=0; t<v; t++)
  12. #define forz(v) for(int z=0; z<v; z++)
  13. #define lli long long int
  14. #define MAX 220020
  15. #define double long double
  16. int inf = pow(10,9);
  17. lli modulo = pow(10,9)+7;
  18. double eps = 1e-11;
  19. ifstream in;
  20. ofstream out;
  21. //#define cin in
  22. //#define cout out
  23. struct point{
  24. double x,y,z;
  25. point(){}
  26. point(double arr[3]) : x(arr[0]) , y(arr[1]), z(arr[2]){}
  27. point(double x1,double y1,double z1) : x(x1), y(y1) , z(z1){}
  28. };
  29. struct sphere{
  30. point centre;
  31. double radius;
  32. sphere(){ }
  33. sphere(point a, double r) : centre(a), radius(r){}
  34. };
  35. struct vect{
  36. double x,y,z;
  37. vect(){ }
  38. vect(double x1,double y1, double z1) : x(x1) , y(y1), z(z1){}
  39. vect(point a1,point b1): x(b1.x-a1.x) , y(b1.y - a1.y ) , z(b1.z - a1.z){ }
  40. vect(double c, vect other) : x(other.x * c) , y(other.y * c) , z(other.z * c){}
  41. };
  42. double dot_product(vect a, vect b){
  43. return a.x*b.x + a.y*b.y + a.z * b.z;
  44. }
  45. vect subtract(vect a, vect b){ // subtract b from a
  46. return vect(a.x-b.x , a.y - b.y , a.z-b.z);
  47. }
  48. double leng(vect a){
  49. return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
  50. }
  51. double find_distance(point a1, point b1, point c1){ // find distance from point a1 to the line defined by b1 and c1
  52. vect a(b1,c1);
  53. vect b(b1,a1);
  54. double constant = dot_product(a,b)/dot_product(a,a);
  55. vect c(constant,a);
  56. return leng(subtract(b,c));
  57. }
  58. void deal(){
  59. cin.tie(0);
  60. ios_base::sync_with_stdio(0);
  61. int t;
  62. cin>>t;
  63. forl(t){
  64. double xyz[3][3];
  65. fori(2)
  66. forj(3)
  67. cin>>xyz[i][j];
  68. double mv[3];
  69. fori(3)
  70. cin>>mv[i];
  71. fori(3)
  72. cin>>xyz[2][i];
  73. double r;
  74. cin>>r;
  75. point still(xyz[0]),move(xyz[1]),cent(xyz[2]);
  76. sphere s(cent,r);
  77. double a = 0 , b = pow(10,9);
  78. while((b-a)>eps){
  79. double c = (a+b)/2;
  80. double x1 = move.x + c*mv[0], y1 = move.y + c*mv[1], z1 = move.z + c*mv[2];
  81. double distance = find_distance(s.centre,still,point(x1,y1,z1));
  82. if(distance > s.radius)
  83. b=c;
  84. else
  85. a=c;
  86. }
  87. cout<<setp(6)<<a<<'\n';
  88. }
  89. }
  90. int main() {
  91. deal();
  92. }
Success #stdin #stdout 0s 4412KB
stdin
1
3 0 0 -10 -10 0 0 10 0 0 -3 0 3
stdout
1.000000