Phillip Trelford's Array

POKE 36879,255

Random Art

Earlier in the year I came across a Stanford coding assignment inspired by Andrej Bauer’s Random Art. Pictures are built using randomly chosen mathematical expressions that take an x and y value and return a colour. The implementation on Andrej’s site uses OCaml, but is closed source, however a clear Python example is given.

F# version

The Python version worked out-of-the-box but took a while to render (10s of seconds) so I rewrote it in F# (a language based on OCaml) to improve generation time (to less than a second).

The random pictures are unsurprisingly a bit hit and miss so I set up a script to generate batches of 1000 and then sifted through to find ones I liked, here’s a few examples:

Random0093 tartan
Random0937 Random - Copy (9)

 

The image generation is a heavy compute task, running on my i7 Desktop was noticeably faster than my MBP. For yet faster generation the F# code was trivial to parallelise using the Array.Parallel module.

You can run the F# version on Linux, Mac or Windows, just run this snippet as an F# script: http://fssnip.net/si

 

Go version

I’ve been picking up the Go programming language recently, just out of curiosity, and thought this would be an interesting task to try.

Initially I’d used Notepad and the command line on Windows, for this task I switched to Mac and the Atom editor with go-plus, which gives syntax colouring and some code completion and appears to be a relatively popular choice nowadays:

Go Editors

Aside: for editing F# in Atom try the excellent atom-fsharp

Rather than building a UI, I simply used Go’s image package which supports setting pixels and saving images.

Here’s a skeleton using a fixed function:

To produce random art I used an interface to define expressions with each type defined using a structure and associated evaluation function. The expressions are then randomly selected and then composed. The source is available as a gist.

Here's some pictures from the first run:

output472 output518
output164 output670

 

Comparing implementations

The F# and Go implementations feel quite close. In F# I used a discriminated union to define the expression types and functions for evaluation. Similarly in Go I used structures to define the expression data and functions for evaluation. In both cases separating the concerns of data representation and evaluation. In effect, if we ignore the curly braces, programming in Go feels to me more akin to programming in F# than programming C# or Java. Other similarities include multiple return values in Go and first class tuples in F#, and Go’s defer and F#’s use keyword for simple resource management.

 

Hands On Random Art Class

If you’re interested in producing your own random art, I’ll be running a free class at the F#unctional Londoners in October, where we’ll explore some different formulas.

Introducing FunSharp

FunSharp is a new cross-platform open source graphics library, based on Small Basic’s library, with a typed API crafted for the sharp languages, F# and C#.

Drawing graphics is quick and easy:

GraphicsWindow.FillEllipse(10,10,300,300)

And when you ask FunSharp to draw graphics it will just go and open a window to view it for you:

Purple_Circle

FunSharp provides similar functionality to Python’s PyGame, used for developing games, and Processing used for visual art.

You can call FunSharp immediately from the F# REPL or build full-blown applications using F# and C#.

It’s an ideal library for beginners or anyone who just wants  to get stuff on the screen quickly.

With this in mind FunSharp works seamlessly on Raspberry Pi, Linux and Windows.

Getting Started

If you haven’t already, you’ll need to install F# on your machine, follow these instructions:

  • Linux (for Raspbian follow the Debian instructions)
  • Windows (install Xamarin Studio or Visual Studio)

Get the FunSharp source from GitHub using Clone or Download Zip, then load the FunSharp solution into MonoDevelop or Visual Studio and starting playing with the samples.

Notes:

  • FunSharp is built on Mono’s cross platform graphics libraries Xwt and Gtk# libraries which must be referenced.
  • currently apps must run in 32-bit mode.

Turtles

FunSharp has a Turtle module, which can be used to make fun shapes:

Turtle.X <- 150.
Turtle.Y <- 150.
for i in 0..5..200 do
   Turtle.Move(i)
   Turtle.Turn(90)

Renders:

Turtle_Example

Games

FunSharp can be used to make games quickly and easily. I’ve ported several games written for Small Basic requiring only minor modifications:

Asteroids

Have fun!

Flappy

This week I ran a half-day hands on games development session at the Progressive .Net Tutorials hosted by Skills Matter in London. I believe this was the last conference to be held in Goswell Road before the big move to an exciting new venue.

My session was on mobile games development with F# as the implementation language:


Here’s a quick peek inside the room:


The session tasks were around 2 themes:

  • implement a times table question and answer game (think Nintendo’s Brain Training game)
  • extended a simple Flappy Bird clone

Times table game

The motivation behind this example was to help people:

  • build a simple game loop
  • pick up some basic F# skills

The first tasks , like asking a multiplication question, could be built using F#’s REPL (F# Interactive) and later tasks that took user input required running as a console application.

Here’s some of the great solutions that were posted up to F# Snippets:

To run them, create a new F# Console Application project in Xamarin Studio or Visual Studio and paste in the code (use the Raw view in F# Snippets to copy the code).

Dominic Finn’s source code includes some fun ASCII art too:

// _____ _   _ _____ _____ _____  ______  _  _   _____  _____ _     
//|  __ \ | | |  ___/  ___/  ___| |  ___|| || |_|  _  ||  _  | |    
//| |  \/ | | | |__ \ `--.\ `--.  | |_ |_  __  _| | | || | | | |    
//| | __| | | |  __| `--. \`--. \ |  _| _| || |_| | | || | | | |    
//| |_\ \ |_| | |___/\__/ /\__/ / | |  |_  __  _\ \_/ /\ \_/ / |____
// \____/\___/\____/\____/\____/  \_|    |_||_|  \___/  \___/\_____/
//  

Flappy Bird clone

For this example I sketched out a flappy bird clone using Monogame (along with WinForms and WPF for comparison) with the idea that people could enhance and extend the game:

image

Monogame lets you target multiple platforms including iOS and Android along with Mac, Linux, Windows and even Rapsberry Pi!

The different flavours are available on F# Snippets, simply cut and paste them into an F# script file to run them:

All the samples and tasks are also available in a zip: http://trelford.com/ProgNet15.zip

Have fun!