B. Rotate
[Problem Description]
Noting is more interesting than rotation!
Your little sister likes to rotate things. To put it easier to analyze, your sister makes n rotations. In the
i-th time, she makes everything in the plane rotate counter-clockwisely around a point ai by a radian
of pi.
Now she promises that the total effect of her rotations is a single rotation around a point A by radian
P (this means the sum of pi is not a multiplier of 2π).
Of course, you should be able to figure out what is A and P :).
[Input Format]
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains an integer n denoting the number of the rotations. Then n
lines follows, each containing 3 real numbers x, y and p, which means rotating around point (x, y)
counter-clockwisely by a radian of p.
We promise that the sum of all p's is differed at least 0.1 from the nearest multiplier of 2π.
T<=100. 1<=n<=10. 0<=x, y<=100. 0<=p<=2π.
[Output Format]
For each test case, print 3 real numbers x, y, p, indicating that the overall rotation is around (x, y)
counter-clockwisely by a radian of p. Note that you should print p where 0<=p<2π.
Your answer will be considered correct if and only if for x, y and p, the absolute error is no larger
than 1e-5.
[Sample Input]
1
3
0 0 1
1 1 1
2 2 1
[Sample Output]
1.8088715944 0.1911284056 3.0000000000
样例中的三个矩阵相乘,重点学习了矩阵的乘法
把思路理清来写程序
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
#define pi (acos(-1.0))
void matx (double b[3][3], double a[3][3], double c[3][3])
{
for(int i=0; i<3; ++i){
for(int j=0; j<3; ++j)
{
double sum = 0;
for(int k=0; k<3; ++k)
sum += a[i][k] * b[k][j];
c[i][j] = sum;
}
}
}
int creat(double a[3][3],double x,double y,double th)
{
a[0][0]=cos(th);
a[0][1]=-sin(th);
a[0][2]=x-x*cos(th)+y*sin(th);
a[1][0]=sin(th);
a[1][1]=cos(th);
a[1][2]=y-x*sin(th)-y*cos(th);
a[2][0]=0;
a[2][1]=0;
a[2][2]=1;
return 0;
}
int main()
{
int t,i,n,j;
double poi[13][2],jd[13],a[3][3],b[3][3],c[3][3];
cin>>t;
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%lf%lf%lf",&poi[i][0],&poi[i][1],&jd[i]);
}
creat(a,poi[1][0],poi[1][1],jd[1]);
for(i=2;i<=n;i++)
{
creat(b,poi[i][0],poi[i][1],jd[i]);
matx(a,b,c);
for(int i1=0;i1<3;i1++)
for(int j1=0;j1<3;j1++)
a[i1][j1]=c[i1][j1];
}
double x0,y0,jd0;
jd0=acos(a[0][0]);
if(a[1][0]<0) jd0=2*pi-jd0;
/*for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}*/
x0=(a[0][2]*(1-a[0][0])-a[1][0]*a[1][2])/((1-a[0][0])*(1-a[0][0])+a[1][0]*a[1][0]);
y0=(a[1][2]*(1-a[0][0])-a[0][1]*a[0][2])/((1-a[0][0])*(1-a[0][0])+a[1][0]*a[1][0]);
printf("%.10lf %.10lf %.10lf\n",x0,y0,jd0);
}
return 0;
}