hdu6559 The Tower(解析几何)

Problem Description

The Tower shows a tall tower perched on the top of a rocky mountain. Lightning strikes, setting the building alight, and two people leap from 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.

Input

The first line contains testcase number T (T ≤ 1000), For each testcase the first line contains spaceseparated real numbers r and 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, vz(1 ≤ v2x + v2y + v2z ≤ 3 × 106). It is guaranteed that at time 0 the point is outside the cone and they will always collide.

Output

For each testcase print Case i : and then print the answer in one line, with absolute or relative error not exceeding 10−6

Sample Input

2 1 2 1 1 1 -1.5 -1.5 -0.5 1 1 1 1 1 -1 -1 -1

Sample Output

Case 1: 0.3855293381 Case 2: 0.5857864376

题意:初始坐标为(x0, y0, z0)的点以(vx, vy, vz)的速度运动,问多长时间后撞到圆锥上(保证一定能撞)

圆锥表面方程:

\left\{\begin{matrix}\frac{h - z}{h} = \frac{R}{r} \\ x^{2} + y^{2} = R^{2} \end{matrix}\right.

设时间为t,则撞点(x, y, z)为:

\left\{\begin{matrix} x = x_{0} + v_{x} * t \\y = y_{0} + v_{y} * t \\ z = z_{0} + v_{z} * t \end{matrix}\right.

上面两式联立,整理一下:

(v_{x}^{2}h^{2} + v_{y}^{2}h^{2} -v_{z}^{2}r^{2} ) * t^{2} + (2x_{0}v_{x}h^{2} + 2y_{0}v_{y}h^{2} + 2hv_{z}r^{2} - 2z_{0}v_{z}r^{2}) * t + x_{0}^{2}h^{2} + y_{0}^{2}h^{2} - r^{2}h^{2} - r^{2}z_{0}^{2}+2hz_{0}r^{2} = 0

这不就是个一元二次方程?

求得的两个解要判断一下是不是和上半圆锥相交,如果两个解都是,取较小解,较大解是穿过圆锥离开的时间

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;

int main() {
	int t, kcase = 0;
	double h, r, vx, vy, vz, x0, y0, z0;
	scanf("%d", &t);
	while(t--) {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &r, &h, &x0, &y0, &z0, &vx, &vy, &vz);
        double a = h * h * vx * vx + h * h * vy * vy - r * r * vz * vz;
        double b = h * h * 2.0 * (x0 * vx + y0 * vy) - r * r * 2.0 * (vz * z0 - vz * h);
        double c = h * h * x0 * x0 + h * h * y0 * y0 - h * h * r * r - z0 * z0 * r * r + 2.0 * h * z0 * r * r;
        double t1 = (sqrt(b * b - 4.0 * a * c) - b) / (2.0 * a);
        double t2 = (-sqrt(b * b - 4.0 * a * c) - b) / (2.0 * a);
        if(t1 > t2) swap(t1, t2);
        if(t1 < 0)
            printf("Case %d: %.8f\n", ++kcase, t2);
        else {
            if(0 <= z0 + vz * t1 && z0 + vz * t1 <= h)  ///上圆锥 输出较小时间
                printf("Case %d: %.8f\n", ++kcase, t1);
            else printf("Case %d: %.8f\n", ++kcase, t2);
        }
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43871207/article/details/109021504