題意:三維空間N個球體,給一個光束的方向,求它在這些球體間的反射情況(球面反射)。
Problem: N spheres are in a 3D-space. Give you a direction of a light and write a program to output the reflections(the light rays reflect from the surface of the spheres according the ordinary law).
題解:如下圖所示,每次反射分為兩步。第一步利用圖中關系求出tt,進而求出反射點。第二步利用投影定理求出反射光線的方向向量。
Solutions: As shown in the figure, each reflection can be considered as two steps. Firstly, calculate the value of tt according to the figure, then make the reflect point. Secondly, calculate the direction vector of the reflected ray by the projection theorem.
#include#include #include #include #include using namespace std; #define P(x) ((x)*(x)) const int N=50+10; const double oo=(1LL<<60); const double eps=1e-8; int n,cnt; double r[N],tt; struct Point { Point() {} Point(double x,double y,double z): x(x),y(y),z(z) {} double x,y,z; }c[N],s,e,dir; double operator *(Point A,Point B) {return A.x*B.x+A.y*B.y+A.z*B.z;} Point operator *(double B,Point A) {return Point(A.x*B,A.y*B,A.z*B);} Point operator +(Point A,Point B) {return Point(A.x+B.x,A.y+B.y,A.z+B.z);} Point operator -(Point A,Point B) {return Point(A.x-B.x,A.y-B.y,A.z-B.z);} inline int next() { int w = 0; double a,b,cc,delta,gen; tt = oo; for(int i=1;i<=n;i++) { a = dir*dir; b = 2.0*(s-c[i])*dir; cc = P(s-c[i]) - P(r[i]); if ((delta = P(b)-4*a*cc)>-eps) { if (fabs(delta) eps && geneps && gen