题目描述
The Tower shows atall tower perched on the top of a rocky mountain. Lightning strikes, setting the building alight, and two people leap frnm the windows, head first and arms outstretched.
It is a scene of chaos and destruction.
There is a cone tower with base center at (0, 0, 0), base radius r and apex (0, 0, h) . At time 0 , a point located at ( x0 ,y0, z0) with velocity (vx,vy,vz). What time will they collide? Here is the cone tower.
输入
The first line contains testcase number T (T≤1000), For each testcase the first line contains spaceseparated real numbers rand h (1≤r,h≤1000) the base radius and the cone height correspondingly.
For each testcase the second line contains three real numbers x0 ,y0, z0 (0≤|x0|,|y0|,z0≤1000). For each testcase the third line contains three real numbers vx,vy,vx . It is guaranteed that at time 0 the point is outside the cone and they will always collide.
输出
For each testcase print Case i: and then print the answer in one line, with absolute or relative error not exceeding 10-6
样例输入
2
1 2
1 1 1
-1.5 -1.5 -0.5
1 1
1 1 1
-1 -1 -1
样例输出
Case 1: 0.3855293381
Case 2: 0.5857864376
思路:
(
x
+
t
v
x
)
2
+
(
y
+
t
v
y
)
2
r
2
=
[
h
−
(
z
+
t
v
z
)
]
2
h
2
\frac{\left(x+t v_{x}\right)^{2}+(y+t v_y)^{2}}{r^{2}}=\frac{\left[h-\left(z+t v_{z}\right)\right]^{2}}{h^{2}}
r2(x+tvx)2+(y+tvy)2=h2[h−(z+tvz)]2
令:
K
=
h
−
z
K=h-z
K=h−z 有 ,
[
h
−
(
z
+
t
v
z
)
]
2
=
[
(
h
−
z
)
−
t
v
z
]
2
=
K
2
−
2
K
t
v
z
+
t
2
v
z
2
[h-(z+t v_ z)]^{2}=\left[(h-z)-tv_{z}\right]^{2}=K^{2}-2 K t v_{z}+t^{2} v_{z}^{2}
[h−(z+tvz)]2=[(h−z)−tvz]2=K2−2Ktvz+t2vz2
令:
C
=
r
2
h
2
C = \frac{r^{2}}{h^{2}}
C=h2r2
化简得:
x
2
+
y
2
+
2
t
(
x
v
x
+
y
v
y
)
+
t
2
(
v
x
2
+
v
y
2
)
=
C
K
2
−
2
C
K
v
z
t
+
C
t
2
v
z
2
x^{2}+y^{2}+2 t\left(x v_{x}+y v_{y}\right)+t^{2}\left(v_{x}^{2}+v_{y}^{2}\right)=C K^{2}-2 C K v_{z} t+C t^{2} v_{z}^{2}
x2+y2+2t(xvx+yvy)+t2(vx2+vy2)=CK2−2CKvzt+Ct2vz2
转换为一元二次方程求解 t
A
=
v
x
2
+
v
y
2
−
C
v
z
2
A=v_{x}^{2}+v_{y}^{2}-C v_{z}^{2}
A=vx2+vy2−Cvz2
B = 2 ∗ ( x v x + y v y + C K v z ) B=2 *( xv_{x}+y v_{y}+C K v_{z} ) B=2∗(xvx+yvy+CKvz)
C = x 2 + y 2 − C K 2 C=x^{2}+y^{2} - C K^{2} C=x2+y2−CK2
ans 取最小的 t 就可以了,大的那个 t 是第二次接触圆锥面的时间
#include<bits/stdc++.h>
using namespace std;
double h, r, x, y, z, vx, vy, vz;
int main(){
int T;
scanf("%d",&T);
for (int CS = 1; CS <= T; CS++) {
scanf("%lf%lf", &r, &h);
scanf("%lf%lf%lf", &x, &y, &z);
scanf("%lf%lf%lf", &vx, &vy, &vz);
z -= h;
double K = r/h;
double A = vx*vx+vy*vy-K*K*vz*vz;
double B = 2*(vx*x+vy*y-K*K*vz*z);
double C = x*x+y*y-K*K*z*z;
double ans = (-B - sqrt(B*B-4*A*C)) / 2 / A;
printf("Case %d: %.10lf\n", CS, ans);
}
}
注意代码里 z = z - h
公式里是 z = h - z