Fast Inverse Square Root


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). Continue reading

Calculating angle and direction between two vectors



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|}

dot_product_01However, 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. Continue reading

Checking divisibility of a number


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.

Continue reading