previous

page 31

next
the Second Last Bug!

Programmers jokingly refer to the bug they have just found as "the second last bug", ruefully admitting that a program is unlikely to be ever bug-free.

What? You can't see the bug in the picture?

Click me for the answer.

Clicking GIVE UP revealed the expected answer to be 22141.

To make matters worse, I found several other examples!

So, we need a way to directly compare the pictures. Fortunately there is a way to do this. We are able to grab the entire canvas using the getImageData() function.

But sadly that only works if you're using a web server and I was determined to have these tutorials avoid that complication. Finally I came up with the idea of comparing the line segments that define the patterns. Took a while to work out an efficent way to do this (see below). I stored them in two arrays making sure that each line segment was only included once and then compared the arrays.

how store() works

I reduced the x and y values to a single digit by dividing by g_step and rounding the result. An example will best explain the rest: v234 is a vertical segment between y values 2 and 3 at x value 4.

previous
next