-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfinal.cpp
2337 lines (2329 loc) · 72.8 KB
/
final.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// vimrc
set guifont=Monaco:h08:b
set cin nu rnu ts=4 sw=4 sts=4 et noswapfile nobackup
set so=100
set backspace=eol,start,indent
"colorscheme last256
syntax on
map <F4> :!g++ -std=c++11 %<.cpp -m32 -Wall -o %<<cr>
map <F5> :!%< < %<.in<cr>
map <F6> :vsplit %<.in<cr>
typedef double DB;
#define op operator
const DB eps = 1e-8;
inline int sgn(DB x) { return x < -eps ? -1 : x > eps; }
struct PT {
DB x, y;
PT (DB x=0, DB y = 0) : x(x), y(y){ }
PT norm() { return PT(-y, x); }
PT rotate(DB ang) { return PT(x * cos(ang) - y * sin(ang), x * sin(ang) + y * cos(ang)); }
};
//矢量V以P为顶点逆时针旋转angle并放大scale倍
point rotate(point v,point p,double angle,double scale){
point ret=p;
v.x-=p.x,v.y-=p.y;
p.x=scale*cos(angle);
p.y=scale*sin(angle);
ret.x+=v.x*p.x-v.y*p.y;
ret.y+=v.x*p.y+v.y*p.x;
return ret;
}
struct Seg { PT s, e; }
struct Line{ int a, b, c; };
struct Cir{PT ct; DB r;}
bool dot_on_seg(PT p, Seg L) { return sgn((L.s - p) * (L.e - p)) == 0 && sgn((L.s - p).dot(L.e - p)) <= 0; }
DB ppdis(PT a, PT b) { return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ); }
bool intersect(PT P, PT v, PT Q, PT w, PT &p) {
PT u = P - Q;
if(sgn(v * w) == 0) return false;
double t = w * u / (v * w);
p = P + v * t;
return true;
}
double cross(PT a, PT b, PT c){ return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);}
PT disptoline(PT p, Seg l) { return fabs(cross(p, l.s, l.e)) / ppdis(l.s, l.e); }
PT ptoline(PT p, Seg l) { PT vec = l.s - l.e; return intersect(p, vec.norm(), l.s, vec); }
PT ptoseg(PT p, Seg l) {
PT norm = (l.s - l.e).norm();
if(sgn(norm * (p - l.s)) * sgn(norm * (p - l.e)) > 0) {
double sa = ppdis(p, l.s);
double sb = ppdis(p, l.e);
return sgn(sa - sb) < 0 ? l.s : l.e;
}
return intersect(p, norm, l.s, l.e - l.s);
}
bool point_in_polygon(PT o, PT *p, int n, bool flag) { //传入flag表示在边界上算不算在里面
int t = 0; PT a, b;
for(int i = 0; i < n; i++) if(dot_on_seg(o, Seg(p[i], p[(i + 1) % n]))) return flag;
for(int i = 0; i < n; i++) {
a = p[i]; b = p[(i + 1) % n];
if(a.y > b.y) std::swap(a, b);
if(sgn((a - o) * (b - o)) < 0 && sgn(a.y - o.y) < 0 && sgn(o.y - b.y) <= 0)
t++;
}
return t & 1;
}
bool segcross(PT p1, PT p2, PT q1, PT q2) { // 快速判断线段相交
return (
std::min(p1.x, p2.x) <= std::max(q1.x, q2.x) &&
std::min(q1.x, q2.x) <= std::max(p1.x, p2.x) &&
std::min(p1.y, p2.y) <= std::max(q1.y, q2.y) &&
std::min(q1.y, q2.y) <= std::max(p1.y, p2.y) &&
cross(p1, q2, q1) * cross(p2, q2, q1) <= 0 &&
cross(q1, p2, p1) * cross(q2, p2, p1) <= 0
);
}
void convex(PT *p, int &n) {
if(n < 3) { return ; }
std::sort(p, p + n);
std::copy(p, p + n - 1, p + n);
std::reverse(p + n, p + 2 * n - 1);
int m = 0, top = 0;
for(int i = 0; i < 2 * n - 1; i++) {
while(top >= m + 2 && sgn((p[top - 1] - p[top - 2]) * (p[i] - p[top - 2])) <= 0) { top --; }
p[top++] = p[i];
if(i == n - 1) { m = top - 1; }
}
n = top - 1;
}
int inhalfplane(point p,point s,point e) { return sgn(cross(e - s, p - s)) ; }
std::vector<point> CUT(const std::vector<point> &p, point s, point e) {
std::vector<point> q;
int n = (int) p.size();
for(int i = 0; i < n; i++) {
int nowin = inhalfplane(p[i], s, e);
int nextin = inhalfplane(p[(i + 1) % n], s, e);
if(nowin >= 0)
q.push_back(p[i]);
if(nextin * nowin < 0)
q.push_back(intersect(p[i], p[(i + 1) % n] - p[i], s, e - s));
}
return q;
}
bool in_convex(PT *p, PT pt, int n) {
if(sgn((p[1]-p[0])*(pt-p[0])) <= 0 || sgn((p[n-1]-p[0])*(pt-p[0])) >= 0) { return false; }
int l = 1, r = n - 2, best = -1;
while(l <= r) {
int mid = l + r >> 1;
int f = sgn((p[mid]-p[0])*(pt-p[0]));
if(f >= 0) {
best = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return sgn((p[best+1]-p[best])*(pt-p[best])) > 0;
}
//圆的折射,输入圆心,p->inter是射线,inter是圆上一点, ref是折射率,返回折射向量
Vector reflect_vector(PT center, PT p, PT inter, double ref) {
Vector p1 = inter - p, p2 = center - inter;
double sinang = p1 * p2 / (p1.vlen() * p2.vlen()) / ref;
double ang = asin(fabs(sinang));
return sinang > eps ? p2.rotate(-ang) : p2.rotate(ang);
}
bool cir_line(PT ct, double r, PT l1, PT l2, PT& p1, PT& p2) {
if ( sgn (pldis(ct, l1, l2) - r ) > 0)
return false;
double a1, a2, b1, b2, A, B, C, t1, t2;
a1 = l2.x - l1.x; a2 = l2.y - l1.y;
b1 = l1.x - ct.x; b2 = l1.y - ct.y;
A = a1 * a1 + a2 * a2;
B = (a1 * b1 + a2 * b2) * 2;
C = b1 * b1 + b2 * b2 - r * r;
t1 = (-B - sqrt(B * B - 4.0 * A * C)) / 2.0 / A;
t2 = (-B + sqrt(B * B - 4.0 * A * C)) / 2.0 / A;
p1.x = l1.x + a1 * t1; p1.y = l1.y + a2 * t1;
p2.x = l1.x + a1 * t2; p2.y = l1.y + a2 * t2;
return true;
}
bool cir_cir(PT c1, double r1, PT c2, double r2, PT& p1, PT& p2) {
double d = ppdis(c1, c2);
if ( sgn(d - r1 - r2) > 0|| sgn (d - fabs(r1 - r2) ) < 0 )
return false;
PT u, v;
double t = (1 + (r1 * r1 - r2 * r2) / ppdis(c1, c2) / ppdis(c1, c2)) / 2;
u.x = c1.x + (c2.x - c1.x) * t;
u.y = c1.y + (c2.y - c1.y) * t;
v.x = u.x + c1.y - c2.y;
v.y = u.y + c2.x - c1.x;
cir_line(c1, r1, u, v, p1, p2);
return true;
}
struct Point // 求圆并
{
double x,y;
Point(double a=0.0,double b=0.0){x=a;y=b;}
Point operator+(const Point&a)const{return Point(x+a.x,y+a.y);}
Point operator-(const Point&a)const{return Point(x-a.x,y-a.y);}
Point operator*(const double&a)const{return Point(x*a,y*a);}
Point operator/(const double&a)const{return Point(x/a,y/a);}
double operator*(const Point&a)const{return x*a.y-y*a.x;}
double operator/(const Point&a)const{return sqrt((a.x-x)*(a.x-x)+(a.y-y)*(a.y-y));}
double operator%(const Point&a)const{return x*a.x+y*a.y;}
}po[1005];
double r[1005];
const double eps = 1e-7;
const double pi=acos(-1.0);
inline int sgn(double x)
{return fabs(x)<eps?0:(x>0.0?1:-1);}
pair<double,bool>arg[2005];
double cir_union(Point c[],double r[],int n) {
double sum=0.0,sum1=0.0,d,p1,p2,p3;
for(int i=0;i<n;i++) {
bool f=1;
for(int j=0;f&&j<n;j++)
if(i!=j&&sgn(r[j]-r[i]-c[i]/c[j])!=-1)f=0;
if(!f)swap(r[i],r[--n]),swap(c[i--],c[n]);
}
for(int i=0;i<n;i++) {
int k=0,cnt=0;
for(int j=0;j<n;j++)
if(i!=j&&sgn((d=c[i]/c[j])-r[i]-r[j])<=0) {
p3=acos((r[i]*r[i]+d*d-r[j]*r[j])/(2.0*r[i]*d));
p2=atan2(c[j].y-c[i].y,c[j].x-c[i].x);
p1=p2-p3;p2=p2+p3;
if(sgn(p1+pi)==-1)p1+=2*pi,cnt++;
if(sgn(p2-pi)==1)p2-=2*pi,cnt++;
arg[k++]=make_pair(p1,0);arg[k++]=make_pair(p2,1);
}
if(k) {
sort(arg,arg+k);
p1=arg[k-1].first-2*pi;
p3=r[i]*r[i];
for(int j=0;j<k;j++) {
p2=arg[j].first;
if(cnt==0) {
sum+=(p2-p1-sin(p2-p1))*p3;
sum1+=(c[i]+Point(cos(p1),sin(p1))*r[i])*(c[i]+Point(cos(p2),sin(p2))*r[i]);
}
p1=p2;
arg[j].second?cnt--:cnt++;
}
}
else sum+=2*pi*r[i]*r[i];
}
return (sum+fabs(sum1))*0.5;
}
double multi(PT &o, PT &a, PT &b) { return (a.x-o.x)*(b.x-o.x) + (a.y-o.y)*(b.y-o.y); }
double cross(PT &o, PT &a, PT &b) {return (a.x-o.x)*(b.y-o.y) - (b.x-o.x)*(a.y-o.y); }
double cp(PT &a, PT &b) {return a.x*b.y - b.x*a.y; }
double angle(PT &a, PT &b) { // 两向量夹角
double ans = fabs((atan2(a.y, a.x) - atan2(b.y, b.x)));
return ans > pi+eps ? 2*pi-ans : ans;
}
double cir_polygon(PT ct, double R, PT *p, int n) { // 圆与简单多边形
PT o, a, b, t1, t2;
double sum=0, res, d1, d2, d3, sign;
o.x = o.y = 0;
p[n] = p[0];
for (int i=0; i < n; i++) {
a = p[i]-ct;
b = p[i+1]-ct;
sign = cp(a,b) > 0 ? 1 : -1;
d1 = a.x*a.x + a.y*a.y;
d2 = b.x*b.x + b.y*b.y;
d3 = sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
if (d1 < R*R+eps && d2 < R*R+eps) { //两个点都在圆内
res = fabs(cp(a, b));
}
else if (d1 < R*R-eps || d2 < R*R-eps) { //一个点在圆内
cir_line(o, R, a, b, t1, t2);
if ((a.x-t2.x)*(b.x-t2.x) < eps && (a.y-t2.y)*(b.y-t2.y) < eps) {
t1 = t2;
}
if (d1 < d2)
res = fabs(cp(a, t1)) + R*R*angle(b, t1);
else
res = fabs(cp(b, t1)) + R*R*angle(a, t1);
}
else if (fabs(cp(a, b))/d3 > R-eps) { // 两个点都在园外,且线段与圆之多只有一个交点
res = R*R*angle(a, b);
}
else { // 线段与圆有两个交点
cir_line(o, R, a, b, t1, t2);
if (multi(t1, a, b) > eps || multi(t2, a, b) > eps) { // a b 在圆的同一侧
res = R*R*angle(a, b);
}
else {
res = fabs(cp(t1, t2));
if (cross(t1, t2, a) < eps)
res += R*R*(angle(a, t1) + angle(b, t2));
else
res += R*R*(angle(a, t2) + angle(b, t1));
}
}
sum += res * sign;
}
return fabs(sum)/2.0;
}
double polyUnion( int n ) { //多边形面积并
double sum = 0;
for( int i = 0; i < n; ++i ) for( int ii = 0; ii < g[i].sz; ++ii ) {
int tot = 0;
c[tot++] = MP(0, 0);
c[tot++] = MP(1, 0);
for( int j = 0; j < n; ++j ) if( i != j ) for( int jj = 0; jj < g[j].sz; ++jj ) {
int d1 = dcmp(cross(g[i].p[ii+1] - g[i].p[ii], g[j].p[jj] - g[i].p[ii]));
int d2 = dcmp(cross(g[i].p[ii+1] - g[i].p[ii], g[j].p[jj+1] - g[i].p[ii]));
if( !d1 && !d2 ) {
point t1 = g[j].p[jj+1] - g[j].p[jj];
point t2 = g[i].p[ii+1] - g[i].p[ii];
if( dcmp( dot(t1, t2) ) > 0 && j < i ) {
c[tot++] = MP(segP(g[j].p[jj], g[i].p[ii], g[i].p[ii+1]), 1);
c[tot++] = MP(segP(g[j].p[jj+1], g[i].p[ii], g[i].p[ii+1]), -1);
}
}
else if( d1 >= 0 && d2 < 0 || d1 < 0 && d2 >= 0 ) {
double tc = cross(g[j].p[jj+1] - g[j].p[jj], g[i].p[ii] - g[j].p[jj]);
double td = cross(g[j].p[jj+1] - g[j].p[jj], g[i].p[ii+1] - g[j].p[jj]);
if( d2 < 0 )
c[tot++] = MP(tc / (tc - td), 1);
else c[tot++] = MP(tc / (tc - td), -1);
}
}
sort(c, c + tot);
double cur = min(max(c[0].first, 0.0), 1.0);
int sgn = c[0].second;
double s = 0;
for( int j = 1; j < tot; ++j ) {
double nxt = min(max(c[j].first, 0.0), 1.0);
if( !sgn ) s += nxt - cur;
sgn += c[j].second;
cur = nxt;
}
sum += cross(g[i].p[ii], g[i].p[ii+1]) * s;
}
return sum / 2;
}
bool judge(double mid) { //判断n+1个圆是否有交,R[n]=mid
double Left,Right;
for(int i = 0; i <= n; i++) {
if(i == 0) {
Left = p[i].x - R[i];
Right = p[i].x + R[i];
} else{
if(p[i].x-R[i] > Left) Left = p[i].x-R[i];
if(p[i].x+R[i] < Right) Right = p[i].x+R[i];
}
}
if(Left - Right > eps) return false;
int step = 50;
while(step--) {
double mid = (Left + Right)*0.5;
double low,high,uy,dy;
int low_id,high_id;
for(int i = 0; i <= n; i++) {
double d = sqrt(R[i]*R[i]-(p[i].x-mid)*(p[i].x-mid));
uy = p[i].y + d;
dy = p[i].y - d;
if(i == 0) {
low_id = high_id = 0;
low = dy; high = uy;
} else {
if(uy < high) high = uy,high_id = i;
if(dy > low) low = dy,low_id = i;
}
}
if(high - low > -eps) { return 1; }
PT a,b;
if(Cir_Cir(p[high_id],R[high_id],p[low_id],R[low_id],a,b)) {
if((a.x+b.x)*0.5 < mid) {
Right = mid;
} else Left = mid;
} else return false;
}
return false;
}
struct point{double x,y;};
struct line{point a,b;};
double distance(point p1,point p2){ return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); }
point intersection(line u,line v){
point ret=u.a;
double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))
/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
ret.x+=(u.b.x-u.a.x)*t;
ret.y+=(u.b.y-u.a.y)*t;
return ret;
}
point circumcenter(point a,point b,point c){//外心
line u,v;
u.a.x=(a.x+b.x)/2;
u.a.y=(a.y+b.y)/2;
u.b.x=u.a.x-a.y+b.y;
u.b.y=u.a.y+a.x-b.x;
v.a.x=(a.x+c.x)/2;
v.a.y=(a.y+c.y)/2;
v.b.x=v.a.x-a.y+c.y;
v.b.y=v.a.y+a.x-c.x;
return intersection(u,v);
}
point incenter(point a,point b,point c){//内心
line u,v;
double m,n;
u.a=a;
m=atan2(b.y-a.y,b.x-a.x);
n=atan2(c.y-a.y,c.x-a.x);
u.b.x=u.a.x+cos((m+n)/2);
u.b.y=u.a.y+sin((m+n)/2);
v.a=b;
m=atan2(a.y-b.y,a.x-b.x);
n=atan2(c.y-b.y,c.x-b.x);
v.b.x=v.a.x+cos((m+n)/2);
v.b.y=v.a.y+sin((m+n)/2);
return intersection(u,v);
}
point perpencenter(point a,point b,point c){//垂心
line u,v;
u.a=c;
u.b.x=u.a.x-a.y+b.y;
u.b.y=u.a.y+a.x-b.x;
v.a=b;
v.b.x=v.a.x-a.y+c.y;
v.b.y=v.a.y+a.x-c.x;
return intersection(u,v);
}
//重心
//到三角形三顶点距离的平方和最小的点
//三角形内到三边距离之积最大的点
point barycenter(point a,point b,point c){
line u,v;
u.a.x=(a.x+b.x)/2;
u.a.y=(a.y+b.y)/2;
u.b=c;
v.a.x=(a.x+c.x)/2;
v.a.y=(a.y+c.y)/2;
v.b=b;
return intersection(u,v);
}
//费马点
//到三角形三顶点距离之和最小的点
point fermentpoint(point a,point b,point c){
point u,v;
double step=fabs(a.x)+fabs(a.y)+fabs(b.x)+fabs(b.y)+fabs(c.x)+fabs(c.y);
int i,j,k;
u.x=(a.x+b.x+c.x)/3;
u.y=(a.y+b.y+c.y)/3;
while (step>1e-10)
for (k=0;k<10;step/=2,k++)
for (i=-1;i<=1;i++)
for (j=-1;j<=1;j++){
v.x=u.x+step*i;
v.y=u.y+step*j;
if (distance(u,a)+distance(u,b)+distance(u,c)>distance(v,a)+distance(v,b)+distance(v,c))
u=v;
}
return u;
}
double cir_area_inst(PT c1, double r1, PT c2, double r2) {
double a1, a2, d, ret;
d = sqrt((c1 - c2).dot(c1 - c2) );
if(sgn(d - r1 - r2) >= 0)
return 0;
if(sgn(d - r2 + r1) <= 0)
return pi * r1 * r1;
if(sgn(d - r1 + r2) <= 0)
return pi * r2 * r2;
a1 = acos((r1 * r1 + d * d - r2 * r2) / 2 / r1 / d);
a2 = acos((r2 * r2 + d * d - r1 * r1) / 2 / r2 / d);
ret = (a1 - 0.5 * sin(2 * a1)) * r1 * r1 + (a2 - 0.5 * sin(2 * a2)) * r2 * r2;
return ret;
}
PT gravity_center(PT* p, int n) { // 多边形重心
int i;
double A=0, a;
PT t;
t.x = t.y = 0;
p[n] = p[0];
for (i=0; i < n; i++) {
a = p[i].x*p[i+1].y - p[i+1].x*p[i].y;
t.x += (p[i].x + p[i+1].x) * a;
t.y += (p[i].y + p[i+1].y) * a;
A += a;
}
t.x /= A*3;
t.y /= A*3;
return t;
}
bool cmpag(PT a, PT b) {
double t = (a-GP).x*(b-GP).y - (b-GP).x*(a-GP).y;
return fabs(t) > eps ? t > 0 : PPdis(a, GP) < PPdis(b, GP);
}
void Grahamag(PT *p, int &n) { // 极角序
int i, top = 1;
GP = p[0];
for (i=1; i < n; i++) if(p[i].y<GP.y-eps || (fabs(p[i].y-GP.y)<eps && p[i].x<GP.x)) {
GP = p[i];
}
sort(p, p+n, cmpag);
for ( i=2; i < n; i++ ) {
while ( top > 0 && Cross(p[top], p[i], p[top-1]) < eps )
top--;
p[++top] = p[i];
}
p[++top] = p[0];
n = top;
}
/**半平面交***************************/
int ord[N], dq[N];
Seg sg[N];
double at2[N];
int cmp(int a, int b) {
if(sgn(at2[a]-at2[b]) != 0) {
return sgn(at2[a] - at2[b]) < 0;
}
return sgn((sg[a].e-sg[a].s)*(sg[b].e-sg[a].s)) < 0;
}
bool is_right(int a, int b, int c) {
PT t;
t = intersect(sg[a], sg[b]);
return sgn((sg[c].e-sg[c].s)*(t-sg[c].s)) < 0;
}
PT intersect(Seg s1, Seg s2) { return intersect(s1.s, s1.e-s1.s, s2.s, s2.e-s2.s); }
int HPI(int n, std::vector<PT>&p) {
int i, j, l=1, r=2;
for(i = 0; i < n; i++) {
at2[i] = atan2(sg[i].e.y-sg[i].s.y, sg[i].e.x-sg[i].s.x);
ord[i] = i;
}
std::sort(ord, ord + n, cmp);
for(i=j=1; i < n; i++) if(sgn(at2[ord[i]]-at2[ord[i-1]]) > 0) {
ord[j++] = ord[i];
}
n = j;
p.clear();
dq[l] = ord[0];dq[r] = ord[1];
for(i=2; i < n; i++) {
for(; l < r && is_right(dq[r-1],dq[r],ord[i]); r--) {
if(sgn(at2[ord[i]] - at2[dq[r-1]] - pi) >= 0)
return -1;
}
while(l < r && is_right(dq[l], dq[l+1], ord[i])) l++;
dq[++r] = ord[i];
}
while(l < r && is_right(dq[r-1], dq[r], dq[l])) r--;
while(l < r && is_right(dq[l], dq[l+1], dq[r])) l++;
dq[l-1] = dq[r]; p.resize(r-l+1);
for(int i = l; i <= r; i++) {
p[i-l]=intersect(sg[dq[i]], sg[dq[i-1]]);
}
return r - l + 1;
}
//返回两点所在大圆劣弧对应圆心角,0<=angle<=pi
double angle(double lng1,double lat1,double lng2,double lat2){//计算圆心角lat表示纬度,-90<=w<=90,lng表示经度
double dlng=fabs(lng1-lng2)*pi/180;
while (dlng>=pi+pi)
dlng-=pi+pi;
if (dlng>pi)
dlng=pi+pi-dlng;
lat1*=pi/180,lat2*=pi/180;
return acos(cos(lat1)*cos(lat2)*cos(dlng)+sin(lat1)*sin(lat2));
}
double line_dist(double r,double lng1,double lat1,double lng2,double lat2){//计算距离,r为球半径
double dlng=fabs(lng1-lng2)*pi/180;
while (dlng>=pi+pi)
dlng-=pi+pi;
if (dlng>pi)
dlng=pi+pi-dlng;
lat1*=pi/180,lat2*=pi/180;
return r*sqrt(2-2*(cos(lat1)*cos(lat2)*cos(dlng)+sin(lat1)*sin(lat2)));
}
inline double sphere_dist(double r,double lng1,double lat1,double lng2,double lat2){//计算球面距离,r为球半径
return r*angle(lng1,lat1,lng2,lat2);
}
#define zero(x) (((x)>0?(x):-(x))<eps)
struct point3{double x,y,z;};
struct line3{point3 a,b;};
struct plane3{point3 a,b,c;};
point3 xmult(point3 u,point3 v){//计算cross product U x V
point3 ret;
ret.x=u.y*v.z-v.y*u.z;
ret.y=u.z*v.x-u.x*v.z;
ret.z=u.x*v.y-u.y*v.x;
return ret;
}
double dmult(point3 u,point3 v){//计算dot product U . V
return u.x*v.x+u.y*v.y+u.z*v.z;
}
point3 subt(point3 u,point3 v){//矢量差 U - V
point3 ret;
ret.x=u.x-v.x;
ret.y=u.y-v.y;
ret.z=u.z-v.z;
return ret;
}
double volume(point3 a, point3 b, point3 c, point3 d) {//四面体体积
return fabs(dmult( (b - a) * (c - a) , (d - a) ) ) / 6.0;
}
point3 shade_ptoplane(point3 p, point3 a, point3 b, point3 c) {//点到平面的投影
point3 nor = (b - a) * (c - a);
point3 nor0 = ( nor * dmult(nor, p - a) ) / vlen(nor) / vlen(nor);
return (p - nor0);
}
point3 pvec(point3 s1,point3 s2,point3 s3){//取平面法向量
return xmult(subt(s1,s2),subt(s2,s3));
}
double distance(point3 p1,point3 p2){//两点距离,单参数取向量大小
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z));
}
double vlen(point3 p){//向量大小
return sqrt(p.x*p.x+p.y*p.y+p.z*p.z);
}
int dots_inline(point3 p1,point3 p2,point3 p3){//判三点共线
return vlen(xmult(subt(p1,p2),subt(p2,p3)))<eps;
}
int dots_onplane(point3 a,point3 b,point3 c,point3 d){//判四点共面
return zero(dmult(pvec(a,b,c),subt(d,a)));
}
int dot_online_in(point3 p,point3 l1,point3 l2){//判点是否在线段上,包括端点和共线
return zero(vlen(xmult(subt(p,l1),subt(p,l2))))&&(l1.x-p.x)*(l2.x-p.x)<eps&&
(l1.y-p.y)*(l2.y-p.y)<eps&&(l1.z-p.z)*(l2.z-p.z)<eps;
}
int dot_online_ex(point3 p,point3 l1,point3 l2){//判点是否在线段上,不包括端点
return dot_online_in(p,l1,l2)&&(!zero(p.x-l1.x)||!zero(p.y-l1.y)||!zero(p.z-l1.z))&&
(!zero(p.x-l2.x)||!zero(p.y-l2.y)||!zero(p.z-l2.z));
}
int dot_inplane_in(point3 p,point3 s1,point3 s2,point3 s3){//判点是否在空间三角形上,包括边界,三点共线无意义
return zero(vlen(xmult(subt(s1,s2),subt(s1,s3)))-vlen(xmult(subt(p,s1),subt(p,s2)))-
vlen(xmult(subt(p,s2),subt(p,s3)))-vlen(xmult(subt(p,s3),subt(p,s1))));
}
int dot_inplane_ex(point3 p,point3 s1,point3 s2,point3 s3){//判点是否在空间三角形上,不包括边界,三点共线无意义
return dot_inplane_in(p,s1,s2,s3)&&vlen(xmult(subt(p,s1),subt(p,s2)))>eps&&
vlen(xmult(subt(p,s2),subt(p,s3)))>eps&&vlen(xmult(subt(p,s3),subt(p,s1)))>eps;
}
int same_side(point3 p1,point3 p2,point3 l1,point3 l2){//判两点在线段同侧,点在线段上返回0,不共面无意义
return dmult(xmult(subt(l1,l2),subt(p1,l2)),xmult(subt(l1,l2),subt(p2,l2)))>eps;
}
int opposite_side(point3 p1,point3 p2,point3 l1,point3 l2){//判两点在线段异侧,点在线段上返回0,不共面无意义
return dmult(xmult(subt(l1,l2),subt(p1,l2)),xmult(subt(l1,l2),subt(p2,l2)))<-eps;
}
int same_side(point3 p1,point3 p2,point3 s1,point3 s2,point3 s3){//判两点在平面同侧,点在平面上返回0
return dmult(pvec(s1,s2,s3),subt(p1,s1))*dmult(pvec(s1,s2,s3),subt(p2,s1))>eps;
}
int opposite_side(point3 p1,point3 p2,point3 s1,point3 s2,point3 s3){//判两点在平面异侧,点在平面上返回0
return dmult(pvec(s1,s2,s3),subt(p1,s1))*dmult(pvec(s1,s2,s3),subt(p2,s1))<-eps;
}
int parallel(point3 u1,point3 u2,point3 v1,point3 v2){//判两直线平行
return vlen(xmult(subt(u1,u2),subt(v1,v2)))<eps;
}
int parallel(point3 u1,point3 u2,point3 u3,point3 v1,point3 v2,point3 v3){//判两平面平行
return vlen(xmult(pvec(u1,u2,u3),pvec(v1,v2,v3)))<eps;
}
int parallel(point3 l1,point3 l2,point3 s1,point3 s2,point3 s3){//判直线与平面平行
return zero(dmult(subt(l1,l2),pvec(s1,s2,s3)));
}
int perpendicular(point3 u1,point3 u2,point3 v1,point3 v2){//判两直线垂直
return zero(dmult(subt(u1,u2),subt(v1,v2)));
}
int perpendicular(point3 u1,point3 u2,point3 u3,point3 v1,point3 v2,point3 v3){//判两平面垂直
return zero(dmult(pvec(u1,u2,u3),pvec(v1,v2,v3)));
}
int perpendicular(point3 l1,point3 l2,point3 s1,point3 s2,point3 s3){//判直线与平面平行
return vlen(xmult(subt(l1,l2),pvec(s1,s2,s3)))<eps;
}
int intersect_in(point3 u1,point3 u2,point3 v1,point3 v2){//判两线段相交,包括端点和部分重合
if (!dots_onplane(u1,u2,v1,v2))
return 0;
if (!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2))
return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
}
int intersect_ex(point3 u1,point3 u2,point3 v1,point3 v2){//判两线段相交,不包括端点和部分重合
return dots_onplane(u1,u2,v1,v2)&&opposite_side(u1,u2,v1,v2)&&opposite_side(v1,v2,u1,u2);
}
int intersect_in(point3 l1,point3 l2,point3 s1,point3 s2,point3 s3){//判线段与空间三角形相交,包括交于边界和(部分)包含
return !same_side(l1,l2,s1,s2,s3)&&!same_side(s1,s2,l1,l2,s3)&&
!same_side(s2,s3,l1,l2,s1)&&!same_side(s3,s1,l1,l2,s2);
}
int intersect_ex(point3 l1,point3 l2,point3 s1,point3 s2,point3 s3){//判线段与空间三角形相交,不包括交于边界和(部分)包含
return opposite_side(l1,l2,s1,s2,s3)&&opposite_side(s1,s2,l1,l2,s3)&&
opposite_side(s2,s3,l1,l2,s1)&&opposite_side(s3,s1,l1,l2,s2);
}
point3 intersection(point3 u1,point3 u2,point3 v1,point3 v2){//计算两直线交点,注意事先判断直线是否共面和平行!
point3 ret=u1;
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t;
ret.z+=(u2.z-u1.z)*t;
return ret;
}
point3 intersection(point3 l1,point3 l2,point3 s1,point3 s2,point3 s3){//计算直线与平面交点,注意事先判断是否平行,并保证三点不共线!
point3 ret=pvec(s1,s2,s3);
double t=(ret.x*(s1.x-l1.x)+ret.y*(s1.y-l1.y)+ret.z*(s1.z-l1.z))/
(ret.x*(l2.x-l1.x)+ret.y*(l2.y-l1.y)+ret.z*(l2.z-l1.z));
ret.x=l1.x+(l2.x-l1.x)*t;
ret.y=l1.y+(l2.y-l1.y)*t;
ret.z=l1.z+(l2.z-l1.z)*t;
return ret;
}
line3 intersection(point3 u1,point3 u2,point3 u3,point3 v1,point3 v2,point3 v3){//计算两平面交线,注意事先判断是否平行,并保证三点不共线!
line3 ret;
ret.a=parallel(v1,v2,u1,u2,u3)?intersection(v2,v3,u1,u2,u3):intersection(v1,v2,u1,u2,u3);
ret.b=parallel(v3,v1,u1,u2,u3)?intersection(v2,v3,u1,u2,u3):intersection(v3,v1,u1,u2,u3);
return ret;
}
double ptoline(point3 p,point3 l1,point3 l2){//点到直线距离
return vlen(xmult(subt(p,l1),subt(l2,l1)))/distance(l1,l2);
}
double ptoplane(point3 p,point3 s1,point3 s2,point3 s3){//点到平面距离
return fabs(dmult(pvec(s1,s2,s3),subt(p,s1)))/vlen(pvec(s1,s2,s3));
}
double linetoline(point3 u1,point3 u2,point3 v1,point3 v2){//直线到直线距离
point3 n=xmult(subt(u1,u2),subt(v1,v2));
return fabs(dmult(subt(u1,v1),n))/vlen(n);
}
double angle_cos(point3 u1,point3 u2,point3 v1,point3 v2){//两直线夹角cos值
return dmult(subt(u1,u2),subt(v1,v2))/vlen(subt(u1,u2))/vlen(subt(v1,v2));
}
double angle_cos(point3 u1,point3 u2,point3 u3,point3 v1,point3 v2,point3 v3){//两平面夹角cos值
return dmult(pvec(u1,u2,u3),pvec(v1,v2,v3))/vlen(pvec(u1,u2,u3))/vlen(pvec(v1,v2,v3));
}
double angle_sin(point3 l1,point3 l2,point3 s1,point3 s2,point3 s3){//直线平面夹角sin值
return dmult(subt(l1,l2),pvec(s1,s2,s3))/vlen(subt(l1,l2))/vlen(pvec(s1,s2,s3));
}
int grid_onedge(int n,point* p){//多边形上的网格点个数
int i,ret=0;
for (i=0;i<n;i++)
ret+=gcd(abs(p[i].x-p[(i+1)%n].x),abs(p[i].y-p[(i+1)%n].y));
return ret;
}
int grid_inside(int n,point* p){//多边形内的网格点个数
int i,ret=0;
for (i=0;i<n;i++)
ret+=p[(i+1)%n].y*(p[i].x-p[(i+2)%n].x);
return (abs(ret)-grid_onedge(n,p))/2+1;
}
struct face { int a,b,c; bool ok; };
struct CH3D {
static const int MAXN = 55;
int n;//初始顶点数
PT P[MAXN];//初始顶点
int num;//凸包表面的三角形个数
face F[8*MAXN];//凸包表面的三角形
int g[MAXN][MAXN];
double vol(PT &p, face &f) {
PT m=P[f.b]-P[f.a];
PT n=P[f.c]-P[f.a];
PT t=p-P[f.a];
return (m*n).dot(t);
}
void deal(int p,int a,int b) {
int f=g[a][b];
face add;
if(F[f].ok) {
if(sgn(vol(P[p],F[f])) > 0) {
dfs(p,f);
} else {
add.a=b; add.b=a; add.c=p; add.ok=true;
g[p][b]=g[a][p]=g[b][a]=num;
F[num++]=add;
}
}
}
void dfs(int p,int now) {
F[now].ok=false;
deal(p,F[now].b,F[now].a);
deal(p,F[now].c,F[now].b);
deal(p,F[now].a,F[now].c);
}
bool same(int s,int t) {
PT a=P[F[s].a];
PT b=P[F[s].b];
PT c=P[F[s].c];
return sgn(volume(a,b,c,P[F[t].a])) == 0 &&
sgn(volume(a,b,c,P[F[t].b])) == 0 &&
sgn(volume(a,b,c,P[F[t].c])) == 0;
}
void create() {
int i,j,tmp;
face add;
num=0;
if(n<4)return;
bool flag=true;
for(i=1;i<n;i++) {
if(sgn((P[0]-P[i]).vlen()) > 0) {
std::swap(P[1],P[i]);
flag=false;
break;
}
}
if(flag)return;
flag=true;
for(i=2;i<n;i++) {
if(sgn(area(P[0], P[1], P[i])) > 0) {
std::swap(P[2],P[i]);
flag=false;
break;
}
}
if(flag)return;
flag=true;
for(i=3;i<n;i++) {
if(sgn(fabs(volume(P[0], P[1], P[2], P[i]))) > 0) {
std::swap(P[3],P[i]);
flag=false;
break;
}
}
if(flag)return;
for(i=0;i<4;i++) {
add.a=(i+1)%4;
add.b=(i+2)%4;
add.c=(i+3)%4;
add.ok=true;
if(sgn(vol(P[i], add)) > 0) {
std::swap(add.b,add.c);
}
g[add.a][add.b]=g[add.b][add.c]=g[add.c][add.a]=num;
F[num++]=add;
}
for(i=4;i<n;i++) {
for(j=0;j<num;j++) {
if(F[j].ok && sgn(vol(P[i],F[j])) > 0) {
dfs(i,j);
break;
}
}
}
tmp=num;
for(i=num=0;i<tmp;i++)
if(F[i].ok)
F[num++]=F[i];
}
double ptoface(PT p,int i) {
return fabs(volume(P[F[i].a],P[F[i].b],P[F[i].c],p)/((P[F[i].b]-P[F[i].a])*(P[F[i].c]-P[F[i].a])).vlen());
}
}hull;
void rotate_to_horizontal(int n, PT *p, PT v) {//旋转点集,使法向量为v的平面水平
if(sgn(v.x)==0 && sgn(v.y)==0) { return ; }
double a, c, s;
a = atan2(v.y, v.x);
c = cos(a), s = sin(a);
for(int i = 0; i < n; i++) {
PT t = p[i];
p[i].x = t.x * c + t.y * s;
p[i].y = t.y * c - t.x * s;
}
a = atan2(sqrt(v.x*v.x+v.y*v.y), v.z);
c = cos(a), s = sin(a);
for(int i = 0; i < n; i++) {
PT t = p[i];
p[i].z = t.z * c + t.x * s;
p[i].x = t.x * c - t.z * s;
}
}
bool input(){
scanf("%d", &n);
if(n == 0) return false;
hull.n = n;
for(int i = 0; i < n; i++) { hull.P[i].in(); }
double ans_h = 0, ans_area = 1e30;
hull.create();
for(int i = 0; i < hull.num; i++) {
for(int j = 0; j < n; j++) {
p[j] = hull.P[j];
}
PT v = norm(p[hull.F[i].b], p[hull.F[i].a], p[hull.F[i].c]);
rotate_to_horizontal(n, p, v);
double z = p[hull.F[i].a].z;
for(int j = 0; j < n; j++) {
p[j].z -= z;
}
double H = 0;
for(int j = 0; j < n; j++) {
H = std::max(H, hull.ptoface(hull.P[j], i));
}
convex.init();
for(int j = 0; j < n; j++) {
convex.push_back(p[j]);
}
convex.gao();
double S = convex.get_area();
if(sgn(H - ans_h) > 0 || sgn(H - ans_h)==0 && sgn(ans_area-S) > 0) {
ans_h = H;
ans_area = S;
}
}
printf("%.3f %.3f\n", ans_h, ans_area);
return true;
}
/*******directed_mst***/
struct Edge {
int u, v;
int cost;
}edge[M];
int pre[N], id[M], f[N], e;
item in[N];
void add_edge(int a, int b, int cost) { edge[e].u = a; edge[e].v = b; edge[e].cost = cost; e++; }
item directed_mst(int root, int n, int m) {
item ret = 0;
int u, v;
while(1) {
std::fill(in, in + n, inf);
for(int i = 0; i < m; i++) {
u = edge[i].u, v = edge[i].v;
if(edge[i].cost < in[v] && u != v) {
pre[v] = u;
in[v] = edge[i].cost;
}
}
for(int i = 0; i < n; i++) {
if(i != root && in[i] == inf) {
return -1;
}
}
int cirs = 0;
std::fill(id, id + n, -1);
std::fill(f, f + n, -1);
in[root] = 0;
for(int i = 0; i < n; i++) {
ret += in[i];
int v = i;
while(f[v] != i && id[v] == -1 && v != root) {
f[v] = i;
v = pre[v];
}
if(v != root && id[v] == -1) {
for(int u = pre[v]; u != v; u = pre[u]) {
id[u] = cirs;
}
id[v] = cirs++;
}
}
if(cirs == 0) { break; }
for(int i = 0; i < n; i++) {
if(id[i] == -1) {
id[i] = cirs++;
}
}
for(int i = 0; i < m; i++) {
v = edge[i].v;
edge[i].u = id[edge[i].u];
edge[i].v = id[edge[i].v];
if(edge[i].u != edge[i].v) {
edge[i].cost -= in[v];
}
}
n = cirs;
root = id[root];
}
return ret;
}
/**********一般图匹配***************/
#define SET(a,b) memset(a,b,sizeof(a))
deque<int> Q;
//g[i][j]存放关系图:i,j是否有边,match[i]存放i所匹配的点
bool g[MAXN][MAXN],inque[MAXN],inblossom[MAXN];
int match[MAXN],pre[MAXN],base[MAXN];
int findancestor(int u,int v) {//找公共祖先
bool inpath[MAXN]={false};
while(1) {
u=base[u];
inpath[u]=true;
if(match[u]==-1)break;
u=pre[match[u]];
}
while(1) {
v=base[v];
if(inpath[v])return v;
v=pre[match[v]];
}
}
void reset(int u,int anc) {//压缩花
while(u!=anc) {
int v=match[u];
inblossom[base[u]]=1;
inblossom[base[v]]=1;
v=pre[v];
if(base[v]!=anc)pre[v]=match[u];
u=v;
}
}
void contract(int u,int v,int n) {
int anc=findancestor(u,v);
SET(inblossom,0);
reset(u,anc);reset(v,anc);
if(base[u]!=anc)pre[u]=v;
if(base[v]!=anc)pre[v]=u;
for(int i=1;i<=n;i++)
if(inblossom[base[i]]) {
base[i]=anc;
if(!inque[i]) {
Q.push_back(i);
inque[i]=1;
}
}
}
bool dfs(int S,int n) {
for(int i=0;i<=n;i++)pre[i]=-1,inque[i]=0,base[i]=i;
Q.clear();Q.push_back(S);inque[S]=1;
while(!Q.empty()) {
int u=Q.front();Q.pop_front();
for(int v=1;v<=n;v++) {
if(g[u][v]&&base[v]!=base[u]&&match[u]!=v) {
if(v==S||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n);
else if(pre[v]==-1) {
pre[v]=u;
if(match[v]!=-1)Q.push_back(match[v]),inque[match[v]]=1;
else {
u=v;
while(u!=-1) {
v=pre[u];
int w=match[v];
match[u]=v;
match[v]=u;
u=w;
}
return true;
} } } } } return false;
}
int solve(int n) {
SET(match,-1);
int ans=0;
for(int i=1;i<=n;i++)
if(match[i]==-1&&dfs(i,n))
ans++;
return ans;
}
/*********max_flow********************/
template<class T>
struct Max_Flow {
int s, t, n;
int Q[N], sign;
int head[N], level[N], cur[N], pre[N];
int nxt[M], pnt[M], E;
T cap[M];