首页 > 代码库 > poj1266Cover an Arc(三角形外接圆)

poj1266Cover an Arc(三角形外接圆)

链接

求出三角形的外接圆,通过圆心和半径可以知道这个圆的上下左右最远点,分别判断这个四个点跟弧的两端点A,B的关系,假如判断P点,弧内给出点为C,判断PC是否与AB相交即可判断出P是否在弧上。

精度问题 ceil-eps floor+eps

  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[4]; 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 struct Circle 37 { 38     Point center; 39     double r; 40 }; 41 double cross(Point a,Point b) 42 { 43     return a.x*b.y-a.y*b.x; 44 } 45 double mul(Point p0,Point p1,Point p2) 46 { 47     return cross(p1-p0,p2-p0); 48 } 49 double dis(Point a) 50 { 51     return a.x*a.x+a.y*a.y; 52 } 53 double area() 54 { 55     return fabs(cross(p[1]-p[3],p[2]-p[3]))/2; 56 } 57 bool seginter(pointt a1,pointt a2,pointt b1,pointt b2) 58 { 59      double c1 = cross(a2-a1,b1-a1),c2 = cross(a2-a1,b2-a1), 60             c3 = cross(b2-b1,a1-b1),c4 = cross(b2-b1,a2-b1); 61      return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0; 62 } 63 struct Circle Circumcircle() 64 { 65     Circle tmp; 66     double a,b,c,c1,c2; 67     double xa,ya,xb,yb,xc,yc; 68     a = sqrt(dis(p[3]-p[1])); 69     b = sqrt(dis(p[1]-p[2])); 70     c = sqrt(dis(p[2]-p[3])); 71     //根据s = a*b*c/R/4,求半径 72     tmp.r = (a*b*c)/(area()*4.0); 73     xa = p[3].x; 74     ya = p[3].y; 75     xb = p[1].x; 76     yb = p[1].y; 77     xc = p[2].x; 78     yc = p[2].y; 79     c1 = (dis(p[3])-dis(p[1]))/2; 80     c2 = (dis(p[3])-dis(p[2]))/2; 81     tmp.center.x = (c1*(ya-yc)-c2*(ya-yb))/((xa-xb)*(ya-yc)-(xa-xc)*(ya-yb)); 82     tmp.center.y = (c1*(xa-xc)-c2*(xa-xb))/((ya-yb)*(xa-xc)-(ya-yc)*(xa-xb)); 83     return tmp; 84 } 85 int main() 86 { 87     int i; 88     double r; 89     int w0,w1,h0,h1; 90     for(i = 1; i <= 3 ; i++) 91     scanf("%lf%lf",&p[i].x,&p[i].y); 92     Circle cc = Circumcircle(); 93     r = cc.r; 94  95     Point q[5]; 96     for(i = 1 ;i < 5 ;i++) 97     q[i] = cc.center; 98     q[1].x-=r,q[2].x+=r,q[3].y-=r,q[4].y+=r; 99 100     if(!seginter(q[1],p[3],p[1],p[2])) w0 = floor(q[1].x+eps);101     else w0 = floor(min(p[1].x,p[2].x)+eps);102 103     if(!seginter(q[2],p[3],p[1],p[2]))  w1 = ceil(q[2].x-eps);104     else w1 = ceil(max(p[1].x,p[2].x)-eps);105 106     if(!seginter(q[3],p[3],p[1],p[2])) h0 = floor(q[3].y+eps);107     else h0 = floor(min(p[1].y,p[2].y)+eps);108 109     if(!seginter(q[4],p[3],p[1],p[2])) h1 = ceil(q[4].y-eps);110     else h1 = ceil(max(p[1].y,p[2].y)-eps);111 112     printf("%d\n",(h1-h0)*(w1-w0));113     return 0;114 }
View Code