
A geek meets the Ikea Lillabo Train Set - jgrahamc
http://www.jgc.org/blog/2010/01/more-fun-with-toys-ikea-lillabo-train.html
======
Hates_
Fascinating and depressing at the same time. Posts like this make me think I'm
in the wrong job as if asked to write a similar program, I'd have no idea
where to start. JGC labels this as a "small program", like it was knocked up
on a whim :(

~~~
jgrahamc
It was knocked up on a whim, and it is small, but don't be depressed: it
wasn't easy...

I had to bang my head against the trigonometry to get it all correct. Although
I know the rules of trig, I always have a hard time applying them in the real
world because I have a hard time imagining the shapes involved. Ended up
drawing little pictures of what was doing on.

~~~
torpor
I think it would be quite interesting to know more about how you approached
this problem for solution, than the solutions presented by the program itself
.. I didn't really get much of the details about how you'd sorted through this
from your post .. perhaps a followup topic?

Its really great, btw .. I play with this toy with my son on a regular basis,
and found myself just a few days ago wondering what is the maximum number of
shapes we can build with the set .. okay, this got a bit screwed up by the
addition of another mini-set gifted to my son by a friend, so there is room
for improvement in my thinking now that I've learned what you have discovered.
Nice job!

~~~
jgrahamc
I tried to explain how it works in the comments in the code. Probably worth
reading them in detail as they do explain the algorithm used.

My thought process was something like this (over a number of days grabbing
time when I could):

Emulate a human selecting pieces and clicking them together. Model pieces as
straight lines of the right length. There are two important things: how long
the line is and how much it modifies the angle at which the next piece will
join. Thus all I need is to keep track of the position of the end of the last
piece added and the angle.

Spend a while figuring out the trigonometry and getting that code working
correctly. To generate the permutations I realized that the process could be
split into three parts:

1\. Always place the bridge. Since there's one just add it at the end.

2\. Separate the straights and curves. Generate all the permutations of
clockwise and anticlockwise curved pieces. To do that I used the trick of all
12-bit binary numbers between 0 and 2^12-1.

3\. Then insert the straight pieces between the curved pieces in all places
possible.

Feed each possible track layout into code that builds the track and check that
the end pieces match up. Add code to eliminate end pieces that join impossibly
(come from the wrong angle), add code to remove duplicates caused by rotation
and reflection.

Once I got that working I then wrote code in Processing to draw a pretty
picture.

~~~
fhars
You might be able to speed it up considerably by adding

2a. Continue with next iteration unless the turning number of the sequence of
curves is a whole number.

You add 1/8 to the turning number for each clockwise curve and subtract 1/8
for each CCW (for numerical stability you would rather add of subtract 1 and
check for multiples of 8).

------
jazzyb
I just noticed that the bridge can be divided into two parts. Have you thought
about how many new track patterns you can create if you allow some of the
pieces to be elevated and use the two separate parts of the bridge as slopes
onto and off of the elevated track?

~~~
pmorici
There are special ramp pieces you are supposed to use for that purpose and
bridging blocks that allow you to elevate regular track. I've never heard of
this brand but it used to be called "Brio" when I was a kid,
<http://brio.knex.com/>

~~~
ErrantX
I have a box full of brio from my childhood at home - and, yeh, they did have
blocks to let you make raised track.

(it was a pain though because it was easiest to build the bridges first - and
then just as you laid the last piece of track a trip/wobble/stumble collapsed
the raised bits :P)

------
bockris
My son got a similar train track when he was little but with a lot more
pieces. I wanted to write something similar but never got around to it. One
twist is that his set has several sets of forks and some pieces to make an
elevated track that can double as a bridge. I'm going to look through the
program and see if I can add these types of pieces. We always just made up the
tracks as we went and quickly learned to follow some rules so the track would
loop but not reverse the train because we had 2 sets of trains. It became a
contest between my wife and I to see who could come up with the coolest
layout. It was lots of fun.

------
jcnnghm
I've got a whole crate full of Brio wooden trains from when I was a kid,
including up/down pieces. I bet if I modified this software to work in three
dimensions it could build millions of configurations with the parts I've got.
Could be fun.

~~~
niels_olson
My brothers and I have split our massive compiled collection of Brio for our
kids. It makes me sad that the company is no more. I've heard bad knockoffs
are now available in discount stores, but they don't fit the original track.

~~~
yason
Since when has Brio ceased to be a company?

They're on the internet and I've seen Brio trains in toy departments each year
-- bought some extensions for my own kids a few years ago, too.

------
petermarks
I've been using jashkenas' ruby processing for a simple gui:
<http://wiki.github.com/jashkenas/ruby-processing> . Processing is a pleasure
to use once it's liberated from java syntax.

------
pmorici
If anyone's interested in more complex wooden train sets...
<http://www.woodentracks.com/CTGY/Toy_Train_Sets.html>

------
yardie
I've got the same train set at home. I also bought the extra parts with the
fork an extra brigde, trestles, and tunnel. Sounds like I've got a new project
to help me learn processing.

------
ivankirigin
Awesome. I'd bet most parents reading this have two sets. You should update
your post with all the other possible configurations. How many 3 and 4 leaf
clovers can you make?

------
gaborcselle
How many shapes could you form with 2 of these train sets? I think each one
costs just $10 or so.

~~~
graywh
$20

The $10 set is smaller.

------
dangoldin
I've also started messing around with Processing. I've been using it to
generate some simple art.

------
hussong
This is simply amazing, John. I wonder what you could do with two sets...

