B - Counterclockwise Rotation (atcoder.jp)
Time Limit: 2 sec / Memory Limit: 1024 MB
Problem Statement
In an xy-coordinate plane whose xx-axis is oriented to the right and whose y-axis is oriented upwards, rotate a point (a, b) around the origin d degrees counterclockwise and find the new coordinates of the point.
Constraints
- −1000≤a,b≤1000
- 1≤d≤360
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
a b d
Output
Let the new coordinates of the point be (a′,b′). Print a'and 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^{-6}
Sample Input 1 Copy
Copy
2 2 180
Sample Output 1 Copy
Copy
-2 -2
When (2,2) is rotated around the origin 180 degrees counterclockwise, it becomes the symmetric point of (2,2) with respect to the origin, which is (-2, -2)
Sample Input 2 Copy
Copy
5 0 120
Sample Output 2 Copy
Copy
-2.49999999999999911182 4.33012701892219364908
Sample Input 3 Copy
Copy
0 0 11
Sample Output 3 Copy
Copy
0.00000000000000000000 0.00000000000000000000
Sample Input 4 Copy
Copy
15 5 360
Sample Output 4 Copy
Copy
15.00000000000000177636 4.99999999999999555911
Sample Input 5 Copy
Copy
-505 191 278
Sample Output 5 Copy
Copy
118.85878514480690171240 526.66743699786547949770
题意:给一个坐标(x,y)和一个角度d,求(x,y)以原点(0,0)为中心逆时针旋转d°后的坐标。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main() {
double x,y,d;
cin>>x>>y>>d;
const double pi=acos(-1.0);
double len=sqrt(x*x+y*y);
//double len = hypot(x,y);
double ang=atan2(y,x);
ang+=d/180.0*acos(-1.0);
double xx=len*cos(ang);
double yy=len*sin(ang);
printf("%10lf %10lf",xx,yy);
return 0;
}
1:acos 是cos的反函数,cos(π)= -1 ,所以acos(-1)= π;
2:hypot函数头文件math.h或cmath
hypot(a,b)的返回值为double类型,相当于sqrt(a*a+b*b)
3:C 语言里 double atan2(double y,double x) 返回的是原点至点(x,y)的方位角,即与 x 轴的夹 角。返回值的单位为弧度,取值范围为(-π, π]。结果为正表示从 X 轴逆时针旋转的角度,结 果为负表示从 X 轴顺时针旋转的角度。若要用度表示反正切值,请将结果再乘以 180/π。另 外要注意的是,函数atan2(y,x)中参数的顺序是倒置的,atan2(y,x)计算的值相当于点(x,y)的角 度值。
4:题目中给我们的d是度数(角度制),我们要把它转换成π(弧度制)才能带入三角函数计算。
d/180*π转换成有多少π
5:最后知道旋转后点到原点的距离和与x轴的角度,就可以利用三角函数sin,cos求得旋转后点的坐标x和y。