B - Counterclockwise Rotation Editorial by Trineutron
座標を回転させる方法として、
- 回転行列
- 複素数
の 2 通りの方法があります。いずれの方法でも実装は同じです。
回転行列
反時計回りに \(\theta\) だけ回転させる回転行列は \(\left( \begin{matrix} \cos{\theta} & -\sin{\theta} \\ \sin{\theta} & \cos{\theta}\end{matrix}\right)\) と表せます。
\(\left( \begin{matrix} \cos{\theta} & -\sin{\theta} \\ \sin{\theta} & \cos{\theta} \end{matrix} \right) \left( \begin{matrix} a \\ b \end{matrix} \right) = \left( \begin{matrix} a \cos{\theta} - b \sin{\theta} \\ a \sin{\theta} + b \cos{\theta} \end{matrix} \right)\) より、回転後の座標は \((a \cos{\theta} - b \sin{\theta}, a \sin{\theta} + b \cos{\theta})\) となります。
複素数
\(i\) を虚数単位とします。
座標 \((a, b)\) を複素数 \(a+bi\) に変換すると、\(e^{i\theta}\) を掛ける操作は反時計回りに \(\theta\) だけ回転させることに相当します。\(e^{i\theta} = \cos{\theta} + i \sin{\theta}\) より、\((a+bi)(\cos{\theta} + i \sin{\theta}) = (a \cos{\theta} - b \sin{\theta}) + i(a \sin{\theta} + b \cos{\theta})\) から回転後の座標は \((a \cos{\theta} - b \sin{\theta}, a \sin{\theta} + b \cos{\theta})\) となります。
コード例 (Python3)
from math import sin, cos, radians
a, b, d = map(int, input().split())
d = radians(d)
print(a * cos(d) - b * sin(d), a * sin(d) + b * cos(d))
posted:
last update: