Skip to content

Commit

Permalink
update 6a5aef7 fix(post): unordered_set in xcode (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jan 1, 2025
1 parent a1ef446 commit 7690f5e
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions docs/posts/2024-07-21-evenly-random-points-on-plane/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
Possible use-cases:</p><ul><li>generate trees in a forest in a game world;</li><li>generate points for <a href=https://en.wikipedia.org/wiki/Voronoi_diagram>Voronoi diagram</a>
(with areas of similar size);</li></ul><p>The simplest approach - to use uniformly distributed points with <code>(random(), random())</code> - doesn&rsquo;t work,
because there will be areas with high density of points and areas with no points at all.
Such distribution doesn&rsquo;t look <em>natural</em>.</p><p><svg id="a0a38a3c796592203f62b231d0ba826f4" width="100%" height="100%" viewBox="0 0 700 400"/><p id=c6af6830f5022a1389263b4c0239d72ba style=text-align:center>Caption</p></p><script type=module>
Such distribution doesn&rsquo;t look <em>natural</em>.</p><p><svg id="ab738b302021582cfa64266032aaf939d" width="100%" height="100%" viewBox="0 0 700 400"/><p id=c938d60286f392c3a462a723fa050b1b2 style=text-align:center>Caption</p></p><script type=module>

const svgns = 'http://www.w3.org/2000/svg';

const width = 700 ;
const height = 400 ;

const numPoints = 1700 ;
document.getElementById('c6af6830f5022a1389263b4c0239d72ba').innerText = `${numPoints} points`;
document.getElementById('c938d60286f392c3a462a723fa050b1b2').innerText = `${numPoints} points`;

function renderSVG_UniformDistribution() {
let svg = document.getElementById('a0a38a3c796592203f62b231d0ba826f4');
let svg = document.getElementById('ab738b302021582cfa64266032aaf939d');


while (svg.firstChild) {
Expand All @@ -41,14 +41,14 @@
return result;
}

document.getElementById('a0a38a3c796592203f62b231d0ba826f4').addEventListener('click', renderSVG_UniformDistribution);
document.getElementById('ab738b302021582cfa64266032aaf939d').addEventListener('click', renderSVG_UniformDistribution);

renderSVG_UniformDistribution();

</script><p>Various types of grids with gaps can give even distribution, but the picture will not look <em>random</em>.
There will always be a pattern, sometimes more visible, sometimes less, but still visible. This
doesn&rsquo;t look <em>natural</em> either.</p><p>A solution to this problem is <a href=https://en.wikipedia.org/wiki/Supersampling#Poisson_disk>Poisson disk sampling (or Poisson disk distribution)</a>:
points are placed randomly, but not too close and not too far away from each other.</p><p><svg id="a2b83206a1cf26a8d0963375b30922a4f" width="100%" height="100%" viewBox="0 0 700 400"/><p id=cb89a32254d3c96a6227bfa0200f38316 style=text-align:center>Caption</p></p><script type=module>
points are placed randomly, but not too close and not too far away from each other.</p><p><svg id="a65b369b8241dff28c000a963aa222337" width="100%" height="100%" viewBox="0 0 700 400"/><p id=cba251f08c02f4636a2d8a9379230263b style=text-align:center>Caption</p></p><script type=module>

const svgns = 'http://www.w3.org/2000/svg';

Expand All @@ -58,15 +58,15 @@
const radius = 10 ;

function renderSVG_PoissonDiskDistribution() {
let svg = document.getElementById('a2b83206a1cf26a8d0963375b30922a4f');
let svg = document.getElementById('a65b369b8241dff28c000a963aa222337');


while (svg.firstChild) {
svg.removeChild(svg.firstChild);
}

const points = generatePointsByPoissonDiskDistribution(width, height, radius);
document.getElementById('cb89a32254d3c96a6227bfa0200f38316').innerText = `${points.length} points`;
document.getElementById('cba251f08c02f4636a2d8a9379230263b').innerText = `${points.length} points`;

for (let p of points) {
let circle = document.createElementNS(svgns, 'circle');
Expand Down Expand Up @@ -190,7 +190,7 @@
}
}

document.getElementById('a2b83206a1cf26a8d0963375b30922a4f').addEventListener('click', renderSVG_PoissonDiskDistribution);
document.getElementById('a65b369b8241dff28c000a963aa222337').addEventListener('click', renderSVG_PoissonDiskDistribution);

renderSVG_PoissonDiskDistribution()

Expand Down
8 changes: 4 additions & 4 deletions docs/posts/2024-07-29-random-points-in-circle/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<meta name=keywords content="procedural generation"><meta name=description content="angle = random(); distance = R * sqrt(random())"><meta name=author content><link rel=canonical href=https://andreynautilus.github.io/posts/2024-07-29-random-points-in-circle/><link crossorigin=anonymous href=/assets/css/stylesheet.b609c58d5c11bb90b1a54e04005d74ad1ddf22165eb79f5533967e57df9c3b50.css integrity="sha256-tgnFjVwRu5CxpU4EAF10rR3fIhZet59VM5Z+V9+cO1A=" rel="preload stylesheet" as=style><link rel=icon href=https://andreynautilus.github.io/favicon.ico><link rel=icon type=image/png sizes=16x16 href=https://andreynautilus.github.io/favicon-16x16.png><link rel=icon type=image/png sizes=32x32 href=https://andreynautilus.github.io/favicon-32x32.png><link rel=apple-touch-icon href=https://andreynautilus.github.io/apple-touch-icon.png><link rel=mask-icon href=https://andreynautilus.github.io/safari-pinned-tab.svg><meta name=theme-color content="#2e2e33"><meta name=msapplication-TileColor content="#2e2e33"><link rel=alternate hreflang=en href=https://andreynautilus.github.io/posts/2024-07-29-random-points-in-circle/><noscript><style>#theme-toggle,.top-link{display:none}</style><style>@media(prefers-color-scheme:dark){:root{--theme:rgb(29, 30, 32);--entry:rgb(46, 46, 51);--primary:rgb(218, 218, 219);--secondary:rgb(155, 156, 157);--tertiary:rgb(65, 66, 68);--content:rgb(196, 196, 197);--code-block-bg:rgb(46, 46, 51);--code-bg:rgb(55, 56, 62);--border:rgb(51, 51, 51)}.list{background:var(--theme)}.list:not(.dark)::-webkit-scrollbar-track{background:0 0}.list:not(.dark)::-webkit-scrollbar-thumb{border-color:var(--theme)}}</style></noscript><script type=text/javascript>(function(e,t,n,s,o,i,a){e[o]=e[o]||function(){(e[o].a=e[o].a||[]).push(arguments)},e[o].l=1*new Date;for(var r=0;r<document.scripts.length;r++)if(document.scripts[r].src===s)return;i=t.createElement(n),a=t.getElementsByTagName(n)[0],i.async=1,i.src=s,a.parentNode.insertBefore(i,a)})(window,document,"script","https://mc.yandex.ru/metrika/tag.js","ym"),ym(99367858,"init",{clickmap:!0,trackLinks:!0,accurateTrackBounce:!0})</script><noscript><div><img src=https://mc.yandex.ru/watch/99367858 style=position:absolute;left:-9999px alt></div></noscript><meta property="og:title" content="Random points in circle with uniform distribution"><meta property="og:description" content="angle = random(); distance = R * sqrt(random())"><meta property="og:type" content="article"><meta property="og:url" content="https://andreynautilus.github.io/posts/2024-07-29-random-points-in-circle/"><meta property="article:section" content="posts"><meta property="article:published_time" content="2024-07-29T18:54:08+02:00"><meta property="article:modified_time" content="2024-07-29T18:54:08+02:00"><meta property="og:site_name" content="Andrey Nautilus blog"><meta name=twitter:card content="summary"><meta name=twitter:title content="Random points in circle with uniform distribution"><meta name=twitter:description content="angle = random(); distance = R * sqrt(random())"><script type=application/ld+json>{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"posts","item":"https://andreynautilus.github.io/posts/"},{"@type":"ListItem","position":2,"name":"Random points in circle with uniform distribution","item":"https://andreynautilus.github.io/posts/2024-07-29-random-points-in-circle/"}]}</script><script type=application/ld+json>{"@context":"https://schema.org","@type":"BlogPosting","headline":"Random points in circle with uniform distribution","name":"Random points in circle with uniform distribution","description":"angle = random(); distance = R * sqrt(random())","keywords":["procedural generation"],"articleBody":"We need to generate a random point in a circle with uniform distribution.\nA naive approach with polar coordinates by picking a random angle and a random distance doesn’t give uniform distribution - there are more points close to center and fewer points at the radius. This article has explanation and visualization.\nNNN points, left to right: naive approach, correct formula, Monte Carlo (XXX attempts) Correct formula for polar coordinates In polar coordinates:\nangle = random(0, 2 * PI) distance = R * sqrt(random(0, 1)) Where R is the radius of the circle. And transformation to Cortesian coordinates:\nx = distance * cos(angle) y = distance * sin(angle) This article and StackOverflow threads give a mathematically correct explanation.\nMonte Carlo (multiple attempts) Another approach is to generate uniformly distributed random points in the bounding box of the circle and pick the first point that is inside the circle.\nWith random() being a uniformly random number, the naive approach for a point in a square:\nx = random() y = random() give uniformly distributed points. And x * x + y * y \u003c= R * R provides an easy test for point being inside the circle.\nObvious drawback - undefined number of attempts, but with uniformly distributed points the average amount of attempt should not be greater than 2 (on average we need 4 / PI attempts).\n","wordCount":"227","inLanguage":"en","datePublished":"2024-07-29T18:54:08+02:00","dateModified":"2024-07-29T18:54:08+02:00","mainEntityOfPage":{"@type":"WebPage","@id":"https://andreynautilus.github.io/posts/2024-07-29-random-points-in-circle/"},"publisher":{"@type":"Organization","name":"Andrey Nautilus blog","logo":{"@type":"ImageObject","url":"https://andreynautilus.github.io/favicon.ico"}}}</script></head><body id=top><script>localStorage.getItem("pref-theme")==="dark"?document.body.classList.add("dark"):localStorage.getItem("pref-theme")==="light"?document.body.classList.remove("dark"):window.matchMedia("(prefers-color-scheme: dark)").matches&&document.body.classList.add("dark")</script><header class=header><nav class=nav><div class=logo><a href=https://andreynautilus.github.io/ accesskey=h title="Andrey Nautilus blog (Alt + H)"><img src=https://andreynautilus.github.io/apple-touch-icon.png alt aria-label=logo height=35>Andrey Nautilus blog</a><div class=logo-switches><button id=theme-toggle accesskey=t title="(Alt + T)"><svg id="moon" xmlns="http://www.w3.org/2000/svg" width="24" height="18" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"/></svg><svg id="sun" xmlns="http://www.w3.org/2000/svg" width="24" height="18" viewBox="0 0 24 24" fill="none" stroke="currentcolor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></svg></button></div></div><ul id=menu><li><a href=https://andreynautilus.github.io/links/ title=links><span>links</span></a></li><li><a href=https://andreynautilus.github.io/tags/ title=tags><span>tags</span></a></li><li><a href=https://andreynautilus.github.io/search/ title="search (Alt + /)" accesskey=/><span>search</span></a></li><li><a href=https://github.com/AndreyNautilus/AndreyNautilus.github.io title=src><span>src</span>&nbsp;<svg fill="none" shape-rendering="geometricPrecision" stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" viewBox="0 0 24 24" height="12" width="12"><path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6"/><path d="M15 3h6v6"/><path d="M10 14 21 3"/></svg></a></li></ul></nav></header><main class=main><article class=post-single><header class=post-header><div class=breadcrumbs><a href=https://andreynautilus.github.io/>Home</a>&nbsp;»&nbsp;<a href=https://andreynautilus.github.io/posts/>posts</a></div><h1 class="post-title entry-hint-parent">Random points in circle with uniform distribution</h1><div class=post-meta><span title='2024-07-29 18:54:08 +0200 +0200'>July 29, 2024</span>&nbsp;·&nbsp;2 min</div></header><div class=post-content><p>We need to generate a random point in a circle with uniform distribution.</p><p>A naive approach with polar coordinates by picking a random angle and a random distance
doesn&rsquo;t give uniform distribution - there are more points close to center and fewer points at the radius.
<a href=https://www.anderswallin.net/2009/05/uniform-random-points-in-a-circle-using-polar-coordinates/>This article</a>
has explanation and visualization.</p><p><svg id="ab16c4270092109cd444d2d22129e8b46" width="100%" height="100%" viewBox="0 0 620 200"/><p id=c9d46247800e26d11c4b24cb99d201422 style=text-align:center>NNN points, left to right: naive approach, correct formula, Monte Carlo (XXX attempts)</p></p><script type=module>
has explanation and visualization.</p><p><svg id="a22c6d0417421429028edb9c4b40d2196" width="100%" height="100%" viewBox="0 0 620 200"/><p id=c49468d4912bd4122c2d2640be0217c90 style=text-align:center>NNN points, left to right: naive approach, correct formula, Monte Carlo (XXX attempts)</p></p><script type=module>

const svgns = 'http://www.w3.org/2000/svg';
const numPoints = 250;

function renderSVG_RandomPointsInCircle() {
let svg = document.getElementById('ab16c4270092109cd444d2d22129e8b46');
let svg = document.getElementById('a22c6d0417421429028edb9c4b40d2196');


while (svg.firstChild) {
Expand Down Expand Up @@ -45,7 +45,7 @@
svg.appendChild(circle);
}

let caption = document.getElementById('c9d46247800e26d11c4b24cb99d201422');
let caption = document.getElementById('c49468d4912bd4122c2d2640be0217c90');
let text = `${numPoints} points, left to right: naive approach, correct formula, Monte Carlo (${attempts} attempts)`;
caption.innerText = text;
}
Expand Down Expand Up @@ -97,7 +97,7 @@
return [attempts, result];
}

document.getElementById('ab16c4270092109cd444d2d22129e8b46').addEventListener('click', renderSVG_RandomPointsInCircle);
document.getElementById('a22c6d0417421429028edb9c4b40d2196').addEventListener('click', renderSVG_RandomPointsInCircle);

renderSVG_RandomPointsInCircle()

Expand Down
Loading

0 comments on commit 7690f5e

Please sign in to comment.