当前位置:首页 » Project Euler » 详细页

Investigating multiple reflections of a laser beam 题号:144 难度: 50 中英对照

In laser physics, a "white cell" is a mirror system that acts as a delay line for the laser beam. The beam enters the cell, bounces around on the mirrors, and eventually works its way back out.

The specific white cell we will be considering is an ellipse with the equation 4x2 + y2 = 100

The section corresponding to −0.01 ≤ x ≤ +0.01 at the top is missing, allowing the light to enter and exit through the hole.

The light beam in this problem starts at the point (0.0,10.1) just outside the white cell, and the beam first impacts the mirror at (1.4,-9.6).

Each time the laser beam hits the surface of the ellipse, it follows the usual law of reflection "angle of incidence equals angle of reflection." That is, both the incident and reflected beams make the same angle with the normal line at the point of incidence.

In the figure on the left, the red line shows the first two points of contact between the laser beam and the wall of the white cell; the blue line shows the line tangent to the ellipse at the point of incidence of the first bounce.

The slope m of the tangent line at any point (x,y) of the given ellipse is: m = −4x/y

The normal line is perpendicular to this tangent line at the point of incidence.

The animation on the right shows the first 10 reflections of the beam.

How many times does the beam hit the internal surface of the white cell before exiting?


Solution

设入射光线和切线的夹角为$a$ 设入射光线与水平面的夹角为$a_0$ 设切线和水平线的夹角为$a_1$ 设反射光线与水平面的夹角为$a_2$。 于是: $$tan(a)=tan(a_0-a_1)=\frac{tan(a_0)-tan(a_1)}{1+tan(a_0)tan(a_1)}$$ $$tan(a)=tan(a_1+\pi -a_2)=tan(a_1-a_2)=\frac{tan(a_1)-tan(a_2)}{1+tan(a_1)tan(a_2)}$$ 可以分离出$tan(a_2)$: $$tan(a_2)=\frac{tan(a_1)-tan(a)}{1+tan(a)tan(a_1)}$$ 其中$tan(a)$可以由第一个式子给出。 设反射光线的解析式为$y=tan(a_2)\times x+d$。 由于它过反射点$(x_O,y_O)$,则$d=y_O-tan(a_2)\times x_O$。 下一个反射点在椭圆和该直线的交点上,有: $$4x^2+(tan(a_2)\times x+d)^2=100$$ 这是一个一元二次方程,求解得到不同于$(x_O,y_O)$的另一个解即为下一个反射点。 按上述模拟即可。

Code

public final class p144 {
    public static void main(String[] args) {
        long start=System.nanoTime();
        String result = run();        
        long end=System.nanoTime();
        System.out.println(result);
        System.out.println( (end-start)/1000000 + "ms" );
    }
    
    static public String run(){
    	int res=0;
    	double xA=0.0;
    	double yA=10.1;
    	double xO=1.4;
    	double yO=-9.6;
    	while(Math.abs(xO)>0.01 || yO<0){
    	    //Calculate the slope of A
    	    double slopeA = (yO - yA) / (xO - xA);
    	    //Calculate the slope of the ellipse tangent
    	    double slopeO = -4*xO/yO;
    	    //Calculate the slope of B
    	    double tanA = (slopeA - slopeO)/(1+slopeA*slopeO);
    	    double slopeB = (slopeO- tanA)/ (1+ tanA*slopeO);
    	    //calculate intercept of line B
    	    double interceptB = yO - slopeB * xO;
    	    //solve the quadratic equation for finding
    	    // the intersection of B and the ellipse
    	    // a*x^2 + b*x + c = 0
    	    double a = 4 + slopeB*slopeB;
    	    double b = 2 * slopeB * interceptB;
    	    double c = interceptB * interceptB - 100;
    	    double ans1 = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
    	    double ans2 = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
    	    xA = xO;
    	    yA = yO;
    	    //Take the solution which is furtherst from x0
    	    xO = (Math.abs(ans1 - xO) > Math.abs(ans2 - xO)) ? ans1 : ans2;
    	    yO = slopeB * xO + interceptB;
    	    res++;
    	}
    	return res+"";
 	}

}
354
0ms