题目如下:
4th Point
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5327 Accepted: 1859
Description
Given are the (x,y) coordinates of the endpoints of two adjacent sides of a parallelogram. Find the (x,y) coordinates of the fourth point.
Input
Each line of input contains eight floating point numbers: the (x,y) coordinates of one of the endpoints of the first side followed by the (x,y) coordinates of the other endpoint of the first side, followed by the (x,y) coordinates of one of the endpoints of the second side followed by the (x,y) coordinates of the other endpoint of the second side. All coordinates are in meters, to the nearest mm. All coordinates are between -10000 and +10000.
Output
For each line of input, print the (x,y) coordinates of the fourth point of the parallelogram in meters, to the nearest mm, separated by a single space.
Sample Input
0.000 0.000 0.000 1.000 0.000 1.000 1.000 1.000
1.000 0.000 3.500 3.500 3.500 3.500 0.000 1.000
1.866 0.000 3.127 3.543 3.127 3.543 1.412 3.145
Sample Output
1.000 0.000
-2.500 -2.500
0.151 -0.398
题目大意就是:
给出平行四边形的两个边,我们可以得到三个点坐标,求第四个点的坐标
这个是计算几何里面比较简单的题目,可以说是入门把,但是这个题目主要要求的就是我们对于情况的分析要足够到位才行,
首先题目给了我们四个点,那么肯定有两个点是重合的,那么一共就有四种情况,我们一次分析,我们分别设这四个点为
A(x1,y1),B(x2,y2),C(x3,y3),D(x4,y4),E(x,y);
1.当x1==x3时:
x=x2+x4-x1;
y=y2+y4-y1;
2.当x1==x4时:
x=x2+x3-x1;
y=y2+y3-y1;
3.当x2==x3时:
x=x1+x4-x2;
y=y1+y4-y2;
4.当x2==x4时:
x=x1+x3-x2;
y=y1+y3-y2;
怎么算的呢,我们随便举个例子,比如就是 A == D 的时候,我们根据向量的表示法可以得到,AB+ AC = AE , DC + CE = AE,将这两个式子合并再代入
x1 == x4,y1 == y4,
我们就可以得到2中的式子了;
AC代码如下:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double x,x1,x2,x3,x4,y,y1,y2,y3,y4;
while(cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4)
{
if(x1==x3&&y1==y3)
{
x=x2+x4-x1;
y=y2+y4-y1;
}
else if(x1==x4&&y1==y4)
{
x=x2+x3-x1;
y=y2+y3-y1;
}
else if(x2==x3&&y2==y3)
{
x=x1+x4-x2;
y=y1+y4-y2;
}
else if(x2==x4&&y2==y4)
{
x=x1+x3-x2;
y=y1+y3-y2;
}
printf("%.3lf %.3lf\n",x,y);
}
return 0;
}