I recently wrote a small patch for gvim to display text shadows for all text. The idea was there are lots of interesting dark background color themes that suffer from decreased color contrast by not being on a black background, so adding a text shadow would make it look better. I grabbed a very naive and fast "color darkening" algorithm from a StackOverflow response:

col = (col & 0xfefefe) >> 1;

This is incredibly fast, and works by snipping off the most significant bit of each color component. However, this means that it will unevenly darken each component; #FF0202 becomes #7F0101, for instance. I took the tip from another comment and write an updated version of my patch that switches the colors pace from RGB to HSV, then lowered the value and converted back.

This made me wonder: what other types of problems do you commonly encounter where changing the actual format of the data can make the algorithm not only simpler but more correct? From the CS angle, I'm thinking of heap sort, which is very fast but embeds all of the complexity of the algorithm onto the heap itself; the actual sort is just throwing items into the heap and then pulling them out. In astrophysics, there are problems which are much more convenient to solve with polar coordinates, or where using certain units can buy you lots of 1 denominators and simplify calculations.

Are there more examples of this? Is this the true value of experience?

Sep 12 2011