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.

Sunday, August 17, 2008

Relegated Brace Style

Have your gander at this JScript.NET:


 1 import System;
 2 import Accessibility; // ??
 3 import System.Windows.Forms;
 4 import System.Drawing;
 5
 6 var title = "top secret";
 7 var textfield = new TextBox();
 8
 9 function foreach( arr, fn ) {
10     forvar k in arr )
11         fn( arr[k] );
12 }
13
14 class main extends Form
15 {
16     function main() {
17         Text = title;
18         ClientSize = new System.Drawing.Size( 500, 380 );
19
20         textfield.Dock = DockStyle.Bottom;
21         textfield.Multiline = true;
22         textfield.Height = 106;
23         this.Controls.Add( textfield );
24     }
25
26     protected override function OnPaint( e: PaintEventArgs ) {
27         foreach( wordwise( textfield.Text ), function( word ) {
28             print( word + " in bed" );
29         } );
30     }
31 }
32
33 function wordwise( str ) {
34     return str.split( /[\s\n\t]+/ );
35 }
36
37 Application.Run( new main() );


Don't worry about what it does, no one cares. Instead notice how ugly the braces make it look. The C-style open-close brace syntax, along with not being able to have arbitrary items as keys in hashes, are the two things that are unfortunate about Javascript, and are two things that its cousin Lua doesn't have. In fact let's see how this code would look like in Lua.


 1 require "System";
 2 require "Accessibility"; -- ??
 3 require "System.Windows.Forms";
 4 require "System.Drawing";
 5
 6 local title = "top secret";
 7 local textfield = TextBox.new();
 8
 9 local main = {};
10
11 main.new = function()
12     local obj = {};
13
14     obj.Text = title;
15     obj.ClientSize = System.Drawing.Size.new( 500, 380 );
16
17     textfield.Dock = DockStyle.Bottom;
18     textfield.Multiline = true;
19     textfield.Height = 106;
20     obj.Controls.Add( textfield );
21
22     obj.OnPaint = function( e )
23         for word in wordwise( textfield.Text ) do
24             print( word + " in bed" );
25         end
26     end
27
28     setmetatable( obj, { __index = Form } );
29
30     return obj;
31 end
32
33 function wordwise( str )
34     return string.gmatch( str, "[^%s]+" );
35 end
36
37 Application.Run( main.new() );


Or something like that. What matters is that there is nary a brace to uglify things. Note: Semicolons are optional in both languages. Ah, but there is a way to save Javascript and all other braced languages. I call it the Relegated Brace Style. To do it, simply put all braces, both opening and closing, far to the right of the page:


 1 import System;
 2 import Accessibility; // ??
 3 import System.Windows.Forms;
 4 import System.Drawing;
 5
 6 var title = "top secret";
 7 var textfield = new TextBox();
 8
 9 function foreach( arr, fn )                                       {
10     forvar k in arr )
11         fn( arr[k] );                                             }
12
13 class main extends Form                                           {
14     function main()                                               {
15         Text = title;
16         ClientSize = new System.Drawing.Size( 500, 380 );
17
18         textfield.Dock = DockStyle.Bottom;
19         textfield.Multiline = true;
20         textfield.Height = 106;
21         this.Controls.Add( textfield );                           }
22
23     protected override function OnPaint( e: PaintEventArgs )      {
24         foreach( wordwise( textfield.Text ), function( word )     {
25                print( word + " in bed" )                          }
26         );                                                        }}
27
28 function wordwise( str )                                          {
29        return str.split( /[\s\n\t]+/ );                           }
30
31 Application.Run( new main() );


Slick eh? It's even better because Vim has its horizontal scroll bar disabled by default, so you can readily muster enough pretension to make the braces disappear completely, or at least be dead to you.

Thursday, July 31, 2008

How to pick up heavy things

Let's start by showing how you shouldn't pick up heavy things:



I call this the "bend over" lifting method. It's very dangerous, as it can lead to this:



When I was a wee teenager, for no reason* I tried a different method of lifting:



I call this the Marquez Lift, and it is the best lifting method I've found so far (not that I've been looking.) To do it, extend one of your legs far behind you, then pick up the heavy thing. You'll notice that the heavy thing will actually feel lighter than it should, and your back won't be in the same sort of pain as the standard bend over method. The whole process will take a lot less effort overall.

And notice that, unlike the bend over method, this isn't a sexually submissive posture. Au contraire, the extended leg implies a swift kick to the would-be penetrator's crotch.

It might seem dumb at first sight, but that's probably because you don't realize how heavy your legs are. The weight of the extended leg, combined with the lever effect that it creates which counterbalances your forward weight and the weight of the heavy object, actually makes for a comfortable lifting posture.

But not only does it effectively make the object lighter, it also doesn't crack your back open in a zig-zag pattern. The reason is that the posture encourages rotation of the hips, like so:




Due to the extended leg. This means you're more in a cartwheel-like posture than in a painful bending one. Cartwheel > painful bending.

* This is a lie.