poj1329Circle Through Three Points(三角形外接圆)

链接

套模板

不知道有没有x,y=0情况,不过这种情况都按+号输出的。

 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 struct Circle

23 {

24     Point center;

25     double r;

26 };

27 typedef Point pointt;

28 pointt operator - (Point a,Point b)

29 {

30     return Point(a.x-b.x,a.y-b.y);

31 }

32 int dcmp(double x)

33 {

34     if(fabs(x)<eps) return 0;

35     return x<0?-1:1;

36 }

37 double dis(Point a)

38 {

39     return a.x*a.x+a.y*a.y;

40 }

41 double cross(Point a,Point b)

42 {

43     return a.x*b.y-a.y*b.x;

44 }

45 double area()

46 {

47     return fabs(cross(p[1]-p[2],p[2]-p[3]))/2;

48 }

49 struct Circle Circumcircle()

50 {

51     Circle tmp;

52     double a,b,c,c1,c2;

53     double xa,ya,xb,yb,xc,yc;

54     a = sqrt(dis(p[3]-p[1]));

55     b = sqrt(dis(p[1]-p[2]));

56     c = sqrt(dis(p[2]-p[3]));

57     //根据s = a*b*c/R/4,求半径

58     tmp.r = (a*b*c)/(area()*4.0);

59     xa = p[3].x;

60     ya = p[3].y;

61     xb = p[1].x;

62     yb = p[1].y;

63     xc = p[2].x;

64     yc = p[2].y;

65     c1 = (dis(p[3])-dis(p[1]))/2;

66     c2 = (dis(p[3])-dis(p[2]))/2;

67     tmp.center.x = (c1*(ya-yc)-c2*(ya-yb))/((xa-xb)*(ya-yc)-(xa-xc)*(ya-yb));

68     tmp.center.y = (c1*(xa-xc)-c2*(xa-xb))/((ya-yb)*(xa-xc)-(ya-yc)*(xa-xb));

69     return tmp;

70 }

71 int main()

72 {

73     while(scanf("%lf%lf%lf%lf%lf%lf",&p[1].x,&p[1].y,&p[2].x,&p[2].y,&p[3].x,&p[3].y)!=EOF)

74     {

75         Circle cc = Circumcircle();

76         double r = cc.r;

77         char c1,c2;

78         c1 = dcmp(cc.center.x)>0?'-':'+';

79         c2 = dcmp(cc.center.y)>0?'-':'+';

80         printf("(x %c %.3f)^2 + (y %c %.3f)^2 = %.3f^2\n",c1,fabs(cc.center.x),c2,fabs(cc.center.y),r);

81         double s = r*r-cc.center.x*cc.center.x-cc.center.y*cc.center.y;

82         char c3 = dcmp(s)>0?'-':'+';

83         printf("x^2 + y^2 %c %.3fx %c %.3fy %c %.3f = 0\n",c1,fabs(cc.center.x*2),c2,fabs(cc.center.y*2),c3,fabs(s));

84         puts("");

85     }

86     return 0;

87 }
View Code

 

 

你可能感兴趣的:(poi)