题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3365
思路: 以A[0]为原点,构造向量A[i]-A[0]。先旋转(注意旋转方向),再伸缩,最后平移至终点。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debu
using namespace std;
const double eps=1e-7;
const int maxn=1e4+50;
const double pi=acos(-1.0);
struct Vector
{
double x,y;
Vector(double x=0,double y=0):x(x),y(y) {}
};
int n;
Vector A[maxn],B[5];
Vector operator + (const Vector &A,const Vector &B)
{
return Vector(A.x+B.x,A.y+B.y);
}
Vector operator - (const Vector &A,const Vector &B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator * (const Vector &A,const double &p)
{
return Vector(A.x*p,A.y*p);
}
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
double Dot(const Vector &A,const Vector &B)
{
return A.x*B.x+A.y*B.y;
}
double Length(const Vector &A)
{
return sqrt(Dot(A,A));
}
double Cross(const Vector &A,const Vector &B)
{
return A.x*B.y-A.y*B.x;
}
Vector Rotate(const Vector &A,double rad)
{
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
int main()
{
#ifdef debug
freopen("in.in","r",stdin);
#endif // debug
int t,cas=0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%lf%lf",&A[i].x,&A[i].y);
for(int i=0; i<2; i++)
scanf("%lf%lf",&B[i].x,&B[i].y);
Vector tmp1,tmp2;
tmp1=A[1]-A[0];
tmp2=B[1]-B[0];
double len=Length(tmp2)/Length(tmp1);
double rad=acos(Dot(tmp1,tmp2)/(Length(tmp1)*Length(tmp2)));
//cout<<rad<<endl;
if(Cross(tmp1,tmp2)<eps) rad=2.0*pi-rad;
//cout<<"rad "<<rad<<endl;
for(int i=2; i<n; i++)
{
A[i]=A[i]-A[0];
//cout<<"flag "<<A[i].x<<" "<<A[i].y<<endl;
A[i]=Rotate(A[i],rad);
//cout<<A[i].x<<" "<<A[i].y<<endl;
A[i]=A[i]*len;
A[i]=A[i]+B[0];
}
printf("Case %d:\n",++cas);
printf("%.2f %.2f\n",B[0].x,B[0].y);
printf("%.2f %.2f\n",B[1].x,B[1].y);
for(int i=2; i<n; i++)
printf("%.2f %.2f\n",A[i].x,A[i].y);
}
return 0;
}