I've attached a rough sketch of the situation.
According to the problem
From the diagram you know:
Now plug in the term for d into the equation with S:
To get real results it is necessary that the radicand is equal or greater than zero. Thus the value of x must be greater than zero and smaller than 2r: