My blog. Like it? Subscribe to my feed or check out the archives.

Bresenhams Line Algorithm in C# May 20

1 comment

I recently implemented Bresenham's line algorithm in C# as part of a new game I am developing. I thought I would share my implementation. I particularly like the interface of returning the points on the line as an IEnumerable.

public static IEnumerable<Point> GetPointsOnLine(int x0, int y0, int x1, int y1)
{
    bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
    if (steep)
    {
        int t;
        t = x0; // swap x0 and y0
        x0 = y0;
        y0 = t;
        t = x1; // swap x1 and y1
        x1 = y1;
        y1 = t;
    }
    if (x0 > x1)
    {
        int t;
        t = x0; // swap x0 and x1
        x0 = x1;
        x1 = t;
        t = y0; // swap y0 and y1
        y0 = y1;
        y1 = t;
    }
    int dx = x1 - x0;
    int dy = Math.Abs(y1 - y0);
    int error = dx / 2;
    int ystep = (y0 < y1) ? 1 : -1;
    int y = y0;
    for (int x = x0; x <= x1; x++)
    {
        yield return new Point((steep ? y : x), (steep ? x : y));
        error = error - dy;
        if (error < 0)
        {
            y += ystep;
            error += dx;
        }
    }
    yield break;
}

Random Mazes using Eller Apr 18

1 comment

Recently I came across Eller's algorithm for generating random mazes. It is particularly clever in that the algorithm generates mazes row by row without storing the entire maze in memory. If your browser supports it you should see a maze and its solution below, drawn on an HTML5 Canvas element. I generate solutions for the mazes using Dijkstra's algorithm. I implemented both algorithms in Javascript. Reloading the page will generate a new maze.

Your browser does not support the <canvas> element.

The code is available in Mercurial under an MIT license.

Peg Solitaire: My New Mini Game Jan 08

Post a comment

Over the holidays I created a new game, Peg Solitaire. Click on the link to go play!

Smallest Palindrome Bases in Haskell 26 Dec 09

Post a comment

Everybody loves palindromes, right? I do, at least. That's why I was excited when I learned that every number can be a palindrome when it's written in the appropriate base. There is a trivial proof for this property: any number N > 3 is a palindrome in base N-1 because it may be written "11". So here is my solution, in Haskell, to this CodeChef problem, that finds the smallest base that makes any given number a palindrome.

isPalindromeInBase :: Integer -> Integer -> Bool
isPalindromeInBase value base = step leftmost 1
    where leftmost = base ^ floor(log(fromInteger value) / log(fromInteger base))
          step left right
              | left <= right             = True
              | digit left == digit right = step (left `div` base) (right * base)
              | otherwise                 = False
          digit position = (value `div` position) `mod` base

smallestPalindromeBase :: Integer -> Integer
smallestPalindromeBase 1 = 2
smallestPalindromeBase 2 = 3
smallestPalindromeBase value = step 2
    where step base 
              | base >= value || isPalindromeInBase value base = base
              | otherwise                                      = step (base + 1)

main = interact (unlines . map (show . smallestPalindromeBase . read) . tail . lines)

This is certainly not the fastest solution possible. Indeed it is downright naive. But hopefully the logic is clear.

A Month of Fishing Girl 17 Oct 09

2 comments

I released Fishing Girl just under a month ago and yesterday I broke 5000 sales. Thank you, everyone, for your support! For me, knowing so many people are having fun with my game is the ultimate reward.

I'm also very happy that Fishing Girl was so well received by the gaming press. I've been collecting reviews of the game and I've linked them all below, in vaguely chronolgical order.

Fishing Girl has also appeared elsewhere on the web and I've collected some of those links, too. This certainly isn't an exhaustive collection but it highlights some of the more interesting references I've stumbled on.

Any more Fishing Girl sightings you'd like to point out? Let me know in the comments!