Thursday, July 21, 2011

Vess Cola, Part 5.

I had to tinker a while but I think I found a nice way to draw the pieces with ImageMagick's '-draw' operators.

Using the 'floodfill' didn't turn out too well. The line's anti-aliasing caused weird artifacts. (See previous post.) Instead of a square plus two lines, I switched to four triangles.Zooming in looks nicer.

Ha!  Fun.











#!/bin/sh

# draw Vess Cola puzzle bottles
# davep 10-Jul-2011

SIZE=300
HALF=$(( ${SIZE}/2 ))
FOURTH=$(( ${SIZE}/4 ))
EIGHTH=$(( ${SIZE}/8 ))
THREEFOURTH=$(( ${FOURTH}/3 ))

function north {
    if [ $1 == "bottom" ] ; then
        echo "rectangle $(($HALF-$EIGHTH)),0 $(($HALF+$EIGHTH)),$FOURTH"
    else
        echo "polygon $(($HALF-$EIGHTH)),0 $(($HALF+$EIGHTH)),0 $HALF,$FOURTH"
    fi
}

function east {
    if [ $1 == "bottom" ] ; then
        echo "rectangle $(($SIZE-$FOURTH)),$(($HALF-$EIGHTH))  $SIZE,$(($HALF+$EIGHTH))"
    else
        echo "polygon $SIZE,$(($HALF-$EIGHTH)) $SIZE,$(($HALF+$EIGHTH)) $(($SIZE-$FOURTH)),$HALF"
    fi
}

function south {
    if [ $1 == "bottom" ] ; then
        echo "rectangle $(($HALF-$EIGHTH)),$(($SIZE-$FOURTH)) $(($HALF+$EIGHTH)),$SIZE"
    else
        echo "polygon $HALF,$(($SIZE-$FOURTH)) $(($HALF+$EIGHTH)),$SIZE $(($HALF-$EIGHTH)),$SIZE"
    fi
}

function west {
    if [ $1 == "bottom" ] ; then
        echo "rectangle 0,$(($HALF-$EIGHTH)) $FOURTH,$(($HALF+$EIGHTH))"
    else
        echo "polygon 0,$(($HALF-$EIGHTH)) $FOURTH,$HALF 0,$(($HALF+$EIGHTH))"
    fi
}

function piece {
    num=$1
    shift
    convert -size $((${SIZE}+1))x$((${SIZE}+1)) xc:gray -fill white -stroke black \
        -draw "rectangle 0,0 $SIZE,$SIZE" \
        -fill $1 -draw "polygon 0,0 $SIZE,$0 $HALF,$HALF"\
        -fill $3 -draw "polygon $SIZE,0 $SIZE,$SIZE $HALF,$HALF" \
        -fill $5 -draw "polygon 0,$SIZE $SIZE,$SIZE $HALF,$HALF"\
        -fill $7 -draw "polygon 0,0 0,$SIZE $HALF,$HALF"\
        -fill black -draw "$(north $2)"\
        -fill black -draw "$(east $4)"\
        -fill black -draw "$(south $6)"\
        -fill black -draw "$(west $8)"\
        puzzlepiece${num}.png
}

piece 0 red bottom green top blue top yellow bottom
piece 1 red bottom blue bottom green top yellow top
piece 2 red top yellow top blue bottom green bottom
piece 3 green bottom blue bottom green top yellow top
piece 4 yellow bottom red top blue top red bottom
piece 5 green top yellow top red bottom blue bottom
piece 6 red top yellow top green bottom blue bottom
piece 7 blue top green bottom yellow bottom red top
piece 8 red top blue bottom yellow bottom green top

Sunday, July 10, 2011

Vess Cola, Part 4.

I started writing a post on how I was arrive at the 4**9 calculation. I wanted a way to illustrate the 4x pattern of the puzzle pieces.

My original drawings were done with OmniGraffle. OmniGraffle is an amazing piece of software. Its ease-of-use gives me something to strive for in my own software.

However, OmniGraffle is quite good at diagramming, drawing squares, shapes, connecting them, etc, but I wanted a nicely colored red/green/yellow/blue square. Flood fill isn't something in OmniGraffle's design.

So how to draw a puzzle piece? I don't want to use my Tkinter GUI app yet (saving that for a future post) but I needed a way to get a simple puzzle piece into a post. And taking a screen shot felt like cheating. Too easy. Yes, I like to find the hard way to do things. I learn a lot that way.

