''' Solution to the problem of pool table. First bounce at position x -> second bounce at what position? the tan(angle) of the first leg of the trajectory is (4-1)/(x-1) = 3/(x-1), because the initial position is (1,1) and the width of pool table is 4. Let y will be the distance of the point of impact on the right border from the top boundary. Continuing the 1st leg until it intersect right border tell us that tan(alpha) is also equal y/(8-x), (make a sketch!). 3/(x-1) = y/(8-x) ==> y = 3(8-x)/(x-1). So for every x we know y. If the bounce happens at y, what is the place where the 3rd leg of trajectory intersects the horizontal line passing through the black ball and the white ball's starting position (3 units from the top)? That question can be answered when we figure out the angles. A sketch of the situation will readily show that the angle w.r.t. lower borderon 1st leg of journey and the 3rd leg are the same. [This is how the refletors on bikes and cars work, they redirect a beam of light in the opposite direction, so when you sit in a car that beams light toward a distant bicycle, the red light bouncing off the bicycle reflector should go straight back toward your car and you!] tan(alpha) in a triangle formed by the final leg of the path is (3-y)/(8-p), where p is the x-coordinate of intersection point. (that we would like to become equal 6, the position of black ball.) 3/(x-1) = (3-y)/(8-p) ==> 8-p = (x-1)*(3-y)/3 Using previous y = y(x), 8-p = (x-1) - (y/3)(x-1) = x-1 - (8-x) = 2x - 9. Finally p = 8-2x+9 = 17-2x At this point we've (unfortunately?) simplified the search to the point where computer is not really needed, since setting the value of p=6 corresponds to perfect hit on the black ball, and gives x = (17-6)/2 = 5.5 (that's our analytical solution that could be also derived by symmetry principles.) 4|-----------x---|+++++| | / \ | | | / \ | this distance is y 3| / \|_____| | / /| | 2| / / | | | / / | | 1|----o------p---- this vert. distance is 4-y | / | | 0|_______________|.....| But how well will a Python code reproduce this result? The error function is E(x) = p - 6 = 11-2x. When the program executes, it prints % python3 pool.py The pool border needs to be hit at position x= 5.5 It has found the analytical solution exactly! I did not intend this, the resulting E(x) is too simple, but that's how it sometimes is... most of the time its hard to hit the zero of a function with absolute precision, but due to our using round numbers 4 and 8 as boundaries of search interval, we succeeded in hitting the black ball exactly head-on. ''' def E(x): E = 11 - 2*x return E # main program that finds zero of E(x) # we'll search between x=4 and x=8 (in the right half of pool table) x0 = 4 x1 = 8 mismatch = E(x0) # this variable must already exist when we enter loop N = 10000 # divide the search interval into this many points # the search loop for i in range(N): x = x0 + (x1-x0)* i/N # i/N has values from 0 to almost 1. previous_mismatch = mismatch # this saves the previous value of E mismatch = E(x) # and that's the current value of E if( mismatch*previous_mismatch <= 0.): break # we break out from the loop if i > 0, at the point when # mismatch changes sign from positive to negative # or from negative to positive or becomes exactly zero, # since then and only then # mismatch*previous_mismatch <= 0 has True value print(" The pool border needs to be hit at position x=",x)