Thursday, December 09, 2004
 
Had a kinda weird moment there.
Have you ever been caught by an idea, something that you just had to think through, no matter how unimportant it seems?

This morning, I had a problem with a very simple set of functions. All they had to was convert a pixel coordinate to a tile location. See, in this graph:



You're given a pixel location, and you want to find out what tile that pixel coincides with. There are easy brute force ways, but of course, programmers want the math ways. And besides, the math isn't that tough. The basic formula is:

TileX = (PixelX / TileWidth) + (PixelY / TileHeight)
TileY = (PixelY / TileHeight) - (PixelX / TileWidth)

But for some reason, in my squirrelly little head, I thought that I could come up with something more optimal. I decided I would take the X pixel component, find out what row that coincided to, then subtract that height from the Y pixel component, and get the distance in TileHeights from that, then just add this new distance to both the X and Y components to find my new location!

Simple, huh?

Actually, it was a really stupid way to make something easy into something really complex. But I just couldn't get it out of my head. I mean, what if, after reducing this complex setup, it actually was more optimal?

So I sat down and built the formulas, reduced them as much as I could, coerced them and massaged them, until I finally came up with this:

TileX = (PixelX / TileWidth) + (PixelY / TileHeight)
TileY = (PixelY / TileHeight) - (PixelX / TileWidth)

Great. It took me three hours to build a mathematical formula that had been around for, oh, I don't know, about a thousand years.

And people tell me I'm smart.

So then, resolved and frustrated, telling myself that Edison found a thousand ways to not make a lightbulb before he found out how to make one (maybe I just found another way not to derive a 2D transformation?), I decided to take a short walk outside.

The leaves had fallen fairly recently (remember, in Texas we get our winters late, and light), so I decided to rake the leaves. At night.

Well, it was physical exertion, and that's what my tired, stupid mind wanted right then. So I raked leaves in the dark. I was guided only by a flickering, tired porchlight.

I managed to rake the whole front yard, and as I gathered all the leaves into one big pile, I noticed that I was subconsciously fretting with the pile of leaves. I didn't mean to do it, but I formed the leaves into a pile about six feet long, three feet wide, and two feet tall. It was weird, I felt kinda like Richard Dreyfuss with a plate of mashed potatoes. And when I was done, I looked at what I'd made.

It was a grave. I don't know why, but I'd managed to make a pile of leaves look just like a freshly dug grave. Suddenly, I had images of Freddy, Jason, or Evil Eddie jumping out of the leaves and grabbing me.

I think I'm going to go lie down for a while.


Powered by Blogger
Visitors since October 7th, 2004

Creative Commons License
This work is licensed under a Creative Commons License.