Posts

Showing posts with the label visualization

Random maze generation

Image
Pt En In this post I just want to share a simple algorithm that I used to create random mazes. The idea came from an e-mail I got, about a past competition, where one of the contestants did this exact thing: a program to generate random mazes. I saw the animation of the program working here and I deduced how to do it. All the code can be found on GitHub , as well as an executable of the program, the animations from the beginning and end of this post, an image of a bigger maze, and this other animation: where you can see a different style of maze; a less straight one. The maze starts in the top left red corner and ends wherever the other red square is, which need not be on the bottom right corner. The algorithm is simple: travel randomly inside the black area without ever hitting a white path; whenever no random move can be made, start going back until you find a place where the path can branch out again. While we are creating white paths, keep updating the final position to be ...

Solving mazes with programming

Image
Everyone knows what mazes/labyrinths are! And plenty of us find it amusing to try and solve one! Take the image above as an example... Can you find a path from the top left opening to the bottom right opening? There exists at least one! I wrote a program in Python (making use of pygame and Pillow) to solve any maze given. I did this project several years ago, after learning about Dijkstra's algorithm . I read about the algorithm and decided to implement it, to get a feel for it. I realized that I could apply it to a maze in a picture, if I took each pixel of the image to be a vertex in the graph where I'd apply the method. I wanted to make my program fairly general, in terms of the mazes that could be given, and so I dwelled with a problem: how to tell if two adjacent pixels should be connected in the graph or not. I knew I only wanted to connect pixels that referred to the path, and not to the walls, so my idea was simple: I supposed that a picture of a maze would have mainly ...

Random Walks to solve Diophantine Equations

Image
Random walks (check my older post here ) and Diophantine equations are two simple mathematical beasts. After a college seminar, I tried putting them together to make something neat, and came up with this: just pick a Diophantine equation, simulate a random walk, and try to see if the random walk went over any solutions! In this report I briefly go over how I came up with this, and show the code I wrote/some results. The Matlab code is all in here (I also have some Python code about random walks in general here , check my post !). In the image above, the blue line represents a path taken by the random walk and in orange/red the nodes tell us how far we are from a solution (the smaller the node, the closer we are). Note that this notion of proximity isn't given by actually computing the distance to a known solution, but just by looking at how similar the two sides of the equation are. The image below shows the evolution of three independent random walks trying to find an example ...

Two-dimensional random walk simulations

Image
Think of a drunk man that continuously tumbles left and right, back and forth, with no final destination. That can be thought of as a random walk on the plane... Now imagine that the drunk man has a shorter leg and tumbles more to one of the sides: that is a biased random walk. Now imagine the drunk man can teleport to a nearby location. That is (kind of) a Lèvy flight ! All those are quite interesting to observe in motion and I implemented them in Python. (the code can be found here ). From those, only the tilted walks and the one with the trail are such that the particle's movement wraps around the borders (leaving the green area makes the particle appear in the opposite side, just like the snake from the game Snake). The image above is a screenshot of the execution of the random walk that leaves a coloured trail. I also implemented an animation where the screen gets progressively filled with circles of different colours, as if being splattered with paint. Not sure why... But it...

Fractals and the Filled Julia set

Image
The Filled Julia set is another fractal that can be built with the complex numbers, just like the Mandelbrot set. To build a Filled Julia set, we pick a complex number c and then repeatedly apply f(z) = z^2 + c to every point of the plane. If that iterative application of f produces a sequence that goes to infinity, the point does not belong to the Filled Julia set. If it does not go to infinity, then the point belongs to the Filled Julia set and is coloured black. Using Python and pygame I developed a small script that allows the user to click a point on the complex plane and create its Filled Julia set. The code, which is very simple, can be found here . A Windows executable can be downloaded from here . Pressing the left arrow shows the previous Filled Julia sets you created, while the right arrow takes you to the more recent ones. Pressing the spacebar erases the current fractal and shows the initial black axis. [Pt] O 'Filled Julia set' é outro fractal que pode ser def...

Fractals and the Mandelbrot set

Image
I have always liked the concept of fractal. They are very beautiful, they have a notion of infinity embedded in it, and they make no sense (seriously though, self-similarity ?). How couldn't they be loved? Despite being fond of fractals, I had never understood them because I didn't know how to mathematically define one. I knew how to draw some, for example the snowflake or the Sierpinski triangle , but drawing and mathematical definitions aren't quite the same thing. Enlightenment struck after watching this video from Numberphile about the Mandelbrot set, the fractal I presented above. After all, a fractal like this was not but a simple formula and a check for a bound on a sequence! I recommend all of you to watch that video. Simplifying it a lot, for each number you can create a sequence. After you do that, you check if the numbers in that sequence explode or remain small. After learning how to create the Mandelbrot set I put that knowledge to practice, making use of my ...

A gradient descent algorithm to optimally distribute points in a sphere

Image
In this first post I want to share with you guys a piece of code I wrote to "solve" a problem where geometry meets optimization. I say "solve" because I didn't actually do anything that fantastic regarding the actual problem I address, but rather developed a small tool to help visualize the geometrical part of the problem. Even so, I do believe that for the smaller cases my tool can solve the problem. The problem is along the lines of: define an energy function whose value depends on the positions of points in a sphere, and now try to minimize/maximize it (depending on a parameter). That is it. I used my coding skills to write an algorithm that solves this when the number of points is small, and that lets me see the creation of the solution: I create a random possible distribution of points and then let them adjust themselves to their desired positions, hopefully reaching the desired minimum/maximum. In here you can find the report I wrote for this, in English....