Spherical Mirrors

Time limit1sMemory limit128 MB

Summary
Simulate a laser reflecting off multiple spheres in 3D using mirror-reflection geometry and report the final reflection point.
Level

Medium6 of 10

Topics
Geometry, Simulation, Math
Solved
No attempts yet

Problem

There are NN spherical mirrors in three-dimensional space.

A laser is fired from (0,0,0)(0,0,0) in the direction (u,v,w)(u,v,w). The laser always travels in a straight line.

Suppose a laser fired from a point II is reflected at a point QQ on a spherical mirror. Let NN be a point that lies outside the mirror and on the straight line joining the mirror's center and QQ. Then the laser is reflected in a direction RR that satisfies the following condition:

(1) RR lies in the plane determined by II, QQ, and NN, and ∠IQN=∠NQR\angle IQN = \angle NQR.

Write a program that finds the position of the point at which the laser is reflected for the last time.

Input

The first line contains the number of test cases TT. For each test case, the first line contains the number of spherical mirrors NN. The second line contains the direction uu, vv, ww in which the laser is fired, separated by spaces.

Each of the next NN lines contains four integers xix_i, yiy_i, ziz_i, rir_i describing a spherical mirror whose center is (xi,yi,zi)(x_i, y_i, z_i) and whose radius is rir_i.

1≤N≤1001 \le N \le 100

−100≤u,v,w≤100-100 \le u, v, w \le 100

−100≤xi,yi,zi≤100-100 \le x_i, y_i, z_i \le 100

5≤ri≤305 \le r_i \le 30

u2+v2+w2>0u^2 + v^2 + w^2 > 0

The distance between any two spherical mirrors is at least 0.10.1. The point (0,0,0)(0,0,0) lies outside every spherical mirror and is at least 0.10.1 away from each of them.

The laser is reflected by the spherical mirrors at least once and at most five times. Moreover, the angle of reflection θ\theta (that is, ∠NQR\angle NQR, which equals ∠IQN\angle IQN) is always smaller than 85∘85^\circ.

Output

For each test case, output the coordinates of the last reflection point, separated by spaces. Every coordinate is rounded at the fourth decimal place and printed to three decimal places.

Examples2

  1. Example 1

    Input
    2
    3
    -20 -20 -24
    100 100 100 30
    10 8 3 5
    -70 -70 -84 5
    4
    0 47 84
    -23 41 42 8
    45 -10 14 19
    -5 28 47 12
    -27 68 34 14
    
    Expected output
    79.094 79.094 94.913
    -21.865 54.977 34.176
    
  2. Example 2

    Input
    1
    3
    17 -68 29
    -46 -82 -12 10
    18 -48 35 29
    -80 93 -54 29
    
    Expected output
    7.870 -31.480 13.425