Let's use gradient descent to optimise the "refractive index" at each point in a square in the middle. After a few iterations we get the following (grey) refractive index leading to the following simulation. Note how it all conspires to arrive at the right point...