Kazun の競プロ記録

競技プログラミングに関する様々な話題を執筆します.

AtCoder Beginner Contest 259 B問題 Counterclockwise Rotation

問題

atcoder.jp

提出解答

atcoder.jp

問題の概要

右手系の座標平面上の点  (a,b) を原点中心に  d 度反時計回りに回転させた後の点の座標を求めよ.

制約

  •  -1000 \leq a,b \leq 1000
  •  1 \leq d \leq 360

解法

 P=(a,b)極座標に変換する. つまり,  (a,b) から次を満たす非負実数  r \geq 0 と実数  \theta \in \mathbb{R} を求める.

  • 原点  O と点  P との距離が  r であり,  x 軸の正の部分から線分  OP まで反時計回りに測った角度が  \theta である.

実は,  a=r \cos \theta, b=r \sin \theta という関係式があり, この関係式から,  r, \theta をそれぞれ次のようにして求められる *1.

 r=\sqrt{r^2}=\sqrt{r^2 (\cos^2 \theta+\sin^2 \theta)}=\sqrt{(r \cos \theta)^2+(r \sin \theta)^2}=\sqrt{x^2+y^2}

 a \neq 0 のとき,  \frac{b}{a}=\frac{r \sin \theta}{r \cos \theta}=\tan \theta なので,

 \theta=\begin{cases} 0 & (a=0, b \geq 0) \\ \pi & (a=0, b \lt 0) \\ \operatorname{Tan}^{-1} \frac{b}{a} & (a \neq 0) \end{cases}

この  r, \theta を利用すると, 求めるべきは  P を原点中心に  d \times \dfrac{\pi}{180} だけ反時計回りに回転させた点なので, 原点との距離が  r で, 偏角 \left(\theta+ d \times \dfrac{\pi}{180} \right) の点なので

 \left(r \cos \left(\theta+ d \times \dfrac{\pi}{180} \right), r \sin \left(\theta+ d \times \dfrac{\pi}{180} \right) \right)

である.

*1:原点の偏角は人や場合によることが多い.