Official

B - Counterclockwise Rotation Editorial by en_translator


Abstract

This problem can be solved by performing the following operations.

  1. Find “the distance from the origin” of \((a, b)\) and “the azimuth angle of the segment connecting the origin and \((a, b)\)
  2. Find the coordinates of the point of which the azimuth angle is increased by \((a,b)\)

For convenience, we will explain 2, and then 1.

Trigonometric functions

Let \(r\) and \(\theta\) be the distance and the azimuth angle found in the first step, respectively. Let \(\theta'\) be the azimuth angle \(\theta\) increased by \(d\). Here, our purpose is to find the coordinates of the point whose distance from the origin is \(r\) and azimuth angle is \(\theta'\).
When \(r=1\), the \(x\) and \(y\) coordinates for a given angle \(\theta\) is equivalent to the definition of the trigonometric functions \(\cos \theta\) and \(\sin \theta\). In many programming languages like C++, the value can be obtained as follows:

x = cos(theta);
y = sin(theta);

When \(r\) is not necessarily \(1\), multiply the \(x\) and \(y\) coordinates by \(r\).

Inverse trigonometric functions

In the first step, you need to find the distance from the origin \(r\) and the azimuth angle \(\theta\) of a given point \((a, b)\).
\(r\) can be found as \(r=\sqrt{a^2+b^2}\).
\(\theta\) can be found with the inverse functions of the trigonometric functions. Here, simply trying to find \(\theta\) using the inverse functions of \(\sin \theta, \cos \theta, \tan \theta\) requires case divisions, which is troublesome, but many programming languages like C++ has a function atan2(y,x) that enables us to find \(\theta\) as follows:

theta = atan2(b,a);

Thus, \(r\) and \(\theta\) can be found.

Notes

  • In this problem, the angle is given in degrees, while many programming languages like C++ handles (accepted as the arguments and returned by trigonometric functions) angles in radians. Thus, you need to convert as \(d' = \frac{d\times \pi}{180}\) before processing.
  • In this problem, it may be that \((a,b)=(0,0)\), but the value \(\mathrm{atan2}(0,0)\) isn’t defined mathematically. If your programming language returns evaluates atan2(0,0) as some specific value \(\theta\), then it’s okay since we are multiplying by \(r=0\) (on which the sample code relies), but if the value is undefined or returns an error, you need to divided into cases.
  • You may need to change the number of digits to be output from default, or you may not get appropriate precision although you calculated correctly.

Sample code (C++)

#include <bits/stdc++.h>
using namespace std;

int main() {
    
	double a,b,d;
	cin>>a>>b>>d;

	double r = hypot(a,b);
	double theta = atan2(b,a);

	theta += d * acos(-1.0) / 180.0;

	double x = cos(theta) * r;
	double y = sin(theta) * r;

	cout<<fixed<<setprecision(10)<<x<<' '<<y<<endl;

	return 0;
	
}

posted:
last update: