Smile

Smile

常用链接

统计

最新评论

hnu 12057 Cutting the Cake

  1 #include <cstdio>
  2 #include <cmath>
  3 
  4 double const EPS = 1e-9;
  5 
  6 struct point_d{
  7     double x, y;
  8     point_d(){}
  9     point_d(point_d const &p): x(p.x), y(p.y){}
 10     point_d(double const &dx, double const &dy): x(dx), y(dy){}
 11     point_d operator=(point_d const point){
 12         x = point.x;
 13         y = point.y;
 14         return *this;
 15     }
 16 };
 17 
 18 struct vector_d : public point_d{
 19     vector_d(){}
 20     vector_d(point_d const &p0, point_d const &p1){
 21         x = p1.x - p0.x;
 22         y = p1.y - p0.y;
 23     }
 24     vector_d operator=(vector_d const v){
 25         x = v.x;
 26         y = v.y;
 27         return *this;
 28     }
 29 };
 30 
 31 struct K{
 32     double k0, k1;
 33     int flag;
 34     K():flag(1){}
 35     struct K operator=(struct K const k){
 36         k0 = k.k0; k1 = k.k1; flag = k.flag;
 37         return *this;
 38     }
 39 };
 40 
 41 struct line_d{
 42     double a, b, c;
 43 };
 44 
 45 double distance(point_d p0, point_d p1){
 46     return sqrt((p1.y - p0.y) * (p1.y - p0.y) + (p1.x - p0.x) * (p1.x - p0.x));
 47 }
 48 
 49 K func(double ls, double e0, double e1){
 50     K k0, k1, k2;
 51     k2.flag = 0;
 52     if(ls * ls / 4.0 <= 2.0 * e0 * e1 - EPS)
 53         return k2;
 54     double r2 = sqrt(ls * ls / 4.0 - 2.0 * e0 * e1);
 55     k0.k1 = (ls / 2.0 - r2) / 2.0 / e0;
 56     k1.k1 = (ls / 2.0 + r2) / 2.0 / e0;
 57     k0.k0 = (ls / 2.0 - k0.k1 * e0) / e1;
 58     k1.k0 = (ls / 2.0 - k1.k1 * e0) / e1;
 59     if(0 <= k0.k0 + EPS && k0.k0 <= 1.0 + EPS &&
 60        0 <= k0.k1 + EPS && k0.k1 <= 1.0 + EPS)
 61         return k0;
 62     if(0 <= k1.k0 + EPS && k1.k0 <= 1.0 + EPS &&
 63        0 <= k1.k1 + EPS && k1.k1 <= 1.0 + EPS)
 64         return k1;
 65     return k2;
 66 }
 67 
 68 line_d get_line(point_d p, point_d q){
 69     line_d line;
 70     line.a = -(q.y - p.y);
 71     line.b = q.x - p.x;
 72     line.c = line.a * q.x + line.b * q.y;
 73     if(line.a < 0)
 74         line.a *= -1.0, line.b *= -1.0, line.c *= -1.0;
 75     double d = sqrt(line.a * line.a + line.b * line.b);
 76     line.a /= d; line.b /= d; line.c /= d;
 77     line.a += EPS; line.b += EPS; line.c += EPS;
 78     return line;
 79 }
 80 
 81 
 82 point_d get_point(point_d start, vector_d vec, double k){
 83     return point_d(start.x + vec.x * k, start.y + vec.y * k);
 84 }
 85 
 86 int main(void){
 87     int t, cas;
 88     point_d a, b, c, p, q;
 89     double ls, eab, eac, ebc;
 90     K k;
 91     line_d line;
 92 
 93     scanf("%d"&t);
 94     while(t--){
 95         scanf("%d%lf%lf%lf%lf%lf%lf"&cas, &a.x, &a.y, &b.x, &b.y,\
 96                                       &c.x, &c.y);
 97         eab = distance(a, b);
 98         eac = distance(a, c);
 99         ebc = distance(b, c);
100         ls = eab + eac + ebc;
101 
102         k = func(ls, eab, eac);// a, bc
103         if(k.flag){
104             p = get_point(a, vector_d(a, b), k.k1);//ab
105             q = get_point(a, vector_d(a, c), k.k0);//ac
106             line = get_line(p, q);
107             printf("%d %.5lf %.5lf %.5lf\n", cas, line.a, line.b, line.c);
108             continue;
109         }
110         k = func(ls, eab, ebc);// b, a,c
111         if(k.flag){
112             p = get_point(b, vector_d(b, a), k.k1);//ba
113             q = get_point(b, vector_d(b, c), k.k0);//bc
114             line = get_line(p, q);
115             printf("%d %.5lf %.5lf %.5lf\n", cas, line.a, line.b, line.c);
116             continue;
117         }
118         k = func(ls, eac, ebc);//c, ab
119         if(k.flag){
120             p = get_point(c, vector_d(c, a), k.k1);//ca
121             q = get_point(c, vector_d(c, b), k.k0);//cb
122             line = get_line(p, q);
123             printf("%d %.5lf %.5lf %.5lf\n", cas, line.a, line.b, line.c);
124             continue;
125         }
126     }
127 
128     return 0;
129 }

posted on 2011-08-01 17:35 Smile3 阅读(89) 评论(0)  编辑 收藏 引用 所属分类: 其他题目