This is a C++ program that rotates a point initially located at origin by a given angle A (degree) counterclockwise and L translate its x-coordinate (sum L to it).
Here is my program:
Firstly it reads the number of test cases. Each test case has 2 real numbers and 1 integer that represents a rotation, translation and how many times the operation of rotate and translate must be applied.
#include <cstdio>
#include <utility>
#include <cmath>
#define gc getchar_unlocked()
inline double getDouble() {
double v;
scanf("%lf",&v);
return v;
}
#define CONST 0.01745329251994329576922 //3.14159265358979323846/180 = 0.01745329251994329576922
std::pair<double, double> rotate(std::pair<double, double> p, double a){
double rad = a*CONST;
return std::make_pair(p.first*std::cos(rad) - p.second*std::sin(rad), p.first*std::sin(rad) + p.second*std::cos(rad));
}
int main(void){
int t = (int) getDouble();
for (int i = 0; i < t; ++i) {
double a = getDouble(),l = getDouble();
int v = (int) getDouble();
std::pair<double, double> p(0,0);
while (v--) {
p = rotate(p, a);
p.first += l;
}
//for cases like 30 1.5 121... the printf just put -0.00
if(std::abs(p.first) < 0.005)
p.first = 0;
if(std::abs(p.second) < 0.005)
p.second = 0;
printf("%.2lf %.2lf\n",p.first,p.second);
}
return 0;
}
Sample input:
4
90 10 1
90 10 2
90 10 3
30 1.5 1000000000
Sample output:
10.00 0.00
10.00 10.00
0.00 10.00
3.55 3.55
How to make it faster ?