Tuesday, July 14, 2009

Using vectors to simplify graphics translation

I've been working on a C# WPF program that deals with graphics. In this program the user is able to move graphics around by dragging. In a past lifetime I handled this sort of thing by doing arithmetic in the mouse handlers.


onmousedown:
    o set deltaX, deltaY to 0 and store location of mouse
onmousemove:
    o figure out cumulative deltaX, deltaY based on current mouse location and the stored mouse location
    o draw item, displaced by deltaX, deltaY
onmouseup:
    o update global displacement using deltaX, deltaY


Today I did it the same way, except instead of dealing with individual X and Y coordinates, I used point and vector structures. The .NET framework comes with a conversion between them, and I don't even have to cast explicitly. The result of arithmetic on points (like say mouseCurrent - mouseInitial) is a vector. Nice.

Wednesday, July 8, 2009

My alarm clock has a feature for power users

I discovered that if I press both the hour and minute buttons on my clock when setting the alarm, the alarm time resets to 12 AM. It's quite convenient since I usually set the alarm for the morning, so I only have to tap the hour button a few times to get the desired time.