Turning your phone into a virtual-joystick

Image
Update: I kept working on this and I have released it as a package for Windows, Linux and macOS. Check it out: https://github.com/zenineasa/joystick/releases/tag/v1.0.0 -- During the days when I was pursying my master's programme, my friends and I used to occasionally go to a classroom in the university, turn on a projector, connect devices like Nintento Switch or Gaming computers and loads of joysticks, and play different simple multiplayer games; MarioKart was my favourite. From time-to-time, when I get together with people, I ponder that it would be a good idea if I bought such devices. Indeed, I do have a laptop, which could easily run such games; SuperTuxKart is similar enough to MarioKart and it can run on Linux, Windows and Mac. However, I do not have joysticks with me at the moment. Therefore, I think it would be a good idea if I simply worked on a project that would enable using our phones as joysticks. From a high-level, the plan is to host APIs on a NodeJS server that wo

Estimating the value of Pi using Monte Carlo Method

The very first moment I was shown this, I was amazed. Estimating something using random events is pretty amazing. The first time I did this in MATLAB, but now, I think I will attempt to do the same in JavaScript.

The undelying concept of Monte Carlo Method is to use randomness to determine the value. The method is often used to solve problems that are hard to determine exactly, when there is enough statistical data available.

Let us define a square centered at the origin with side of length 2. A circle centered at the origin is inscribed in this square.


The assumption that we are using in this method is that if we randomly take a few points within the square, the ratio of the number of points that falls within the circle to the total points taken is equal to that of the area of the square and the circle. That is,

Area of square / area of circle = totalRandomSamples / totalPointsInCircle
Let us write a JavaScript program that could do perform this with a huge number of random sample points generated on the go. To check if the generated point lies within the circle or not, we use pythogoras theorem to calculate the distance from the center.

var squareSide = 2;
var circleRadius = squareSide/2; 
var totalRandomSamples = 100000;
var totalPointsInCircle = 0; 
for(var i = 0; i < totalRandomSamples; i++){
    // A random point contained within the square
    var newX = Math.random() * squareSide - squareSide/2;
    var newY = Math.random() * squareSide - squareSide/2;
    // Check if the new coordinate is inside or outside the circle
    if(Math.sqrt(newX * newX + newY * newY) < circleRadius){
        totalPointsInCircle++;
    }
// Area of square / area of circle = totalRandomSamples / totalPointsInCircle
// (squareSide * squareSide) / (pi * circleRadius * circleRadius) = totalRandomSamples / totalPointsInCircle
var pi = squareSide * squareSide * totalPointsInCircle / (circleRadius * circleRadius * totalRandomSamples); 
console.log(pi);

The value of 𝜋 calculated using the method may vary everytime one runs the code. If you wish to improve the accurace, you can increase the number of Random Samples being used. Also, a better algorithm for generating random points may also be used.

I find that the value calculated is usually within 1% accuracy. The fact that this method simply uses cartesian coordinates to calculate 𝜋 is something that blew my mind.

Comments

Popular posts from this blog

First impression of Lugano - Mindblowing

Thinking about developing an opensource P2P social network

From Correlation to Causation through stories and math