Problem E
The Other Two Trees
Input: standard input
Output: standard output
Time Limit: 2 seconds
You have a quadrilateral shaped land whose opposite fences are of equal length. You have four neighbors whose lands are exactly adjacent to your four fences, that means you have a common fence with all of them. For example if you have a fence of length d in one side, this fence of length d is also the fence of the adjacent neighbor on that side. The adjacent neighbors have no fence in common among themselves and their lands also don’t intersect. The main difference between their land and your land is that their lands are all square shaped. All your neighbors have a tree at the center of their lands. Given the Cartesian coordinates of trees of two opposite neighbors, you will have to find the Cartesian coordinates of the other two trees.
Input
The input file contains several lines of input. Each line contains four floating point or integer numbers x1, y1, x2, y2, where (x1, y1), (x2, y2) are the coordinates of the trees of two opposite neighbors. Input is terminated by end of file.
Output
For each line of input produce one line of output which contains the line “Impossible.” without the quotes, if you cannot determine the coordinates of the other two trees. Otherwise, print four floating point numbers separated by a single space with ten digits after the decimal point ax1, ay1, ax2, ay2, where (ax1, ay1) and (ax2, ay2) are the coordinates of the other two trees. The output will be checked with special judge program, so don’t worry about the ordering of the points or small precision errors. The sample output will make it clear.
Sample Input
10 0 -10 0
10 0 -10 0
10 0 -10 0
Sample Output
0.0000000000 10.0000000000 0.0000000000 -10.0000000000
0.0000000000 10.0000000000 -0.0000000000 -10.0000000000
0.0000000000 -10.0000000000 0.0000000000 10.0000000000
其实对于数学基础的题目,我现在真的是不会多少,能力还不足以搞出来正确有效的公式。
不过题目还是要做的~不会的可以查,不懂的可以慢慢学,学的多了见得多了,自然也就会做了~
#include<stdio.h>
#include<math.h>
int main()
{
double x1,y1,x2,y2;
while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2)!=EOF)
{
if(x1==x2&&y1==y2)
printf("Impossible.\n");
double x,y;
x=(x1+x2)/2;
y=(y1+y2)/2;
printf("%.10lf %.10lf %.10lf %.10lf\n",(y1-y)+x,-(x1-x)+y,(y2-y)+x,-(x2-x)+y);
}
return 0;
}
其实我一直在想给出的矩形会不会就一定是 边平行于坐标轴的那种呢?
后来发现这个想法真是太二了。。怎么可能只给边平行于坐标轴的那种呢?
这个问题可以抽象,很容易就发现 相对的邻居的中点的距离是相等的,这一点自己画图就可以看得出来。
然后就抽象成了 已知正方形的一条对角线的两个端点的坐标,求另外两个相对端点的坐标;
抛弃掉笛卡尔坐标系 直接由正方形对角线两个端点的坐标去推另外两个端点的坐标:
即使所抽象出来的正方形的边并不是平行于坐标轴的,这些变相对于坐标轴的偏移量其实是相同的(当然你也可以据此理解成他们的坐标的改变量△实际上也是相同的)
求另外的两个点的坐标的公式如下:
x3= (y1-y)+x;
y3= -(x1-x)+y;
x4= (y2-y)+x;
y4=-(x2-x)+y;
我也不知道为什么会是这个样子。。但我要学会。。