AtCoder Beginner Contest 259 - B - Counterclockwise Rotation

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 200200 points

Problem Statement

In an xyxy-coordinate plane whose xx-axis is oriented to the right and whose yy-axis is oriented upwards, rotate a point (a,b)(a, b) around the origin dd degrees counterclockwise and find the new coordinates of the point.

Constraints

  • 1000a,b1000-1000 \leq a,b \leq 1000
  • 1d3601 \leq d \leq 360
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

aa bb dd

Output

Let the new coordinates of the point be (a,b)(a', b'). Print aa' and bb' in this order, with a space in between.
Your output will be considered correct when, for each value printed, the absolute or relative error from the answer is at most 10610^{-6}.


Sample Input 1

1
2 2 180

Sample Output 1

1
-2 -2

When (2,2)(2, 2) is rotated around the origin 180180 degrees counterclockwise, it becomes the symmetric point of (2,2)(2, 2) with respect to the origin, which is (2,2)(-2, -2).


Sample Input 2

1
5 0 120

Sample Output 2

1
-2.49999999999999911182 4.33012701892219364908

When (5,0)(5, 0) is rotated around the origin 120120 degrees counterclockwise, it becomes (52,532)(-\frac {5}{2} , \frac {5\sqrt{3}}{2}).
This sample output does not precisely match these values, but the errors are small enough to be considered correct.


Sample Input 3

1
0 0 11

Sample Output 3

1
0.00000000000000000000 0.00000000000000000000

Since (a,b)(a, b) is the origin (the center of rotation), a rotation does not change its coordinates.


Sample Input 4

1
15 5 360

Sample Output 4

1
15.00000000000000177636 4.99999999999999555911

A 360360-degree rotation does not change the coordinates of a point.


Sample Input 5

1
-505 191 278

Sample Output 5

1
118.85878514480690171240 526.66743699786547949770

题目大意

$x$轴正向朝右,$y$轴正向朝上的二维坐标平面上有一点的坐标为$(a, b)$

求 将这个点绕坐标原点逆时针旋转$d$°后 的坐标

解题思路

C++内置了$\sin、\cos、\arctan$等函数,但这些函数都是以弧度制为基础的。

先计将直角坐标转换为极坐标,然后把角度加上$d$,再转换为直角坐标即可。


AC代码

赛时:较为复杂

后面会有简化及技巧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;

const double PI = acos(-1);

int main() {
double a, b;
cin >> a >> b;
double l = sqrt(a * a + b * b);
if (l < 1e-8) {
puts("0 0");
return 0;
}
double alpha;
if (a == 0) {
if (b > 0)
alpha = PI / 2;
else
alpha = PI / 2 * 3;
}
alpha = atan(b / a);
if (a < 0)
alpha += PI;
double d;
cin >> d;
double belta = alpha + d / 180 * PI;
belta += 4 * PI;
while (belta > 2 * PI)
belta -= 2 * PI;
double x = l * cos(belta);
double y = l * sin(belta);
printf("%.9lf %.9lf\n", x, y);
return 0;
}

简化及技巧

用 hypot(a, b) 代替 sqrt(a * a + b * b)

1
double l = sqrt(a * a + b * b);

👇

1
double l = hypot(a, b);

直接用atan2求出四个象限下的角度

1
2
3
4
5
6
7
8
9
10
double alpha;
if (a == 0) {
if (b > 0)
alpha = PI / 2;
else
alpha = PI / 2 * 3;
}
alpha = atan(b / a);
if (a < 0)
alpha += PI;

👇

1
double alpha = atan2(b, a);

取消掉映射到[0, 2π)

1
2
3
belta += 4 * PI;
while (belta > 2 * PI)
belta -= 2 * PI;

👇

1

最终代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;

const double PI = acos(-1);

int main() {
double a, b;
cin >> a >> b;
double l = hypot(a, b); // 代替sqrt(a * a + b * b);
double alpha = atan2(b, a); // 非常方便地求角
double d;
cin >> d;
double belta = alpha + d / 180 * PI;
double x = l * cos(belta);
double y = l * sin(belta);
printf("%.9lf %.9lf\n", x, y); // 也可以:cout<<fixed<<setprecision(9)<<x<<' '<<y<<endl;
return 0;
}

同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125699603


AtCoder Beginner Contest 259 - B - Counterclockwise Rotation
https://blog.letmefly.xyz/2022/07/09/AtCoder Beginner Contest 259 - B - Counterclockwise Rotation/
作者
Tisfy
发布于
2022年7月9日
许可协议