hdu1033题目很长,很难懂,愣是把题目翻来覆去读了好多遍,最后才晓得,原来是个很简单的问题。
向量的旋转,当前向量的末端是完成旋转后向量的起始端。相当于把向量沿着其方向平移其模长度后再旋转。
题目的大概意思就是这个,A顺时针旋转,V逆时针旋转,每次旋转均为90°。
设有点start,end,temp;start,end分别为向量的起始和终止端点,temp为旋转过程中向量的终点。
总结会发现一个规律,在顺时针旋转时有:
temp.x = end.x+end.y-start.y;
temp.y=end.y+start.x-end.x;
逆时针时有:
temp.x = end.x+start.y-end.y;
temp.y=end.y+end.x-start.x;
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1033
Author:Jeick Jia @ CS-SCU
CODE:
#include<iostream>
#include<string>
using namespace std;
struct Point{
int x,y;
Point():x(0),y(0){}
Point(int _x,int _y)
{
x = _x; y = _y;
}
};
Point start(300,420),ed(310,420),pt;
void Rotate(string& direction)
{
int len = direction.size();
cout<<start.x<<' '<<start.y<<" moveto"<<endl;
cout<<ed.x<<' '<<ed.y<<" lineto"<<endl;
for(int i=0;i<len;i++)
{
if('A'==direction[i])
{
pt.x = ed.x + ed.y-start.y;
pt.y = ed.y + start.x - ed.x;
}
else
{
pt.x = ed.x + start.y - ed.y;
pt.y = ed.y + ed.x - start.x;
}
start = ed;ed = pt;
cout<< ed.x <<' '<< ed.y <<" lineto"<<endl;
}
cout<<"stroke"<<endl;
cout<<"showpage"<<endl;
}
int main()
{
string dir;
while(cin>>dir)
{
Rotate(dir);
dir="";
start.x = 300;
start.y = 420;
ed.x = 310;
ed.y = 420;
}
return 0;
}
转载于:https://blog.51cto.com/jeick/557890