Fast Inverse Square Root

Standard

I remembered that I hit a bottle neck while doing the project for my thesis. After gathering detection windows of pedestrians, I needed to apply Mean Shift clustering in order to have final detection windows.

However, one of the line of codes that ended up as bottleneck was pow( x, -0.5 ). Googling around, I managed to find a piece of code from Quake III Arena (yeah, Quake III Arena, but the code wasn’t from Carmack though).

Calculating angle and direction between two vectors

Standard

Normally, if we were to find the angle between two vectors, say up-vector and dir-vector, what we would do is to do a dot product between them. Since

$a \cdot b = |a||b| \cos{\theta}$ and

$\cos{\theta} = \frac{a \cdot b}{|a||b|}$

However, since the calculated angle is the smallest / nearest angle between the two vectors, we can’t derive the (rotational) direction of one vector from the other. Thus in the following figure, it’s hard to figure out whether the direction vector is on the left or right of the up vector. The angle between two vectors calculated using dot product is always between 0° to 180°, thus we are not able to derive the direction from it.

Squaring 2 digits number from the top of your head

Standard

Say that you want to square the number 73 and you don’t have a calculator near you ? So what can you do ? Well, that might not always be the case, but you can use it as a party trick, or to check the results of your calculations, etc.

To be able to square 2 digits number from the top of your head, you need to be able to  do these two things:

Checking divisibility of a number

Standard

The ability to check whether a number is divisible by, say 4 or 9, seems to be useless at first.

But it could be a time saver when you’re trying to check the result of your calculation without resorting to the help of a calculator. Or it could be an amusing mental trick (or what Arthur Benjamin called Mathemagic) which you can use it for fun while drinking some beers in the pub.