poj1673EXOCENTER OF A TRIANGLE
据说这题是垂心。。数学太弱没有看出来,写了分朴实无华的代码。。
旋转三边得到图中的外顶点,然后连接三角形顶点求交点,交上WA。。觉得没什么错误就去看了下discuss,发现都在说精度问题,果断开始水,最后+了epsAC了。。
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 100000 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 struct Point 18 { 19 double x,y; 20 Point(double x=0,double y=0):x(x),y(y) {} 21 }p[5]; 22 typedef Point pointt; 23 pointt operator + (Point a,Point b) 24 { 25 return Point(a.x+b.x,a.y+b.y); 26 } 27 pointt operator - (Point a,Point b) 28 { 29 return Point(a.x-b.x,a.y-b.y); 30 } 31 int dcmp(double x) 32 { 33 if(fabs(x)<eps) return 0; 34 else return x<0?-1:1; 35 } 36 Point rotate(Point a,double rad) 37 { 38 return Point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad)); 39 } 40 bool intersection1(Point p1, Point p2, Point p3, Point p4, Point& p) // 直线相交 41 { 42 double a1, b1, c1, a2, b2, c2, d; 43 a1 = p1.y - p2.y; 44 b1 = p2.x - p1.x; 45 c1 = p1.x*p2.y - p2.x*p1.y; 46 a2 = p3.y - p4.y; 47 b2 = p4.x - p3.x; 48 c2 = p3.x*p4.y - p4.x*p3.y; 49 d = a1*b2 - a2*b1; 50 if (!dcmp(d)) return false; 51 p.x = (-c1*b2 + c2*b1) / d; 52 p.y = (-a1*c2 + a2*c1) / d; 53 return true; 54 } 55 double cross(Point a,Point b) 56 { 57 return a.x*b.y-a.y*b.x; 58 } 59 double mul(Point p0,Point p1,Point p2) 60 { 61 return cross(p1-p0,p2-p0); 62 } 63 int main() 64 { 65 int n,i; 66 cin>>n; 67 while(n--) 68 { 69 for(i = 1; i <= 3 ; i++) 70 scanf("%lf%lf",&p[i].x,&p[i].y); 71 Point p1,p2,p3,p4; 72 if(dcmp(mul(p[1],p[2],p[3]))>0) 73 { 74 p1 = rotate(p[3]-p[1],3*pi/2.0); 75 p2 = rotate(p[2]-p[1],pi/2.0); 76 } 77 else 78 { 79 p1 = rotate(p[3]-p[1],pi/2.0); 80 p2 = rotate(p[2]-p[1],3*pi/2.0); 81 } 82 p1.x+=p[1].x; 83 p1.y+=p[1].y; 84 p2.x+=p[1].x; 85 p2.y+=p[1].y; 86 p1.x = (p1.x+p2.x)/2; 87 p1.y = (p1.y+p2.y)/2; 88 89 if(dcmp(mul(p[2],p[1],p[3]))>0) 90 { 91 p3 = rotate(p[3]-p[2],3*pi/2.0); 92 p4 = rotate(p[1]-p[2],pi/2.0); 93 } 94 else 95 { 96 p3 = rotate(p[3]-p[2],pi/2.0); 97 p4 = rotate(p[1]-p[2],3*pi/2.0); 98 } 99 p3.x+=p[2].x; 100 p3.y+=p[2].y; 101 p4.x+=p[2].x; 102 p4.y+=p[2].y; 103 p3.x = (p3.x+p4.x)/2; 104 p3.y = (p3.y+p4.y)/2; 105 Point pp ; 106 intersection1(p1,p[1],p3,p[2],pp); 107 printf("%.4f %.4f\n",pp.x+eps,pp.y+eps); 108 109 } 110 return 0; 111 }