1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include<iostream>
#include<math.h>
#define DB double
using namespace std;
DB dot_product_result(DB x1,DB y1, DB z1, DB x2, DB y2, DB z2)
{
DB result1= x1*x2+y1*y2+z1*z2;
return result1;
}
int main()
{
DB t,p1,p2,p3,q1,q2,q3,d1,d2,d3,c1,c2,c3,r;
cin>>t;
while(t--)
{ /*r^2 = |C - P|^2 - { (Q(t) - P).(C - P) }^2 / |Q(t) - P |^2*/
cin>>p1>>p2>>p3>>q1>>q2>>q3>>d1>>d2>>d3>>c1>>c2>>c3>>r;
//cout<<p1<<p2<<p3<<q1<<q2<<q3<<d1<<d2<<d3<<c1<<c2<<c3<<r<<endl;
/*|Q(t) - P|^2
=(Q(t) - P).(Q(t) - P)
=(Q0+td - P).(Q0+td - P)
=(Q0 - P).(Q0 - P)+2td.(Q0 - P)+t2(d.d)
=(constant1) + t (constant2)+t2(constant3)
*/
DB constant1= dot_product_result(q1-p1,q2-p2,q3-p3,q1-p1,q2-p2,q3-p3 );
DB constant2= 2*dot_product_result( q1-p1,q2-p2,q3-p3,d1,d2,d3);
DB constant3= dot_product_result(d1,d2,d3,d1,d2,d3);
//cout<<constant1<<" "<<constant2<<" "<<constant3<<endl;
/*
(Q(t) - P).(C - P)
=(Q0+td - P).(C - P)
=(Q0 - P).(C - P)+td.(C - P)
=(constant1) + t (constant2)*/
DB constant1_1= dot_product_result(q1-p1,q2-p2,q3-p3,c1-p1,c2-p2,c3-p3);
DB constant2_2= dot_product_result(d1,d2,d3,c1-p1,c2-p2,c3-p3);
//cout<<constant1_1<<" "<<constant2_2<<endl;
/*|C - P|^2 */
DB term1= dot_product_result(c1-p1,c2-p2,c3-p3,c1-p1,c2-p2,c3-p3);
/*rearrange the equation and obtain the a,b,c*/
//cout<<term1<<endl;
DB a= r*r*constant3- term1*constant3+ constant2_2*constant2_2;
DB b= r*r*constant2-term1*constant2+ 2*constant1_1*constant2_2;
DB c= r*r*constant1- term1*constant1+constant1_1*constant1_1;
// cout<<a<<" "<<b<<" "<<c<<endl;
if(a==0)
cout<<-c/b<<endl;
else {
DB time1= -b/2*a+( sqrt(b*b-4*a*c) )/2*a;
DB time2= -b/2*a-( sqrt(b*b-4*a*c) )/2*a;
DB time;
if(time1<time2)
time=time1;
else
time= time2;
//cout<<time1<<" "<<time2<<endl;
cout<<time<<endl;
}
}
}
| |