Jekyll2021-08-23T11:03:39+00:00http://andywiecko.github.io/feed.xmlAndrzej Więckowski, Ph.D.Andrzej Więckowski homepageN-body pendulum using Maple2020-02-01T10:00:00+00:002020-02-01T10:00:00+00:00http://andywiecko.github.io/2020/02/01/pendulum<p>I made Maple worksheet for studying the $N$-body system of pendulums. You can find it <a href="https://github.com/andywiecko/Maple-classes-the-final-project">here</a>. Below I attached some snapshots from pendulum dynamics with different initial conditions.</p> <figure style="text-align: center"> <p><img src="6-body_pendulum1.gif" width="50%" /></p> <figure style="text-align: center"> <p><img src="6-body_pendulum2.gif" width="50%" /></p> <figure style="text-align: center"> <p><img src="6-body_pendulum3.gif" width="50%" /></p> <figure style="text-align: center"> <p><img src="6-body_pendulum4.gif" width="50%" /></p> <figure style="text-align: center"> <p><img src="6-body_pendulum5.gif" width="50%" /></p> </figure> </figure> </figure> </figure> </figure>I made Maple worksheet for studying the $N$-body system of pendulums. You can find it here. Below I attached some snapshots from pendulum dynamics with different initial conditions.Simulated annealing animation2019-08-16T08:00:00+00:002019-08-16T08:00:00+00:00http://andywiecko.github.io/2019/08/16/simulatedannealing<iframe src="https://widgets.figshare.com/articles/9638315/embed?show_title=1" width="480" height="360" allowfullscreen="true" frameborder="0"></iframe> <p><a href="https://doi.org/10.6084/m9.figshare.9638315.v1">Link</a></p>Snake using GNUplot2019-05-25T16:00:00+00:002019-05-25T16:00:00+00:00http://andywiecko.github.io/2019/05/25/gnuplotsnake<p>Check this out! I made a game using gnuplot: <a href="https://github.com/andywiecko/GnuplotSnake">GnuplotSnake</a> You ask why? Because I can! To play the game, just type <code class="language-plaintext highlighter-rouge">gnuplot snake.gnuplot</code>. Controls: arrows. Make sure that you have installed <code class="language-plaintext highlighter-rouge">GNUplot version 5.2</code> I attached below the preview from the game:</p> <figure style="text-align: center"> <p><img src="preview.gif" width="75%" /></p> </figure>Check this out! I made a game using gnuplot: GnuplotSnake You ask why? Because I can! To play the game, just type gnuplot snake.gnuplot. Controls: arrows. Make sure that you have installed GNUplot version 5.2 I attached below the preview from the game:My first Unity 3d project2018-09-01T08:00:00+00:002018-09-01T08:00:00+00:00http://andywiecko.github.io/2018/09/01/towerdefence<p>Check this out! I’ve made my first game using Unity Engine: <a href="https://github.com/andywiecko/TowerDefence">Tower Defence</a> There are already builds for Linux and Windows OS. To play the game, just run the proper file from <code class="language-plaintext highlighter-rouge">Builds/</code> dir. I’ve attached below some screenshots from the game:</p> <figure style="text-align: center"> <p><img src="screen1.png" width="100%" /></p> <figure style="text-align: center"> <p><img src="screen2.png" width="100%" /></p> </figure> </figure>Check this out! I’ve made my first game using Unity Engine: Tower Defence There are already builds for Linux and Windows OS. To play the game, just run the proper file from Builds/ dir. I’ve attached below some screenshots from the game:Voronoi diagrams2018-08-01T12:00:00+00:002018-08-01T12:00:00+00:00http://andywiecko.github.io/2018/08/01/voronoi<p><a href="https://en.wikipedia.org/wiki/Voronoi_diagram">Voronoi diagram</a> is a diagram for set $P$ of $N$ points from $\mathbb R^n$ space (generally to any set $X$). It is constructed by splitting $\mathbb R^n$ space into $N$ subspaces $A_i$, in such a way that arbitrary point $x\in\mathbb R^n$ belongs to $A_i$, only if point $p_i\in P$ is the nearest point to $x$ for given metric $d$. From the mathematical perspective one can define each subspace in the following way:</p> $A_i = \{ x\in \mathbb R^n : \forall_{i\neq j} d(x,p_i) \le d(x,p_j) \}, \$ <p>where $d(x,y)$ is given metric. One can use an arbitrary metric. However, most common ones are <a href="https://en.wikipedia.org/wiki/Euclidean_distance">Euclidean</a> (standard) and <a href="https://en.wikipedia.org/wiki/Taxicab_geometry">Manhattan</a> (sometimes referred to as taxicab) metrics. The first defined by function: $d(x,y) = \sqrt{\displaystyle\sum_{i=1}^{n}(x_i-y_i)^2}$ and the other one by: $d(x,y) = \displaystyle\sum_{i=1}^n |x_i-y_i|$. Voronoi diagrams are very useful. There can be found in many aspects of our lives: in natural sciences, engineering, geometry, computer games or even in medicine. The main goal of the post is to introduced to the reader some basics concepts and thought regarding the Voronoi diagram. I prepared some basics scripts in <code class="language-plaintext highlighter-rouge">gnuplot</code> and <code class="language-plaintext highlighter-rouge">python</code> to present to you some methods for solving this kind of problem. Let’s begin with the simple brute-force algorithm in <code class="language-plaintext highlighter-rouge">python</code>. From now on we will be focus only on $n=2$ case. Below a simple program is presented. Here we assume that we work in a square area: $(x,y)\in \mathbb R^2, 0 \le x,y \le$<code class="language-plaintext highlighter-rouge">L</code>. we generate 20 <code class="language-plaintext highlighter-rouge">points</code> and we sample points in a square with <code class="language-plaintext highlighter-rouge">L/samples</code> steps and for each we check, which point from <code class="language-plaintext highlighter-rouge">data</code> is the nearest.</p> <figure class="highlight"> <pre><code class="language-python" data-lang="python"><span class="c1">#!/bin/python </span><span class="kn">import</span> <span class="nn">random</span> <span class="kn">import</span> <span class="nn">sys</span> <span class="n">L</span> <span class="o">=</span> <span class="mf">10.</span> <span class="n">samples</span> <span class="o">=</span> <span class="mf">1000.</span> <span class="n">points</span> <span class="o">=</span> <span class="mi">20</span> <span class="n">data</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">points</span><span class="p">):</span> <span class="n">x</span> <span class="o">=</span> <span class="n">L</span> <span class="o">*</span> <span class="n">random</span><span class="p">.</span><span class="n">random</span><span class="p">()</span> <span class="n">y</span> <span class="o">=</span> <span class="n">L</span> <span class="o">*</span> <span class="n">random</span><span class="p">.</span><span class="n">random</span><span class="p">()</span> <span class="n">data</span><span class="p">.</span><span class="n">append</span><span class="p">([</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">])</span> <span class="k">def</span> <span class="nf">d</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">):</span> <span class="c1">#euclidan </span> <span class="c1">#return (x-y)**2 + (x-y)**2 </span> <span class="c1">#metro metric </span> <span class="k">return</span> <span class="nb">abs</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-</span><span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="nb">abs</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">-</span><span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="k">def</span> <span class="nf">min_d</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">P</span><span class="p">):</span> <span class="n">MIN</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">P</span><span class="p">:</span> <span class="n">MIN</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">d</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">i</span><span class="p">))</span> <span class="k">return</span> <span class="n">MIN</span><span class="p">.</span><span class="n">index</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">MIN</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">samples</span><span class="p">)):</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">samples</span><span class="p">)):</span> <span class="n">px</span> <span class="o">=</span> <span class="n">i</span> <span class="o">*</span> <span class="n">L</span><span class="o">/</span><span class="n">samples</span> <span class="n">py</span> <span class="o">=</span> <span class="n">j</span> <span class="o">*</span> <span class="n">L</span><span class="o">/</span><span class="n">samples</span> <span class="k">print</span> <span class="n">px</span><span class="p">,</span><span class="n">py</span><span class="p">,</span><span class="n">min_d</span><span class="p">([</span><span class="n">px</span><span class="p">,</span><span class="n">py</span><span class="p">],</span><span class="n">data</span><span class="p">)</span></code></pre> </figure> <p>After the procedure, one can visualize the results using <code class="language-plaintext highlighter-rouge">gnuplot</code> program. Our output has the following form: $x$, $y$, $i$, where $i$ is the index of subspace $A_i$.</p> <figure class="highlight"> <pre><code class="language-bash" data-lang="bash"><span class="c">#!/bin/gnuplot</span> <span class="nb">unset </span>key <span class="nb">unset </span>colorbox <span class="nb">unset </span>tics <span class="nb">set </span>border 0 <span class="nb">set </span>size ratio 1 <span class="nb">set </span>xr[0:10] <span class="nb">set </span>yr[0:10] filename <span class="o">=</span> <span class="s1">'&lt;python voronoi.py'</span> p filename u 1:2:3 w image</code></pre> </figure> <p>where <code class="language-plaintext highlighter-rouge">voronoi.py</code> is the filename of the previous python script. Here you have an example and comparison of these two different metrics on the same random points:</p> <figure style="text-align: center"> <p><img src="voronoi1.png" width="50%" /><img src="voronoi2.png" width="50%" /></p> <p>Fig. 1: <em>An example Voronoi diagram for Euclidean (left) / Manhattan (right) metric.</em></p> </figure> <p>As you can see the result with the Manhattan metric is similar to a polyline. Each area’s border is a polyline, in contrast to the Euclidean metric, where borders between neighbors’ areas are just straight lines. In many blogs, websites, people explain Voronoi diagrams by using some graphical programs. By generating cones, one can find the Voronoi diagram (for Euclidean metric) by simply searching for the intersection between them. However, I have never seen a solution to the problem, which was fully designed in <code class="language-plaintext highlighter-rouge">gnuplot</code>. <code class="language-plaintext highlighter-rouge">Gnuplot</code> is plotting and also a graphical program. Most of the community presents an example only using the cones, but similar understanding can be considered for other geometric objects, for example for pyramids to solve the problem in Manhattan metric.</p> <figure style="text-align: center"> <p><img src="cone.png" width="50%" /><img src="pyramid.png" width="50%" /></p> <p>Fig. 2: <em>Cone/pyramid landscape - graphical method for finding Voronoi diagram.</em></p> </figure> <p>The problem comes down to simply drawing the selected geometrical shapes. Then one should only map these objects into $xy$ plain. Here a <code class="language-plaintext highlighter-rouge">gnuplot</code> example is presented:</p> <figure class="highlight"> <pre><code class="language-bash" data-lang="bash"><span class="c">#!/bin/gnuplot</span> <span class="c"># square dim</span> L <span class="o">=</span> 10 <span class="c"># number of points</span> N <span class="o">=</span> 20 <span class="c"># radius</span> r <span class="o">=</span> 3 <span class="c"># euclidean metric</span> <span class="c"># d(x,y) = sqrt(x**2+y**2)</span> <span class="c"># manhattan metric</span> d<span class="o">(</span>x,y<span class="o">)</span> <span class="o">=</span> abs<span class="o">(</span>x<span class="o">)</span>+abs<span class="o">(</span>y<span class="o">)</span> f<span class="o">(</span>x,y,x0,y0,r<span class="o">)</span> <span class="o">=</span> <span class="o">(</span>d<span class="o">(</span>x-x0,y-y0<span class="o">)</span>&lt;r<span class="o">)</span>? <span class="nt">-d</span><span class="o">(</span>x-x0,y-y0<span class="o">)</span> : 1/0 <span class="c"># random seed</span> sys <span class="o">=</span> rand<span class="o">(</span>int<span class="o">(</span><span class="sb"></span><span class="nb">date</span> +%s.%N<span class="sb"></span><span class="o">))</span> <span class="c"># some settings</span> <span class="nb">set </span>border 0 <span class="nb">unset </span>tics <span class="nb">unset </span>colorbox <span class="nb">set </span>view 0,0 <span class="nb">set </span>isosamples 100 <span class="nb">set </span>samples 1000 <span class="nb">set </span>view equal xy <span class="nb">set </span>surface <span class="nb">set </span>hidden3d <span class="nb">unset </span>key <span class="nb">set </span>ur[0:L] <span class="nb">set </span>vr[0:L] array A[N]<span class="p">;</span>array B[N]<span class="p">;</span> <span class="k">do for</span> <span class="o">[</span><span class="nv">i</span><span class="o">=</span>1:N] <span class="o">{</span> A[i] <span class="o">=</span> rand<span class="o">(</span>0<span class="o">)</span><span class="k">*</span>L B[i] <span class="o">=</span> rand<span class="o">(</span>0<span class="o">)</span><span class="k">*</span>L <span class="o">}</span> <span class="nb">set </span>pm3d depthorder sp <span class="k">for</span> <span class="o">[</span><span class="nv">i</span><span class="o">=</span>1:N] <span class="s1">'++'</span> u 1:2:<span class="o">(</span>f<span class="o">(</span>x,y,A[i],B[i],r<span class="o">))</span>:<span class="o">(</span>i<span class="o">)</span> w pm3d</code></pre> </figure> <figure style="text-align: center"> <p><img src="d.gif" width="50%" /><img src="c.gif" width="50%" /></p> <p>Fig. 3: <em>Maping cone (left) / pyramid (right) landscape into plain - Voronoi diagram.</em></p> </figure> <p>In the previous code snippet, there is another variable, which can be changed: distance radius <code class="language-plaintext highlighter-rouge">r</code>. Changing <code class="language-plaintext highlighter-rouge">r</code> value, creation of the intersection between objects can be seen. In the next animations, this phenomenon was presented.</p> <figure style="text-align: center"> <p><img src="a.gif" width="50%" /><img src="b.gif" width="50%" /></p> <p>Fig. 4: <em>Radial growth of Voronoi diagram: Euklidean (left), Manhattan (right) metric.</em></p> </figure> <p>In this post, the simplest solutions to these kinds of problems were presented. There are many more (much more sophisticated) algorithms. Probably the famous one: <a href="https://en.wikipedia.org/wiki/Fortune%27s_algorithm">Fortune’s algorithm</a>.</p> <p style="text-align: right"> AW </p>Voronoi diagram is a diagram for set $P$ of $N$ points from $\mathbb R^n$ space (generally to any set $X$). It is constructed by splitting $\mathbb R^n$ space into $N$ subspaces $A_i$, in such a way that arbitrary point $x\in\mathbb R^n$ belongs to $A_i$, only if point $p_i\in P$ is the nearest point to $x$ for given metric $d$. From the mathematical perspective one can define each subspace in the following way:Welcome to my page!2018-07-22T12:00:00+00:002018-07-22T12:00:00+00:00http://andywiecko.github.io/first/post/2018/07/22/welcome-post<p>Welcome to my homepage!</p>Welcome to my homepage!