<?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>polar Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/polar/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/polar/</link>
	<description></description>
	<lastBuildDate>Tue, 04 Sep 2018 15:14:42 +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>polar Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/polar/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 883. Generate Random Point in a Circle</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-883-generate-random-point-in-a-circle/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-883-generate-random-point-in-a-circle/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 28 Jul 2018 23:50:28 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[polar]]></category>
		<category><![CDATA[random]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3336</guid>

					<description><![CDATA[<p>Problem Given the radius and x-y positions of the center of a circle, write a function randPoint which generates a uniform random point in the circle. Note: input and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-883-generate-random-point-in-a-circle/">花花酱 LeetCode 883. Generate Random Point in 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[<h1><strong>Problem</strong></h1>
<p>Given the radius and x-y positions of the center of a circle, write a function <code>randPoint</code> which generates a uniform random point in the circle.</p>
<p>Note:</p>
<ol>
<li>input and output values are in <a href="https://www.webopedia.com/TERM/F/floating_point_number.html" target="_blank" rel="noopener">floating-point</a>.</li>
<li>radius and x-y position of the center of the circle is passed into the class constructor.</li>
<li>a point on the circumference of the circle is considered to be in the circle.</li>
<li><code>randPoint</code> returns a size 2 array containing x-position and y-position of the random point, in that order.</li>
</ol>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: 
</strong><span id="example-input-1-1">["Solution","randPoint","randPoint","randPoint"]
</span><span id="example-input-1-2">[[1,0,0],[],[],[]]</span>
<strong>Output: </strong><span id="example-output-1">[null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: 
</strong><span id="example-input-2-1">["Solution","randPoint","randPoint","randPoint"]
</span><span id="example-input-2-2">[[10,5,-7.5],[],[],[]]</span>
<strong>Output: </strong><span id="example-output-2">[null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]</span></pre>
<p><strong>Explanation of Input Syntax:</strong></p>
<p>The input is two lists: the subroutines called and their arguments. <code>Solution</code>&#8216;s constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. <code>randPoint</code> has no arguments. Arguments are always wrapped with a list, even if there aren&#8217;t any.</p>
<p><ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"> </ins></p>
<h1>Solution: Polar Coordinate</h1>
<p>uniform sample an angle a: [0, 2*Pi)</p>
<p>uniform sample a radius r: [0, 1)</p>
<p>Number of random points in a cycle should be proportional to the square of distance to the center.</p>
<p>e.g. there are 4 times of points with distance d than points with distance d/2.</p>
<p>Thus sqrt(r) is uniformly distributed.</p>
<p>r&#8217; = sqrt(r),</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 116 ms
class Solution {
public:
  Solution(double radius, double x_center, double y_center)
    :r_(radius), x_(x_center), y_(y_center) {}

  vector&lt;double&gt; randPoint() {
    double a = rand() / static_cast&lt;double&gt;(RAND_MAX) * 2 * M_PI;
    double r = sqrt(rand() / static_cast&lt;double&gt;(RAND_MAX)) * r_;
    return {x_ + r * cos(a), y_ + r * sin(a)};
  }
private:
  double r_;
  double x_;
  double y_;
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-883-generate-random-point-in-a-circle/">花花酱 LeetCode 883. Generate Random Point in 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-883-generate-random-point-in-a-circle/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
