UVa 10250 - The Other Two Trees

来源:互联网 发布:相约星期六网络直播 编辑:程序博客网 时间:2024/05/17 07:31

题目:已知2点,求另外两点,使得这4个点构成正方形。

分析:计算几何。直接求出已知线段的长度相同的垂直平分线即可。求中点,利用向量计算点坐标。

说明:注意精度。

#include <iostream>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;typedef struct pnode{double x,y;}point;point A,B,C,D,M;double dist( point a, point b ){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){while ( ~scanf("%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y) ) {M.x = (A.x+B.x)*0.5;M.y = (A.y+B.y)*0.5;double l = dist( A, B );double sinA = (B.y-A.y)/l;double cosA = (B.x-A.x)/l;C.x = M.x - 0.5*l * sinA;C.y = M.y + 0.5*l * cosA;D.x = M.x + 0.5*l * sinA;D.y = M.y - 0.5*l * cosA;printf("%.10lf %.10lf %.10lf %.10lf\n",C.x,C.y,D.x,D.y);}return 0;}

0 0