Skip to main content

The Trouble with Tribble Visitors

So, I'm now partly sold on the power of a Visitor approach to rendering. Consider this snippet:

if (clicked) diagram.accept(new DropShadowVisitor(g, 5, -5);
else         diagram.accept(new DrawVisitor(g));
What this is doing is drawing a diagram normally unless the mouse is clicked, when it draws with a drop shadow. I see that the beauty of this is the ability to manipulate functionality as a block (just as in languages where you can pass around functions...).

However, I should point out that the approach has its tricky rapids as well as such smooth sailing. The image below is a spot-the-difference (click for bigger):


On the left is the version drawn by a naive first try at the drop visitor. Its methods look like this, the visitText(Text text) method:

    g.setColor(Color.LIGHT_GRAY);
g.drawString(text.text, text.x, text.y);
g.setColor(Color.BLACK);
g.drawString(text.text, text.x + dx, text.y + dy);
The problem with this code is subtle - the elements are visited once each, and the shadow is rendered at the same time as the element. The change that had to be made was to have a boolean "drawingShadow" and to visit the elements twice
        this.drawingShadow = true;
for (DiagramElement element : diagram.children) { element.accept(this); }
this.drawingShadow = false;
for (DiagramElement element : diagram.children) { element.accept(this); }
The shadow has to be drawn first - BUT first for the whole diagram, not just first for each element. So the new text method is :

if (this.drawingShadow) {
g.setColor(Color.LIGHT_GRAY);
g.drawString(text.text, text.x, text.y);
} else {
g.setColor(Color.BLACK);
g.drawString(text.text, text.x + dx, text.y + dy);
}

Comments

Popular posts from this blog

How many isomers of C4H11N are there?

One of the most popular queries that lands people at this blog is about the isomers of C4H11N - which I suspect may be some kind of organic chemistry question on student homework. In any case, this post will describe how to find all members of a small space like this by hand rather than using software.

Firstly, lets connect all the hydrogens to the heavy atoms (C and N, in this case). For example:


Now eleven hydrogens can be distributed among these five heavy atoms in various ways. In fact this is the problem of partitioning a number into a list of other numbers which I've talked about before. These partitions and (possible) fragment lists are shown here:


One thing to notice is that all partitions have to have 5 parts - even if one of those parts is 0. That's not strictly a partition anymore, but never mind. The other important point is that some of the partitions lead to multiple fragment lists - [3, 3, 2, 2, 1] could have a CH+NH2 or an NH+CH2.

The final step is to connect u…

Havel-Hakimi Algorithm for Generating Graphs from Degree Sequences

A degree sequence is an ordered list of degrees for the vertices of a graph. For example, here are some graphs and their degree sequences:



Clearly, each graph has only one degree sequence, but the reverse is not true - one degree sequence can correspond to many graphs. Finally, an ordered sequence of numbers (d1 >= d2 >= ... >= dn > 0) may not be the degree sequence of a graph - in other words, it is not graphical.

The Havel-Hakimi (HH) theorem gives us a way to test a degree sequence to see if it is graphical or not. As a side-effect, a graph is produced that realises the sequence. Note that it only produces one graph, not all of them. It proceeds by attaching the first vertex of highest degree to the next set of high-degree vertices. If there are none left to attach to, it has either used up all the sequence to produce a graph, or the sequence was not graphical.



The image above shows the HH algorithm at work on the sequence [3, 3, 2, 2, 1, 1]. Unfortunately, this produce…

Generating Trees

Tree generation is a well known (and solved!) problem in computer science. On the other hand, it's pretty important for various problems - in my case, making tree-like fusanes. I'll describe here the slightly tortuous route I took to make trees.

Firstly, there is a famous theorem due to Cayley that the number of (labelled) trees on n vertices is nn - 2 which can be proved by using Prüfer sequences. That's all very well, you might well say - but what does all this mean?

Well, it's not all that important, since there is a fundamental problem with this approach : the difference between a labelled tree and an unlabelled tree. There are many more labeled trees than unlabeled :


There is only one unlabeled tree on 3 vertices, but 3 labeled ones
this is easy to check using the two OEIS sequences for this : A000272 (labeled) and A000055 (unlabeled). For n ranging from 3 to 8 we have [3, 16, 125, 1296, 16807, 262144] labeled trees and [1, 2, 3, 6, 11, 23] unlabeled ones. Only 23 …