Several years ago, I wrote a little Python+ImageMagick script to do the familiar "Forbidden!" red crossed-out circle. ImageMagick has some very nice drawing primitives. (I'll post the cross.py at some point. I'd like to stay remotely close to the Vess Cola topic for a while.)

First shot was something like:

% convert -size 100x100 xc:gray -fill white -stroke black -draw "rectangle 10,10 90,90" -draw "line 10,10 90,90" -draw "line 90,10 10,90" puzzlepiece.png

Well. It's square with a cross through it. Missing color. Needs some fill. Needs some flood fill. Easy with a paint program. But what's the fun in that? ImageMagick can do flood fill.


% convert -size 100x100 xc:gray -fill white -stroke black \
    -draw "rectangle 10,10 90,90" \
    -draw "line 10,10 90,90" \
    -draw "line 90,10 10,90" \
    -fill blue -draw 'color 20,50 floodfill' \
    -fill red -draw 'color 50,20 floodfill' \
    -fill green -draw 'color 75,50 floodfill' \
    -fill yellow -draw 'color 50,75 floodfill' puzzlepiece2.png




Flood fill is a bit tricky. There can be strange effects around the edges. I'm getting weird white borders. The diagonals are because of anti-aliasing (ImageMagick's web page warns about this). Not sure what's causing the white along the red, green, and blue triangles. Not the yellow, though.

Zooming in on the image via an ImageMagick scaling operation:

% convert -resize 500% -filter Point puzzlepiece2.png p3.png

I'm using the 'point' filter to avoid interpolating the result. I didn't want a soft scaled up image. I wanted to see pixel-by-pixel.

(I could have scaled up with OSX's Preview which does a good job of not interpolating pixels. I would have had to take a dreaded screen shot. Nothing against screen shots but I'm having fun looking for a good way to create these images programatically.)

My Tkinter app I used filled triangles. I wonder how that would look in ImageMagick? Could I do it even as an SVG to avoid the jaggies?

Friday, July 8, 2011

Vess Cola, Part 3.

Graphs can be made with pointers or an array configuration.

Speed is of the essence in this application. Why? The sheer number of permutations.

Flexibility isn't important. I don't need the full range of capabilities of a directed graph. Each vertex needs a right and a down edge.

But how many permutations are there? Curious.

There are nine items that can be arranged.

Permutations http://en.wikipedia.org/wiki/Permutation. 9! == 362880

But each card has four positions. Crud. (How did I calculate this again?)  4**9 comes to mind.

Wednesday, July 6, 2011

Vess Cola, Part 2.

The problem is how to represent the 3x3 grid of Vess Cola cards in such a way that I can compare the color and bottle top/bottom. Red Top must match Red Bottom, and so forth.

At first glance, a good data structure to represent the cards' grid would be a directed graph.


A directed graph would be simpler than an undirected graph. The compare only needs to work one way.


For example, Card 0 only has to compare itself to Cards 1 and 3. The relationship is symmetrical. If Card 0 matches Card 1 then certainly Card 1 matches Card 0. With a directed graph, I can connect the cards together to quickly traverse the pattern.

So how to represent in Python?

There are numerous graph libraries. There are numerous graph libraries in Python.

Tuesday, July 5, 2011

Vess Cola, Part 1.

When I was growing up, I had a wonderful window+grandmotherly next door neighbor--Gerri (Jeri? Geraldine?) LeVeaux. She was the kind of next door neighbor who always had cookies for us kids and paid us $1 to mow her tiny lawn even though she was perfectly able to mow it herself.

Some time, when I was a kid, she gave me a puzzle she said belonged to her son. I became the proud caretaker of the Vess Puzzle.


Hmmm... I need to scan the back of the pieces, too.

I've long since lost the envelope the puzzle came in. But the puzzle is simple enough. Match up the same color top and bottom of the Vess Cola bottles.

During college, I'd get the puzzle out of my desk, solve it to avoid doing homework.

A little while ago, I started talking with my father-in-law about puzzles. I hadn't played with Vess for quite a while so I brought out the puzzle. And couldn't solve it.

Being a programmer nerd, I thought Vess would be a fun programming problem. So I set about solving the puzzle with a Python script.

Installing 32-bit Support to 64-bit Ubuntu

If you are running Ubuntu 64-bit, install the ia32-libs

# sudo apt-get install ia32-libs

(Or use Synaptic.)

References:
http://ubuntuforums.org/showthread.php?t=720732
http://www.debian-administration.org/articles/534