Space Junk

Space Junk

Space Junk_第1张图片
.
.
题意:给出两个球三位位置,半径以及速度向量,问什么时候能相撞。
.
.
解法:很明显这是一个不等式方程:
(X1+VX1tX2VX2t)2+(Y1+VY1tY2VY2t)2+(Z1+VZ1tZ2VZ2t)2Z<=r1+r2
然后这条式子两边平方解除t出来就好了。
.
.

#include 
#include 
#include 

const double eps = 1e-8;
double a, b, c, X1, Y1, z1, X2, Y2, z2;
double vX1, vX2, vY1, vY2, vz1, vz2, r1, r2, t;

double sqr(double X) {
    return X*X;
}

double check(double a, double b, double c) {
    if (fabs(a) < eps) {
        if (fabs(b) < eps) {
            if (fabs(c) < eps) return 0;
            else return -1;
        }
        if ((-c)*b > -eps) return (-c)/b;
        else return -1;
    }
    if (sqr(b)-4*a*c < -eps) return -1;
    if ((-b+sqrt(fabs(sqr(b)-4*a*c)))/(2*a) < -eps) return -1;
    if ((-b-sqrt(fabs(sqr(b)-4*a*c)))/(2*a) < -eps)
        return (-b+sqrt(fabs(sqr(b)-4*a*c)))/(2*a);
    else return (-b-sqrt(fabs(sqr(b)-4*a*c)))/(2*a);
}

int main() {
    int tt;
    scanf("%d", &tt);
    while (tt--) {
        scanf("%lf %lf %lf %lf %lf %lf %lf", &X1, &Y1, &z1, &r1, &vX1, &vY1, &vz1);
        scanf("%lf %lf %lf %lf %lf %lf %lf", &X2, &Y2, &z2, &r2, &vX2, &vY2, &vz2);

        if (sqrt(sqr(X1-X2)+sqr(Y1-Y2)+sqr(z1-z2)) - (r1+r2) < eps) {
            printf("0.000\n");
            continue;
        }

        a = sqr(vX1-vX2)+sqr(vY1-vY2)+sqr(vz1-vz2);
        b = 2*((vX1-vX2)*(X1-X2)+(vY1-vY2)*(Y1-Y2)+(vz1-vz2)*(z1-z2));
        c = sqr(X1-X2)+sqr(Y1-Y2)+sqr(z1-z2)-sqr(r1+r2);
        t = check(a, b, c);
        if (fabs(t+1) < eps) printf("No collision\n");
        else printf("%.3lf\n", fabs(t));
    }
}

你可能感兴趣的:(acm训练,UVALive)