Hello and welcome to Rick Umali's Tech Talk. I'm a computer professional (IT developer) living and working in Greater Boston.
Feel free to send me e-mail (rickumali@gmail.com) or comment on any item. Thanks for visiting!
I've been noodling with a Win32 version of Tetris, the ubiquitous "shape placing" game. I'm working in C++ using the Simple Directmedia Layer for my graphics. One of the interesting things in working out the game is the "shape" data. Below is the code for one my shapes, and the methods to "rotate" and "draw" it.
For some reason, I was pretty happy with this, especially when my testing produced the correct behavior. (Note the "rotate" method just loops through the numbers 0, 1, 2, and 3, which correspond to a shape's four possible positions.)
However, the more I thought about it, the more I realized that I was simply hardcoding the shape diagrams for each rotation. Couldn't I store the start rotation once, and just derive the other rotations? As I pondered this, I realized that the answer would involve matrix algebra. I studied this in school, but that was ages ago. I'm sure more searching would yield an algorithm (and rotating in arbitrary 90 degree "increments" reduces the complexity), but my proggie is a toy, running on Win32. If my game were targeted for a handheld, I may need to worry about that extra 18 bytes that my code is using, trading that off against the CPU cycles that an algorithm would need to calculate the new shape data. So in the end, I decided to remain happy with it. For now.