Yesterday I was flirting with the concept of Key Distance. It wasn’t supposed to be complicated, a just for fun activity. It’s actually very interesting. You’ll never guess what keys are the farthest apart.
The distance that separated these two keys is 12.4092. The two keys are Z and + or =. What’s thar or about? Look at the = key. It’s also the + key. Thus, they are the same distance from Z. What about Z and z? They aren’t the same distance actually. z and = or + were in second place for 12.36931689769.
I was chatting with the people in ##php on freenode.net this afternoon, checking to see if anyone had already made this type of thing. Nobody had nor did they want to. Internationalization stopped them cold. Using anything outside of ASCII would over-complicate things. Anyway, I left with the suggestion to take the deltas and get a distance. Knowing what little algebra I know, I actually did it!
I hate posting code in WordPress for any reason; let’s just go through the steps to figure it out.
- Make the keyboard into a multi-dimensional array. My structure was shifted and non-shifted. (I called these z.)
- Make each row of the keyboard an array inside of the shifted and non-shifted parent array. (I called these y.)
- Rather than typing out each key for each letter, I used the php array_search method instead. This allowed me to get the actual character. (And thus the last value, which I call x.)
- WIth these values, I create a new array with them stored in it.
- 1 through 4 over again. Two arrays are present now, Key1 and Key2.
- With the new arrays containing x, y and z, I used my favorite formula. The distance formula! (There’s a side story here. I asked my math teacher in 9th grade how to determine distance in a 3D plane and it was all very confusing. Silly of me to ask. Angles.)
- Using distance formula, sqrt( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 ), I was returned a number that was the distance between the two keys. And amazingly, it’s generally right!
The decimal parts, although minuscule, were there and important. They often appeared when a shifted key was used. Often, they were small amounts like .04 or so.