See Rotation matrix If you've got more questions directly related to code, I recommend writing an SSCCE which tries to accomplish your needs and ask questions directly related to that code.