Random increment algorithm (a randomized incremental algorithm)
#defineSQR (x) ((x) * (x))#defineEPS 1e-4structp{Doublex, y; P (DoubleXDoubley): x (x), Y (y) {} p (p&a, P &b): X (b.x-a.x), Y (b.y-a.y) {} p () {} p mid (P&a) { returnP ((a.x+x)/2, (a.y+y)/2); } DoubleCross (P &a) { returnx*a.y-y*a.x; } Doublelen2 () {returnSQR (x) +Sqr (y); } DoubleDis (P &a) { returnsqrt (SQR (x-a.x) +sqr (ya.y)); } voidprint () {printf ("%f%f\n", x, y); }};structdisc{P o; DoubleR; BOOLCover (P &a) { returnR-o.dis (a) >=-EPS; } Disc () {} Disc (P&o,Doubler): O (O), R (r) {} Disc (P&a, P &b): O (a.mid (b)), R (A.dis (b)/2) {} Disc (P&a, P &b, P &c) { DoubleT1=b.len2 ()-A.len2 (); DoubleT2=c.len2 ()-A.len2 (); P P1 (A, B), p2 (A, c); DoubleT3=p1.cross (p2) *2; P P3 (T1, p1.y), P4 (T2, P2.Y); P P5 (p1.x, T1), P6 (p2.x, T2); o=p (P3.cross (P4)/t3, P5.cross (P6)/T3); R=O.dis (a); }};D ISC Mindisc (vector<P> &p) { if(P.size () <=1)returnDisc (); Random_shuffle (P.begin (), P.end ()); Disc D (p[0], p[1]); for(intI=2; I<p.size (); i++) if(!D.cover (P[i])) {D=disc (p[0], p[i]); for(intj=1; j<i; J + +) if(!D.cover (P[j])) {D=Disc (P[i], p[j]); for(intk=0; k<j; k++) if(!D.cover (P[k]) d=Disc (P[i], p[j], p[k]); } } returnD;}
Minimum circle coverage (smallest enclosing discs)