Ball in a square

Also a slightly different requirement, but may help you....