我發現this excellent question and answer與x
/y
(加上center x/y
和degrees/radians
)開始,並計算旋轉到x'
/y'
。這個計算完美,但我想運行在相反的方向;從x'
/y'
和degrees/radians
開始,我想計算原始的x
/y
和center x/y
。點上的(未)旋轉矩形
(x', y') = new position
(xc, yc) = center point things rotate around
(x, y) = initial point
theta = counterclockwise rotation in radians (radians = degrees * Pi/180)
dx = x - xc
dy = y - yc
x' = xc + dx cos(theta) - dy sin(theta)
y' = yc + dx sin(theta) + dy cos(theta)
或者,在JavaScript/jQuery的:
XYRotatesTo = function($element, iDegrees, iX, iY, iCenterXPercent, iCenterYPercent) {
var oPos = $element.position(),
iCenterX = ($element.outerWidth() * iCenterXPercent/100),
iCenterY = ($element.outerHeight() * iCenterYPercent/100),
iRadians = (iDegrees * Math.PI/180),
iDX = (oPos.left - iCenterX),
iDY = (oPos.top - iCenterY)
;
return {
x: iCenterX + (iDX * Math.cos(iRadians)) - (iDY * Math.sin(iRadians)),
y: iCenterY + (iDX * Math.sin(iRadians)) + (iDY * Math.cos(iRadians))
};
};
上面的數學/代碼解決了在圖A的情況;它基於x
/y
(紅色圓圈),center x/y
(藍色星)和degrees/radians
的已知值計算目的地x'
/y'
(綠色圓圈)的位置。
但我需要數學/代碼來解決圖B;在這裏我不僅可以找到從起始x
/y
的已知值目的地x
/y
(綠圈),而且目的地center x/y
(綠星)(灰色圓圈,雖然可能不需要),目的地x'
/y'
(紅圈)和degrees/radians
。
上面的代碼將通過iDegrees * -1
爲目標x
/y
(綠圈)解決(感謝@andrew這已經被他刪除庫克的回答),但爲了做到這一點,我需要喂到它的目的地center x/y
(綠星)的位置,這是我目前缺少的計算,你可以在圖C見,下面:
那麼......是嗎?找到座標?
/?
(綠色星號)n
,A
(角度)和x'
/y'
(紅色圓圈)?