The algorithm is based on the "Vector rotation by transformation matrix", and this solution is based on a solution from Jens Geisler. The formula for clockwise rotation is:
![Vector rotation](
More information about the vector rotation can be found on Wikipedia.