传送门UVa 10250 - The Other Two Trees
题意是给出两个点的坐标, 求以他们的连线为斜边的正方形的另外两个顶点坐标.
一开始捣鼓了半天弄不出来, 后来看到有人说可以用旋转坐标系来做, 就去百度了一下公式.
设在复平面中:原曲线上一点直角坐标(x,y),原曲线绕坐标原点旋转α角后该点对应直角坐标(x',y')
则:(x,yi)*(cosα,isinα)=(x',y'i)
即:(x',y'i)=(xcosα-ysinα,i(xsinα+ycosα))
所以:x'= xcosα-ysinα;y'= xsinα+ycosα
假设一开始的点是A和C, 向量AC = (x, y)那么可以把A看成原点, 得到逆时针旋转45°的向量AB.
∵AB = OB - OA. ∴OB = AB + OA. 这样点B的坐标就出来了.
也就是这个公式
详情见代码
#include <cstdio>
using namespace std;
int main()
{
//freopen("input.txt", "r", stdin);
double a0, b0, a1, b1, a2, b2, a3, b3;
while (~scanf("%lf%lf%lf%lf", &a0, &b0, &a1, &b1))
{
if (a0 == a1 && b0 == b1)
{
printf("Impossible.\n");
continue;
}
else
{
a2 = (a1 + a0 - b1 + b0) / 2;
b2 = (a1 + b1 - a0 + b0) / 2;
a3 = (a1 + a0 + b1 - b0) / 2;
b3 = (a0 - a1 + b1 + b0) / 2;
printf("%.10lf %.10lf %.10lf %.10lf\n", a2, b2, a3, b3);
}
}
return 0;
}