Question: N sphere in a three-dimensional space. Give a beam direction and find the reflection (spherical reflection) between these sphere ).
Problem: N spheres are in a 3D-space. give you a ction 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 ).
Question: As shown in, each reflection is divided into two steps. The first step is to use the link in the graph to find the tt and then the reflection point. Step 2 use the Projection Theorem to obtain the direction vector of the reflected light.
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 && gen
eps && gen