AtCoder Beginner Contest 259 - B - Counterclockwise Rotation - 题解

贺海
2023-12-01

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 200200200 points

Problem Statement

In an xyxyxy-coordinate plane whose xxx-axis is oriented to the right and whose yyy-axis is oriented upwards, rotate a point (a,b)(a, b)(a,b) around the origin ddd degrees counterclockwise and find the new coordinates of the point.

Constraints

  • −1000≤a,b≤1000-1000 \leq a,b \leq 10001000a,b1000
  • 1≤d≤3601 \leq d \leq 3601d360
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

aaa bbb ddd

Output

Let the new coordinates of the point be (a′,b′)(a', b')(a,b). Print a′a'a and b′b'b 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 10−610^{-6}106.


Sample Input 1

2 2 180

Sample Output 1

-2 -2

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


Sample Input 2

5 0 120

Sample Output 2

-2.49999999999999911182 4.33012701892219364908

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


Sample Input 3

0 0 11

Sample Output 3

0.00000000000000000000 0.00000000000000000000

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


Sample Input 4

15 5 360

Sample Output 4

15.00000000000000177636 4.99999999999999555911

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


Sample Input 5

-505 191 278

Sample Output 5

118.85878514480690171240 526.66743699786547949770

题目大意

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

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

解题思路

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

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


AC代码

赛时:较为复杂

后面会有简化及技巧

#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)

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

double l = hypot(a, b);

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

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 alpha = atan2(b, a);

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

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

最终代码

#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

 类似资料: