Problem E
The Other Two Trees
Input: standard input
Output: standard output
Time Limit: 2 seconds
You have a quadrilateral shapedland whose opposite fences are of equal length. You have four neighbors whose landsare exactly adjacent to your four fences, that meansyou have a common fence with all of them. For example if you have a fence oflength d in one side, this fence oflength d is also the fence of theadjacent neighbor on that side. The adjacent neighbors have no fence in commonamong themselves and their lands also don’t intersect. The main differencebetween their land and your land is that their lands are all square shaped. Allyour neighbors have a tree at the center of their lands. Given the Cartesiancoordinates of trees of two opposite neighbors, you will have to find theCartesian coordinates of the other two trees.
Input
Theinput file contains several lines of input. Each line contains four floatingpoint or integer numbers x1, y1, x2, y2,where (x1, y1), (x2, y2) are thecoordinates of the trees of two opposite neighbors. Input is terminated by endof file.
Output
For each line of input produce one line of output which contains theline “Impossible.” without thequotes, if you cannot determine the coordinates of the other two trees.Otherwise, print four floating point numbers separated by a single space withten 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 bechecked with special judge program, so don’t worry about the ordering of thepoints 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
题目意思就是已知一个正方形对角线的两个点的坐标,求另外两个点的坐标。。
根据两个点的坐标,求出中点坐标坐标,在推一推就能等到公式了。。
x3 = (x1 + x2 +y2 - y1) / 2;
y3 = (y1 + y2 + x1 - x2) / 2;
x4 = (x1 + x2 - y2 + y1) / 2;
y4 = (y1 + y2 - x1 +x2) / 2;
AC代码:
#include<stdio.h>
#include<cmath>
int main () {
double x1,y1,x2,y2,x3,y3,x4,y4;
while (scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2) != EOF) {
x3 = (x1 + x2 +y2 - y1) / 2;
y3 = (y1 + y2 + x1 - x2) / 2;
x4 = (x1 + x2 - y2 + y1) / 2;
y4 = (y1 + y2 - x1 +x2) / 2;
printf("%.10lf %.10lf %.10lf %.10lf\n",x3,y3,x4,y4);
}
return 0;
}