Direct way of computing clockwise angle between 2 vectors

2D case Just like the dot product is proportional to the cosine of the angle, the determinant is proprortional to its sine. So you can compute the angle like this: The orientation of this angle matches that of the coordinate system. In a left-handed coordinate system, i.e. x pointing right and y down as is … Read more

Finding square root without using sqrt function?

There is a better algorithm, which needs at most 6 iterations to converge to maximum precision for double numbers: Algorithm starts with 1 as first approximation for square root value. Then, on each step, it improves next approximation by taking average between current value y and x/y. If y = sqrt(x), it will be the same. If y > sqrt(x), then x/y < sqrt(x) by about the same … Read more

Rotating a Vector in 3D Space

If you want to rotate a vector you should construct what is known as a rotation matrix. Rotation in 2D Say you want to rotate a vector or a point by θ, then trigonometry states that the new coordinates are To demo this, let’s take the cardinal axes X and Y; when we rotate the X-axis 90° counter-clockwise, … Read more

Is there a standard sign function (signum, sgn) in C/C++?

I want a function that returns -1 for negative numbers and +1 for positive numbers. It’s easy enough to write my own, but it seems like something that ought to be in a standard library somewhere. Edit: Specifically, I was looking for a function working on floats.