4th Point
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5066 | Accepted: 1765 |
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
Source
题意:给出平行四边形的两邻边的端点坐标,求第四个点的坐标。
一开始没怎么看题目,看了一下input就默认第二点和第三点为两条邻边的的连接点了,知道错了之后就把代码改得这么长了(-_-;)
很奇怪的是用g++上交就WA,c++就AC,也是很迷
#include<iostream>
#include<cstdio>
#include<cmath>
#define esp 1e-8
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B)
{
return Vector(A.x+B.x,A.y+B.y);
}
Vector operator - (Vector A,Vector B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator * (Vector A,double p)
{
return Vector(A.x*p,A.y*p);
}
Vector operator / (Vector A,double p)
{
return Vector(A.x/p,A.y/p);
}
int dcmp(double x)
{
if(fabs(x)<esp) return 0;
else return x<0?-1:1;
}
bool operator == (const Point& a,const Point& b)
{
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y)==0;
}
int main()
{
//freopen("in.txt","r",stdin);
Vector a,b,c,d;
while(~scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y))
{
if(a==b)
{
Vector Ans=d-a-(b-c);
printf("%.3lf %.3lf\n",Ans.x+a.x,Ans.y+a.y);
}
if(a==c)
{
Vector Ans=d-c-(a-b);
printf("%.3lf %.3lf\n",Ans.x+c.x,Ans.y+c.y);
}
if(a==d)
{
Vector Ans=c-d-(a-b);
printf("%.3lf %.3lf\n",Ans.x+a.x,Ans.y+a.y);
}
if(b==c)
{
Vector Ans=d-c-(b-a);
printf("%.3lf %.3lf\n",Ans.x+c.x,Ans.y+c.y);
}
if(b==d)
{
Vector Ans=c-d-(b-a);
printf("%.3lf %.3lf\n",Ans.x+b.x,Ans.y+b.y);
}
if(c==d)
{
Vector Ans=b-c-(d-a);
printf("%.3lf %.3lf\n",Ans.x+c.x,Ans.y+c.y);
}
}
return 0;
}
计算几何基础——【点积和叉积的用处】