<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>circle Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/circle/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/circle/</link>
	<description></description>
	<lastBuildDate>Fri, 31 Dec 2021 22:52:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>circle Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/circle/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1974. Minimum Time to Type Word Using Special Typewriter</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1974-minimum-time-to-type-word-using-special-typewriter/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1974-minimum-time-to-type-word-using-special-typewriter/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 31 Dec 2021 22:41:17 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9344</guid>

					<description><![CDATA[<p>There is a special typewriter with lowercase English letters&#160;'a'&#160;to&#160;'z'&#160;arranged in a&#160;circle&#160;with a&#160;pointer. A character can&#160;only&#160;be typed if the pointer is pointing to that character. The&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1974-minimum-time-to-type-word-using-special-typewriter/">花花酱 LeetCode 1974. Minimum Time to Type Word Using Special Typewriter</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>There is a special typewriter with lowercase English letters&nbsp;<code>'a'</code>&nbsp;to&nbsp;<code>'z'</code>&nbsp;arranged in a&nbsp;<strong>circle</strong>&nbsp;with a&nbsp;<strong>pointer</strong>. A character can&nbsp;<strong>only</strong>&nbsp;be typed if the pointer is pointing to that character. The pointer is&nbsp;<strong>initially</strong>&nbsp;pointing to the character&nbsp;<code>'a'</code>.</p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/07/31/chart.jpg" alt=""/></figure>



<p>Each second, you may perform one of the following operations:</p>



<ul><li>Move the pointer one character&nbsp;<strong>counterclockwise</strong>&nbsp;or&nbsp;<strong>clockwise</strong>.</li><li>Type the character the pointer is&nbsp;<strong>currently</strong>&nbsp;on.</li></ul>



<p>Given a string&nbsp;<code>word</code>, return the<strong>&nbsp;minimum</strong>&nbsp;number of seconds to type out the characters in&nbsp;<code>word</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word = "abc"
<strong>Output:</strong> 5
<strong>Explanation: 
</strong>The characters are printed as follows:
- Type the character 'a' in 1 second since the pointer is initially on 'a'.
- Move the pointer clockwise to 'b' in 1 second.
- Type the character 'b' in 1 second.
- Move the pointer clockwise to 'c' in 1 second.
- Type the character 'c' in 1 second.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word = "bza"
<strong>Output:</strong> 7
<strong>Explanation:
</strong>The characters are printed as follows:
- Move the pointer clockwise to 'b' in 1 second.
- Type the character 'b' in 1 second.
- Move the pointer counterclockwise to 'z' in 2 seconds.
- Type the character 'z' in 1 second.
- Move the pointer clockwise to 'a' in 1 second.
- Type the character 'a' in 1 second.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word = "zjpc"
<strong>Output:</strong> 34
<strong>Explanation:</strong>
The characters are printed as follows:
- Move the pointer counterclockwise to 'z' in 1 second.
- Type the character 'z' in 1 second.
- Move the pointer clockwise to 'j' in 10 seconds.
- Type the character 'j' in 1 second.
- Move the pointer clockwise to 'p' in 6 seconds.
- Type the character 'p' in 1 second.
- Move the pointer counterclockwise to 'c' in 13 seconds.
- Type the character 'c' in 1 second.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= word.length &lt;= 100</code></li><li><code>word</code>&nbsp;consists of lowercase English letters.</li></ul>



<h2><strong>Solution: Clockwise or Counter-clockwise?</strong></h2>



<p>For each pair of (prev, curr), choose the shortest distance.<br>One is abs(p &#8211; c), another is 26 &#8211; abs(p &#8211; c).<br>Don&#8217;t forget to add 1 for typing itself.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minTimeToType(string word) {
    int ans = 0;
    char p = 'a';
    for (char c : word) {
      ans += 1 + min(abs(c - p), 26 - abs(c - p));
      p = c;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1974-minimum-time-to-type-word-using-special-typewriter/">花花酱 LeetCode 1974. Minimum Time to Type Word Using Special Typewriter</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-1974-minimum-time-to-type-word-using-special-typewriter/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1828. Queries on Number of Points Inside a Circle</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1828-queries-on-number-of-points-inside-a-circle/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1828-queries-on-number-of-points-inside-a-circle/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 17 Apr 2021 18:57:55 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8357</guid>

					<description><![CDATA[<p>You are given an array&#160;points&#160;where&#160;points[i] = [xi, yi]&#160;is the coordinates of the&#160;ith&#160;point on a 2D plane. Multiple points can have the&#160;same&#160;coordinates. You are also given&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1828-queries-on-number-of-points-inside-a-circle/">花花酱 LeetCode 1828. Queries on Number of Points Inside a Circle</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given an array&nbsp;<code>points</code>&nbsp;where&nbsp;<code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>&nbsp;is the coordinates of the&nbsp;<code>i<sup>th</sup></code>&nbsp;point on a 2D plane. Multiple points can have the&nbsp;<strong>same</strong>&nbsp;coordinates.</p>



<p>You are also given an array&nbsp;<code>queries</code>&nbsp;where&nbsp;<code>queries[j] = [x<sub>j</sub>, y<sub>j</sub>, r<sub>j</sub>]</code>&nbsp;describes a circle centered at&nbsp;<code>(x<sub>j</sub>, y<sub>j</sub>)</code>&nbsp;with a radius of&nbsp;<code>r<sub>j</sub></code>.</p>



<p>For each query&nbsp;<code>queries[j]</code>, compute the number of points&nbsp;<strong>inside</strong>&nbsp;the&nbsp;<code>j<sup>th</sup></code>&nbsp;circle. Points&nbsp;<strong>on the border</strong>&nbsp;of the circle are considered&nbsp;<strong>inside</strong>.</p>



<p>Return&nbsp;<em>an array&nbsp;</em><code>answer</code><em>, where&nbsp;</em><code>answer[j]</code><em>&nbsp;is the answer to the&nbsp;</em><code>j<sup>th</sup></code><em>&nbsp;query</em>.</p>



<p><strong>Example 1:</strong><img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/chrome_2021-03-25_22-34-16.png"></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]
<strong>Output:</strong> [3,2,2]
<strong>Explanation: </strong>The points and circles are shown above.
queries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle.
</pre>



<p><strong>Example 2:</strong><img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/chrome_2021-03-25_22-42-07.png"></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]
<strong>Output:</strong> [2,3,2,4]
<strong>Explanation: </strong>The points and circles are shown above.
queries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= points.length &lt;= 500</code></li><li><code>points[i].length == 2</code></li><li><code>0 &lt;= x<sub>​​​​​​i</sub>, y<sub>​​​​​​i</sub>&nbsp;&lt;= 500</code></li><li><code>1 &lt;= queries.length &lt;= 500</code></li><li><code>queries[j].length == 3</code></li><li><code>0 &lt;= x<sub>j</sub>, y<sub>j</sub>&nbsp;&lt;= 500</code></li><li><code>1 &lt;= r<sub>j</sub>&nbsp;&lt;= 500</code></li><li>All coordinates are integers.</li></ul>



<h2><strong>Solution: Brute Force</strong></h2>



<p>Time complexity: O(P * Q)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; countPoints(vector&lt;vector&lt;int&gt;&gt;&amp; points, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    vector&lt;int&gt; ans;
    ans.reserve(queries.size());
    for (const auto&amp; q : queries) {
      const int rs = q[2] * q[2];
      int cnt = 0;      
      for (const auto&amp; p : points)
        if ((q[0] - p[0]) * (q[0] - p[0]) + 
            (q[1] - p[1]) * (q[1] - p[1]) &lt;= rs)
          ++cnt;
      ans.push_back(cnt);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1828-queries-on-number-of-points-inside-a-circle/">花花酱 LeetCode 1828. Queries on Number of Points Inside a Circle</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/geometry/leetcode-1828-queries-on-number-of-points-inside-a-circle/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1453. Maximum Number of Darts Inside of a Circular Dartboard</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1453-maximum-number-of-darts-inside-of-a-circular-dartboard/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1453-maximum-number-of-darts-inside-of-a-circular-dartboard/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 May 2020 05:22:56 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[angular sweep]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[gemoetry]]></category>
		<category><![CDATA[hard]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6770</guid>

					<description><![CDATA[<p>You have a very large square wall and a circular dartboard placed on the wall.&#160;You have been challenged to throw darts into the board blindfolded.&#160;Darts&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1453-maximum-number-of-darts-inside-of-a-circular-dartboard/">花花酱 LeetCode 1453. Maximum Number of Darts Inside of a Circular Dartboard</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You have a very large square wall and a circular dartboard placed on the wall.&nbsp;You have been challenged to throw darts into the board blindfolded.&nbsp;Darts thrown at the wall are represented as an array of&nbsp;<code>points</code>&nbsp;on a 2D plane.&nbsp;</p>



<p>Return&nbsp;the maximum number of points that are within or&nbsp;lie&nbsp;on&nbsp;<strong>any</strong>&nbsp;circular dartboard of radius&nbsp;<code>r</code>.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/04/29/sample_1_1806.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[-2,0],[2,0],[0,2],[0,-2]], r = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Circle dartboard with center in (0,0) and radius = 2 contain all points.
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/04/29/sample_2_1806.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[-3,0],[3,0],[2,6],[5,4],[0,9],[7,8]], r = 5
<strong>Output:</strong> 5
<strong>Explanation:</strong> Circle dartboard with center in (0,4) and radius = 5 contain all points except the point (7,8).
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[-2,0],[2,0],[0,2],[0,-2]], r = 1
<strong>Output:</strong> 1
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> points = [[1,2],[3,5],[1,-1],[2,3],[4,1],[1,3]], r = 2
<strong>Output:</strong> 4
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= points.length &lt;= 100</code></li><li><code>points[i].length == 2</code></li><li><code>-10^4 &lt;= points[i][0], points[i][1] &lt;= 10^4</code></li><li><code>1 &lt;= r &lt;= 5000</code></li></ul>



<h2><strong>Solution 1: Angular Sweep</strong></h2>



<p>See for more details: <a href="https://www.geeksforgeeks.org/angular-sweep-maximum-points-can-enclosed-circle-given-radius/">https://www.geeksforgeeks.org/angular-sweep-maximum-points-can-enclosed-circle-given-radius/</a></p>



<p>Time complexity: O(n^2*logn)<br>Space complexity: O(n^2)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
// Python-Like enumerate() In C++17
// http://reedbeta.com/blog/python-like-enumerate-in-cpp17/
template &lt;typename T,
          typename TIter = decltype(std::begin(std::declval&lt;T&gt;())),
          typename = decltype(std::end(std::declval&lt;T&gt;()))&gt;
constexpr auto enumerate(T &amp;&amp; iterable) {
  struct iterator {
    size_t i;
    TIter iter;
    bool operator != (const iterator &amp; other) const { return iter != other.iter; }
    void operator ++ () { ++i; ++iter; }
    auto operator * () const { return std::tie(i, *iter); }
  };
  struct iterable_wrapper {
    T iterable;
    auto begin() { return iterator{ 0, std::begin(iterable) }; }
    auto end() { return iterator{ 0, std::end(iterable) }; }
  };
  return iterable_wrapper{ std::forward&lt;T&gt;(iterable) };
}

class Solution {
public:
  int numPoints(vector&lt;vector&lt;int&gt;&gt;&amp; points, int r) {
    const int n = points.size();
    vector&lt;vector&lt;double&gt;&gt; d(n, vector&lt;double&gt;(n));
    
    for (const auto&amp; [i, pi] : enumerate(points))  
      for (const auto&amp; [j, pj] : enumerate(points))
        d[i][j] = d[j][i] = sqrt((pi[0] - pj[0]) * (pi[0] - pj[0]) 
                               + (pi[1] - pj[1]) * (pi[1] - pj[1]));
        
    int ans = 1;
    for (const auto&amp; [i, pi] : enumerate(points)) {    
      vector&lt;pair&lt;double, int&gt;&gt; angles; // {angle, event_type}
      for (const auto&amp; [j, pj] : enumerate(points)) {
        if (i != j &amp;&amp; d[i][j] &lt;= 2 * r) {
          const double a = atan2(pj[1] - pi[1], pj[0] - pi[0]);
          const double b = acos(d[i][j] / (2 * r));
          angles.emplace_back(a - b, -1); // entering
          angles.emplace_back(a + b, 1); // exiting
        }
      }
      // If angles are the same, entering points go first.
      sort(begin(angles), end(angles));
      int pts = 1;
      for (const auto&amp; [_, event] : angles)
        ans = max(ans, pts -= event);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1453-maximum-number-of-darts-inside-of-a-circular-dartboard/">花花酱 LeetCode 1453. Maximum Number of Darts Inside of a Circular Dartboard</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/geometry/leetcode-1453-maximum-number-of-darts-inside-of-a-circular-dartboard/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1401. Circle and Rectangle Overlapping</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1401-circle-and-rectangle-overlapping/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1401-circle-and-rectangle-overlapping/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Apr 2020 03:49:18 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[inside]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[rectangle]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6569</guid>

					<description><![CDATA[<p>Given a circle represented as (radius,&#160;x_center,&#160;y_center)&#160;and an axis-aligned rectangle represented as (x1,&#160;y1,&#160;x2,&#160;y2),&#160;where (x1,&#160;y1) are the coordinates of the bottom-left corner, and (x2,&#160;y2) are the coordinates&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1401-circle-and-rectangle-overlapping/">花花酱 LeetCode 1401. Circle and Rectangle Overlapping</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a circle represented as (<code>radius</code>,&nbsp;<code>x_center</code>,&nbsp;<code>y_center</code>)&nbsp;and an axis-aligned rectangle represented as (<code>x1</code>,&nbsp;<code>y1</code>,&nbsp;<code>x2</code>,&nbsp;<code>y2</code>),&nbsp;where (<code>x1</code>,&nbsp;<code>y1</code>) are the coordinates of the bottom-left corner, and (<code>x2</code>,&nbsp;<code>y2</code>) are the coordinates of the top-right corner of the&nbsp;rectangle.</p>



<p>Return True if the circle and rectangle are overlapped otherwise return False.</p>



<p>In other words, check if there are&nbsp;<strong>any&nbsp;</strong>point&nbsp;(xi, yi) such that belongs to the circle and the rectangle at the same time.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/02/20/sample_4_1728.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> radius = 1, x_center = 0, y_center = 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>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/02/20/sample_2_1728.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> radius = 1, x_center = 0, y_center = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1
<strong>Output:</strong> true
</pre>



<p><strong>Example 3:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/03/03/sample_6_1728.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> radius = 1, x_center = 1, y_center = 1, x1 = -3, y1 = -3, x2 = 3, y2 = 3
<strong>Output:</strong> true
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> radius = 1, x_center = 1, y_center = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1
<strong>Output:</strong> false
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= radius &lt;= 2000</code></li><li><code>-10^4 &lt;= x_center, y_center, x1, y1, x2, y2 &lt;= 10^4</code></li><li><code>x1 &lt; x2</code></li><li><code>y1 &lt; y2</code></li></ul>



<h2><strong>Solution: Geometry</strong></h2>



<p>Find the shortest distance from the center to the rectangle, return dist &lt;= radius.</p>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
    int dx = x_center - max(x1, min(x2, x_center));
    int dy = y_center - max(y1, min(y2, y_center));
    return dx * dx + dy * dy &lt;= radius * radius;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1401-circle-and-rectangle-overlapping/">花花酱 LeetCode 1401. Circle and Rectangle Overlapping</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/geometry/leetcode-1401-circle-and-rectangle-overlapping/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
