Chris Nauroth
Welcome About Me Diary Software Downloads About the Site
Introduction
www.cnauroth.net
WebSlinger
Mancala
Base64Converter
LogAnalyzer
Permutations

WebSlinger

Download the source code.

For a while, one of the teams at Amazon.com was running a monthly programming contest for the Amazon software development community. One of my favorite challenges was to write a program to draw a spider web. I decided to implement a Windows Forms application in C# with user interface elements that would let me easily change the parameters of the web rendering algorithm.

Before discussing the implementation, let's define some terminology:

User Interface

User input is easily implemented with standard Windows Forms controls, but display of the web rendering required something new. I needed a control that would render a dynamic image based on the state of a Web object. I decided to define an IDrawable interface, representing any object capable of rendering a visual representation of itself. The Web object implements IDrawable, defining the Draw method with a web-drawing algorithm. I then chose to subclass Panel, overriding the OnPaint method to draw an encapsulated IDrawable object.

Web-Drawing Algorithm

The first step of the algorithm is to draw all of the anchors. Remember, the anchors are a collection of lines starting from the origin. If you imagine a circle bounding the web, then the length of the anchors would be the radius of that bounding circle.

The next step is to draw the rings. Each ring is a series of lines connecting an anchor to the next anchor at the same distance from the origin. Again, it's helpful to consider a bounding circle to simplify visualization of the algorithm.

I wanted my user interface to have the capability to pan left, right, up, and down to view different portions of the web. To implement that requirement, I used a matrix translation operation. A Matrix object is encapsulated within the DrawablePanel object. Before the DrawablePanel begins rendering, it applies its Matrix transformation to the Graphics object.

When I wrote WebSlinger, I didn't have any version of Visual Studio running on my computer. I downloaded the free .NET command-line compilers, coded the application in Notepad and wrote a makefile to run through nmake for my builds. A good IDE is a blessing, but I guess it's not a necessity.