<p>You are given a circle represented as <code>(radius, xCenter, yCenter)</code> and an axis-aligned rectangle represented as <code>(x1, y1, x2, y2)</code>, where <code>(x1, y1)</code> are the coordinates of the bottom-left corner, and <code>(x2, y2)</code> are the coordinates of the top-right corner of the rectangle.</p>

<p>Return <code>true</code><em> if the circle and rectangle are overlapped otherwise return </em><code>false</code>. In other words, check if there is <strong>any</strong> point <code>(x<sub>i</sub>, y<sub>i</sub>)</code> that belongs to the circle and the rectangle at the same time.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/20/sample_4_1728.png" style="width: 258px; height: 167px;" />
<pre>
<strong>Input:</strong> radius = 1, xCenter = 0, yCenter = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> Circle and rectangle share the point (1,0).
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> radius = 1, xCenter = 1, yCenter = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1
<strong>Output:</strong> false
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/20/sample_2_1728.png" style="width: 150px; height: 135px;" />
<pre>
<strong>Input:</strong> radius = 1, xCenter = 0, yCenter = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1
<strong>Output:</strong> true
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= radius &lt;= 2000</code></li>
	<li><code>-10<sup>4</sup> &lt;= xCenter, yCenter &lt;= 10<sup>4</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= x1 &lt; x2 &lt;= 10<sup>4</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= y1 &lt; y2 &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
    double distance(int x1, int y1, int x2, int y2)
    {
        double dx, dy;
        
        dx = x2 - x1;
        dy = y2 - y1;
        
        return std::sqrt(dx * dx + dy * dy);
    }

public:
    bool checkOverlap(int radius, int xCenter, int yCenter,
                      int x1, int y1, int x2, int y2)
    {
        double xM, yM;
        int dx, dy;
        bool bOverlap;
        
        bOverlap = (xCenter >= x1 - radius && xCenter <= x2 + radius) ||
                   (yCenter >= y1 - radius && yCenter <= y2 + radius);
        if (bOverlap)
            if (xCenter < x1 || xCenter > x2) {
                dx = (xCenter < x1) ? (x1 - xCenter) : (xCenter - x2);
                yM = std::sqrt(radius * radius - dx * dx);
                bOverlap = (double)yCenter >= y1 - yM &&
                           (double)yCenter <= y2 + yM;
            } else if (yCenter < y1 || yCenter > y2) {
                dy = (yCenter < y1) ? (y1 - yCenter) : (yCenter - y2);
                xM = std::sqrt(radius * radius - dy * dy);
                bOverlap = (double)xCenter >= x1 - xM &&
                           (double)xCenter <= x2 + xM;
            }

        return bOverlap;
    }
};
7300