Check if the circle is inside the Rectangle

1.4k Views Asked by At

As shown in the following figures, the upper right coordinate (W,H) of the rectangle and the central coordinate (x, y) and radius r of the circle are given. So how can I determines whether the circle is arranged inside the rectangle.image

1

There are 1 best solutions below

0
On

The circle is within the rectangle as long as its center lies on a rectangle that has a distance of r to the outer/original rectangle. So, as long as the coordinates of the circle's center fulfill the following boundary conditions, your circle lies within the rectangle.

$$ r \le x \le W-r\\ r \le y \le H-r $$

In the third figure, you see that the ordinate $y = H$ does not satisfy $y \le H-r$, although $x$ satisfies its boundary condition. So both conditions apply.