Here's a way to rotate any point around any other point in 2D. Note that in 3D this can be used as rotation around the z axis, the z-coordinate of the received point, since it does not change. Rotation around the X axis and Y axis in 3D can also be easily implemented.
The code is in JavaScript. The numbered lines at the beginning are a set of tests for the function. They also serve as an example of use.
//A = new Array(0,0) //S = new Array(-1,0) //fi = 90 //alert("rotujBod: " + rotatePoint(A, S, fi)) function rotatePoint(A, S, fi) { /** IN points A - rotated point, S - centre, fi - angle of rotation (rad) * points in format [Ax, Ay, Az], angle fi (float) * OUT point B */ r = Math.sqrt((A[0] - S[0])*(A[0] - S[0]) + (A[1] - S[1])*(A[1] - S[1])) originOfRotation = new Array(S[0] + r, S[1]) if (A[1] < S[1]) { A2 = new Array(A[0], -1*A[1]) originalAngle = -1*sizeOfAngle(originOfRotation, S, A2) } else { originalAngle = sizeOfAngle(originOfRotation, S, A) } x = S[0] + r*Math.cos(fi + originalAngle) y = S[1] + r*Math.sin(fi + originalAngle) B = new Array(x, y) return(B) } function sizeOfAngle(A, S, B) { ux = A[0] - S[0] uy = A[1] - S[1] vx = B[0] - S[0] vy = B[1] - S[1] if((Math.sqrt(ux*ux + uy*uy)*Math.sqrt(vx*vx + vy*vy)) == 0) {return 0} return Math.acos((ux*vx + uy*vy)/(Math.sqrt(ux*ux + uy*uy)*Math.sqrt(vx*vx + vy*vy))) }
Jan Kokes Nov 07 '15 at 18:12 2015-11-07 18:12
source share