Hex Artifact Content
Not logged in

Artifact d88a2122970afda17ee85e9cfa0b0d50bdaf4cfc:


0000: 2f 2f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  //--------------
0010: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0020: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0030: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
0040: 2f 2f 20 63 63 77 0a 2f 2f 0a 2f 2f 20 56 65 72  // ccw.//.// Ver
0050: 69 66 69 65 64 20 62 79 0a 2f 2f 20 20 20 2d 20  ified by.//   - 
0060: 53 52 4d 20 34 39 32 20 44 69 76 31 20 4c 56 31  SRM 492 Div1 LV1
0070: 0a 2f 2f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .//-------------
0080: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0090: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
00a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
00b0: 0a 0a 64 6f 75 62 6c 65 20 6f 75 74 65 72 5f 70  ..double outer_p
00c0: 72 6f 64 28 63 6f 6e 73 74 20 43 4d 50 26 20 61  rod(const CMP& a
00d0: 2c 20 63 6f 6e 73 74 20 43 4d 50 26 20 62 29 20  , const CMP& b) 
00e0: 7b 20 72 65 74 75 72 6e 20 69 6d 61 67 28 63 6f  { return imag(co
00f0: 6e 6a 28 61 29 2a 62 29 3b 20 7d 0a 64 6f 75 62  nj(a)*b); }.doub
0100: 6c 65 20 69 6e 6e 65 72 5f 70 72 6f 64 28 63 6f  le inner_prod(co
0110: 6e 73 74 20 43 4d 50 26 20 61 2c 20 63 6f 6e 73  nst CMP& a, cons
0120: 74 20 43 4d 50 26 20 62 29 20 7b 20 72 65 74 75  t CMP& b) { retu
0130: 72 6e 20 72 65 61 6c 28 63 6f 6e 6a 28 61 29 2a  rn real(conj(a)*
0140: 62 29 3b 20 7d 0a 0a 69 6e 74 20 63 63 77 28 63  b); }..int ccw(c
0150: 6f 6e 73 74 20 43 4d 50 26 20 61 2c 20 43 4d 50  onst CMP& a, CMP
0160: 20 62 2c 20 43 4d 50 20 63 29 20 7b 0a 09 62 20   b, CMP c) {..b 
0170: 2d 3d 20 61 3b 20 63 20 2d 3d 20 61 3b 0a 09 69  -= a; c -= a;..i
0180: 66 28 20 6f 75 74 65 72 5f 70 72 6f 64 28 62 2c  f( outer_prod(b,
0190: 63 29 20 3e 20 30 20 29 20 72 65 74 75 72 6e 20  c) > 0 ) return 
01a0: 2b 31 3b 20 2f 2f 20 63 6f 75 6e 74 65 72 20 63  +1; // counter c
01b0: 6c 6f 63 6b 77 69 73 65 0a 09 69 66 28 20 6f 75  lockwise..if( ou
01c0: 74 65 72 5f 70 72 6f 64 28 62 2c 63 29 20 3c 20  ter_prod(b,c) < 
01d0: 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 20 2f  0 ) return -1; /
01e0: 2f 20 63 6c 6f 63 6b 77 69 73 65 0a 09 69 66 28  / clockwise..if(
01f0: 20 69 6e 6e 65 72 5f 70 72 6f 64 28 62 2c 63 29   inner_prod(b,c)
0200: 20 3c 20 30 20 29 20 72 65 74 75 72 6e 20 2b 32   < 0 ) return +2
0210: 3b 20 2f 2f 20 63 2d 2d 5b 61 2d 2d 62 5d 20 6f  ; // c--[a--b] o
0220: 6e 20 6c 69 6e 65 0a 09 69 66 28 20 6e 6f 72 6d  n line..if( norm
0230: 28 62 29 20 3c 20 6e 6f 72 6d 28 63 29 20 29 20  (b) < norm(c) ) 
0240: 20 20 72 65 74 75 72 6e 20 2d 32 3b 20 2f 2f 20    return -2; // 
0250: 5b 61 2d 2d 62 5d 2d 2d 63 20 6f 6e 20 6c 69 6e  [a--b]--c on lin
0260: 65 0a 09 72 65 74 75 72 6e 20 30 3b 20 2f 2f 20  e..return 0; // 
0270: 5b 61 2d 2d 63 2d 2d 62 5d 20 6f 6e 20 6c 69 6e  [a--c--b] on lin
0280: 65 0a 7d 0a 0a 0a 2f 2f 20 69 6e 74 65 72 73 65  e.}...// interse
0290: 63 74 69 6f 6e 20 6f 66 20 74 77 6f 20 6c 69 6e  ction of two lin
02a0: 65 20 73 65 67 6d 65 6e 74 73 2e 0a 62 6f 6f 6c  e segments..bool
02b0: 20 63 72 6f 73 73 28 43 4d 50 20 70 31 2c 20 43   cross(CMP p1, C
02c0: 4d 50 20 70 32 2c 20 43 4d 50 20 50 31 2c 20 43  MP p2, CMP P1, C
02d0: 4d 50 20 50 32 29 20 7b 0a 09 72 65 74 75 72 6e  MP P2) {..return
02e0: 20 63 63 77 28 70 31 2c 70 32 2c 50 31 29 2a 63   ccw(p1,p2,P1)*c
02f0: 63 77 28 70 31 2c 70 32 2c 50 32 29 3c 3d 30 20  cw(p1,p2,P2)<=0 
0300: 26 26 20 63 63 77 28 50 31 2c 50 32 2c 70 31 29  && ccw(P1,P2,p1)
0310: 2a 63 63 77 28 50 31 2c 50 32 2c 70 32 29 3c 3d  *ccw(P1,P2,p2)<=
0320: 30 3b 0a 7d 0a 0a 0a 2f 2f 20 49 6e 74 65 72 73  0;.}...// Inters
0330: 65 63 74 69 6f 6e 20 6f 66 20 74 77 6f 20 6c 69  ection of two li
0340: 6e 65 20 73 65 67 6d 65 6e 74 73 2c 20 62 79 20  ne segments, by 
0350: 70 31 2b 28 2a 70 6f 73 29 28 70 32 2d 70 31 29  p1+(*pos)(p2-p1)
0360: 20 77 68 65 72 65 20 30 3c 3d 2a 70 6f 73 3c 3d   where 0<=*pos<=
0370: 31 2e 0a 2f 2f 20 6f 6e 6c 79 20 74 65 73 74 65  1..// only teste
0380: 64 20 62 79 20 6c 6f 63 61 6c 20 73 69 6d 70 6c  d by local simpl
0390: 65 20 75 6e 69 74 74 65 73 74 2e 20 62 65 20 63  e unittest. be c
03a0: 61 72 65 66 75 6c 2e 0a 74 65 6d 70 6c 61 74 65  areful..template
03b0: 3c 74 79 70 65 6e 61 6d 65 20 54 3e 0a 62 6f 6f  <typename T>.boo
03c0: 6c 20 63 72 6f 73 73 28 63 6f 6e 73 74 20 73 74  l cross(const st
03d0: 64 3a 3a 63 6f 6d 70 6c 65 78 3c 54 3e 26 20 70  d::complex<T>& p
03e0: 31 2c 20 63 6f 6e 73 74 20 73 74 64 3a 3a 63 6f  1, const std::co
03f0: 6d 70 6c 65 78 3c 54 3e 26 20 70 32 2c 0a 20 20  mplex<T>& p2,.  
0400: 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 73           const s
0410: 74 64 3a 3a 63 6f 6d 70 6c 65 78 3c 54 3e 26 20  td::complex<T>& 
0420: 50 31 2c 20 63 6f 6e 73 74 20 73 74 64 3a 3a 63  P1, const std::c
0430: 6f 6d 70 6c 65 78 3c 54 3e 26 20 50 32 2c 20 54  omplex<T>& P2, T
0440: 2a 20 70 6f 73 29 20 7b 0a 09 69 66 28 20 63 63  * pos) {..if( cc
0450: 77 28 70 31 2c 70 32 2c 50 31 29 2a 63 63 77 28  w(p1,p2,P1)*ccw(
0460: 70 31 2c 70 32 2c 50 32 29 3c 3d 30 20 26 26 20  p1,p2,P2)<=0 && 
0470: 63 63 77 28 50 31 2c 50 32 2c 70 31 29 2a 63 63  ccw(P1,P2,p1)*cc
0480: 77 28 50 31 2c 50 32 2c 70 32 29 3c 3d 30 20 29  w(P1,P2,p2)<=0 )
0490: 20 7b 0a 09 09 69 66 28 6f 75 74 65 72 5f 70 72   {...if(outer_pr
04a0: 6f 64 28 70 32 2d 70 31 2c 20 50 32 2d 50 31 29  od(p2-p1, P2-P1)
04b0: 20 3d 3d 20 30 29 0a 09 09 09 72 65 74 75 72 6e   == 0)....return
04c0: 20 66 61 6c 73 65 3b 20 2f 2f 20 70 61 72 61 6c   false; // paral
04d0: 6c 65 6c 0a 09 09 2a 70 6f 73 20 3d 20 6f 75 74  lel...*pos = out
04e0: 65 72 5f 70 72 6f 64 28 50 31 2d 70 31 2c 20 50  er_prod(P1-p1, P
04f0: 32 2d 50 31 29 20 2f 20 6f 75 74 65 72 5f 70 72  2-P1) / outer_pr
0500: 6f 64 28 70 32 2d 70 31 2c 20 50 32 2d 50 31 29  od(p2-p1, P2-P1)
0510: 3b 0a 09 09 72 65 74 75 72 6e 20 74 72 75 65 3b  ;...return true;
0520: 0a 09 7d 0a 09 72 65 74 75 72 6e 20 66 61 6c 73  ..}..return fals
0530: 65 3b 0a 7d 0a                                   e;.}.