School Assignment: Magic Squares

On keeping up with the regular bullshit at school; I get to skip a part off my minor. Anyhow, this one is on doing ‘algorithms and data structures’.

Recently someone finally gave a decent explanation about arrays. That was about time after three years of computer science classes. Great. Even better, we get to use those now, by generating magic squares.

Magic squares are great. The goal with these things is that when summed up, every row, column or diagonal has the same value as shown in the example above. There’s an algorithm for generating these things. I’ve implemented a sloppy algorithm which can generate these things as long as the row and column size are an odd number;

int num = 5;
int[,] matrix = new int[num, num];

var x = ((num) / 2);
var y = num-1;

int i = 1;

int upper = num\*num;

var prev_x = x;
var prev_y = y;

while (i <= upper)
{
if (matrix[x, y] == 0) {
matrix[x, y] = i;
i++;

prev_x = x;
prev_y = y;

x = x < num - 1 ? x + 1 : 0;
y = y < num - 1 ? y + 1 : 0;
} else {
x = prev_x;
y = prev_y - 1;
}
}

matrix.Dump();


I can definitely recommend LinqPad for playing with fiddles like this as it will give a clear view of what you are doing.

Visualizing Squares

While we can generate magic squares now there’s bonus points for visualizing these. Hmm. How does one go, without having to bundle LinqPad. There’s like two options here;

1. You do bundle LinqPad
2. You start generating grids in a console app
3. You think of something else

Originality for the win! Actually this is the part which took more time then programming the algorithm itself.

So the go to solution was to generate bitmaps of these grids, each pixel’s color representing a value on the grid. This one was actually combined with stress testing the algorithm to see if I could generate a 24 bit grid or more, which succeeded by the way. There was no reason to go higher though, as there are no more then like 16 million colors available.

The trick was in converting a value between 0 and 2^24 to a RGB color value. See the example below;

using (var image = new Bitmap(num, num)) {
var multiplier = 16777216 / upper;

for (var i = 0; i < num; i++) {
for (var ii = 0; ii < num; ii++) {
var hex = (matrix[i, ii] \* multiplier).ToString("X8");

var color = Color.FromArgb(
(byte)255,
(byte)(Convert.ToUInt32(hex.Substring(6, 2), 16)),
(byte)(Convert.ToUInt32(hex.Substring(2, 2), 16)),
(byte)(Convert.ToUInt32(hex.Substring(4, 2), 16))
);

image.SetPixel(i, ii, color);
}
}

image.Save(\$"./{Guid.NewGuid().ToString()}.bmp");
}


The results, when playing with color codings, can be quite astonishing, epileptic attack inducing and trippy at the same time:

Thankfully there is no need to copy paste this code as it’s all available on GitHub.

Cumulus Granitus (Soaring through the mountains)

This spring I have had the amazing possibility to go to Sisteron and enjoy one of the most awesome things there is to enjoy. Mountain (wave) soaring.

Rides will be bumpy. Weather may be quite cold and windy, all while burning in the sun. But the beer (and food) will be good.

Flying at the WBAC

Flying aircraft, and especially gliders is definitely fun! Check out this vid for a little impression about flying at the West Brabantse AeroClub (WBAC).

Luisterbieb Windows Phone App

Recently I’ve been working on a ‘luisterbieb’ application for Windows Phone. ‘luisterbieb’ means something like audio library in Dutch. The main point behind this application is that users should be able to listen to many well known books. (There are around 300 available right now.)

This application is already available for the Android and iOS platforms. The main reason for me to develop a ‘clone’ for the Windows Phone platform is due to me always having my Windows Phone with me while traveling, and not an iOS or Android device.

I’ve almost completed this application. I can navigate through books, I can select which books I want to listen and so on, but, there’s one thing which prevents me from finishing this application.

When I started this application I thought it’d be as simple as shit because I had access to their API and I saw the direct links to their MP3 files were available. I tried to listen to an excerpt which worked. HOWEVER, it turned out these developers have applied some kind of encryption on their actual MP3 files (not the excerpts) which means I can’t play these files.

At the time of publishing I’ve just mailed the developers of this application with the request to help me play this file. Of course they have their reasons to encrypt these sound files. One of them is to prevent it from leaking to the web. Whatsoever, I dare to bet Windows Phone is at least more safe than the Android platform, and maybe even the iOS platform.

There are two possible outcomes on my request:

They deny and the development of the app will stop. They accept and I can finish this application, and publish it to the store. I truly hope the last is going to happen. Maybe in the first place so I can listen to these books when I’m traveling, and at second so I can publish a nice application to the Windows Phone store most of the Dutch Windows Phone users can use.

Let’s hope for the best!

EDIT (2015):

Despite the initial enthousiasm for making it possible to release this application the people from the body which nationwide governs the libraries have decided not to help with this application by providing the encryption keys. There were three problems (in chronological order):

• At first I didn’t have a legal entity which is required for a contract between me and the governing body.
• At the time I could use a legal entity the body was reorganizing it’s organisation.
• After the reorganization there were a lot of things which had higher priorities.
• Now in the summer of 2015 (Roughly a year later) they have expressed not to help with this project because they are actively developing a new (long planned) version of the application which means I’d have to update the application. Sadly they are not keen on providing documentation on their new API.

Maybe I’ll take another try when the application is released and there is no Windows (Phone) version yet.