// File: Hybryd[P]HyRoadGenerator/Polygon.cpp
// Function: Polygon::split()
std::pair< Polygon,Polygon > Polygon::split()
{
  std::pair<int,Point> pair = randPoint();
  int ed=pair.first; // indice de l'arete contenant le point aleatoire
  Point p=pair.second; // le point aleatoire
  Point v(poly[ed].second.x()-poly[ed].first.x(), poly[ed].second.y()-poly[ed].first.y()); // vecteur arete
  double c=-(v.x()*p.x() + v.y()*p.y()); // le c de l'equation de la droite ax + bx + c =0
  int i=0;
  bool bb=true;
  while (i<size() && bb)
  {
    if(i!=ed)
    {
      // on regarde si pour chaque arete les deux points sont du meme coté de la droite, ie meme signe
      bb = (poly[i].first.x()*v.x() + poly[i].first.y()*v.y() + c)*(poly[i].second.x()*v.x() + poly[i].second.y()*v.y() + c) > 0;
    }
    if(bb)
    {
      ++i;
    }
  }
  double denom=(v.x()*(poly[i].second.x()-poly[i].first.x()) + v.y()*(poly[i].second.y()-poly[i].first.y()));
  double t=0;
  if(denom != 0)
  {
    t=-(v.x()*poly[i].first.x() + v.y()*poly[i].first.y() + c)/denom;
  }
  else
  {
    std::cerr << " PROBLEME DENOMINATEUR NUL " << std::endl; // impossible to get here
  }
  
  
  Point intersec(poly[i].first.x() + t*(poly[i].second.x() - poly[i].first.x() ), poly[i].first.y() + t*(poly[i].second.y() - poly[i].first.y() )  );
  
  
  Polygon p1,p2;
  
  // split de l'arete de depart
  std::pair< Point,Point > ed1(p,poly[ed].second);
  std::pair< Point,Point > ed2(poly[ed].first,p);
  
  // split de l'arete d'intersection
  std::pair< Point,Point > edd1(poly[i].first,intersec);
  std::pair< Point,Point > edd2(intersec,poly[i].second);
  
  // creation du segment [intersec,p] (a inverser pour p2)
  std::pair< Point,Point > pont(intersec,p);
  
  p1.push_back(ed1);
  
  int k=ed;
  if(k==size()-1)
    k=0;
  else
    ++k;
  
  while(k<size() && k != i)
  {
    p1.push_back(poly[k]);
    ++k;
  }
  if(k==size())
  {
    k=0;
    while(k<size() && k != i)
    {
      p1.push_back(poly[k]);
      ++k;
    }
  }
  // on est arrivé en i
  p1.push_back(edd1);
  p1.push_back(pont); // p1 est defini
  

  // on commence p2
  if(k==size()-1)
    k=0;
  else
    ++k;
  
  p2.push_back(edd2);
  while(k<size() && k != ed)
  {
    p2.push_back(poly[k]);
    ++k;
  }
  if(k==size())
  {
    k=0;
    while(k<size() && k != ed)
    {
      p2.push_back(poly[k]);
      ++k;
    }
  }
  // on arrive en ed
  p2.push_back(ed2);
  // on inverse pont
  std::pair< Point,Point > tnop(pont.second,pont.first);
  p2.push_back(tnop);
  
  return std::pair< Polygon,Polygon >(p1,p2);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x130(%rbp)
movq %rdi, %rax
movq %rax, -0x128(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0x118(%rbp)
leaq -0x1c(%rbp), %rdi
callq 0x1ef0
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
leaq -0x1c(%rbp), %rsi
addq $0x4, %rsi
leaq -0x28(%rbp), %rdi
callq 0x2f30
movq -0x118(%rbp), %rdi
movslq -0x20(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13b0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x11c(%rbp)
movslq -0x20(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13b0
movq -0x118(%rbp), %rdi
movaps %xmm0, %xmm1
movss -0x11c(%rbp), %xmm0
subss %xmm1, %xmm0
movss %xmm0, -0x108(%rbp)
movslq -0x20(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13d0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x10c(%rbp)
movslq -0x20(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13d0
movss -0x10c(%rbp), %xmm1
movaps %xmm0, %xmm2
movss -0x108(%rbp), %xmm0
subss %xmm2, %xmm1
leaq -0x30(%rbp), %rdi
callq 0x2e80
leaq -0x30(%rbp), %rdi
callq 0x13b0
movss %xmm0, -0xfc(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x13b0
movss %xmm0, -0x100(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x13d0
movss %xmm0, -0x104(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x13d0
movss -0x104(%rbp), %xmm1
movss -0x100(%rbp), %xmm2
movaps %xmm0, %xmm3
movss -0xfc(%rbp), %xmm0
mulss %xmm3, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movb $0x1, -0x3d(%rbp)
movq -0x118(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl %eax, -0x138(%rbp)
callq 0x2d10
movl -0x138(%rbp), %ecx
movl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x131(%rbp)
jge 0x2244
movb -0x3d(%rbp), %al
movb %al, -0x131(%rbp)
movb -0x131(%rbp), %al
testb $0x1, %al
jne 0x2253
jmp 0x23b2
movl -0x3c(%rbp), %eax
cmpl -0x20(%rbp), %eax
je 0x239e
movq -0x118(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13b0
movss %xmm0, -0x150(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x13b0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x154(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13d0
movss %xmm0, -0x158(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x13d0
movss -0x158(%rbp), %xmm1
movss -0x154(%rbp), %xmm2
movq -0x118(%rbp), %rdi
movaps %xmm0, %xmm3
movss -0x150(%rbp), %xmm0
mulss %xmm3, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x140(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13b0
movss %xmm0, -0x148(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x13b0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x144(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13d0
movss %xmm0, -0x14c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x13d0
movss -0x14c(%rbp), %xmm2
movss -0x148(%rbp), %xmm1
movss -0x144(%rbp), %xmm3
movaps %xmm0, %xmm4
movsd -0x140(%rbp), %xmm0
mulss %xmm4, %xmm2
mulss %xmm3, %xmm1
addss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
addsd -0x38(%rbp), %xmm1
mulsd %xmm1, %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
seta %al
andb $0x1, %al
movb %al, -0x3d(%rbp)
testb $0x1, -0x3d(%rbp)
je 0x23ad
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2212
leaq -0x30(%rbp), %rdi
callq 0x13b0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x15c(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13b0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x16c(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13b0
movaps %xmm0, %xmm1
movss -0x16c(%rbp), %xmm0
subss %xmm1, %xmm0
movss %xmm0, -0x160(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x13d0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x164(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13d0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x168(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13d0
movss -0x168(%rbp), %xmm3
movss -0x164(%rbp), %xmm1
movss -0x160(%rbp), %xmm2
movaps %xmm0, %xmm4
movss -0x15c(%rbp), %xmm0
subss %xmm4, %xmm3
mulss %xmm3, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x48(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x24bc
jp 0x24bc
jmp 0x2569
leaq -0x30(%rbp), %rdi
callq 0x13b0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x170(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13b0
movss %xmm0, -0x174(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x13d0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x178(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13d0
movss -0x178(%rbp), %xmm1
movss -0x174(%rbp), %xmm2
movaps %xmm0, %xmm3
movss -0x170(%rbp), %xmm0
mulss %xmm3, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x38(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
divsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
jmp 0x258b
movq 0xaa80(%rip), %rdi      # 0xcff0
leaq 0x5ab9(%rip), %rsi      # 0x8030
callq 0x10f0
movq %rax, %rdi
movq 0xaa3a(%rip), %rsi      # 0xcfc0
callq 0x1120
movq -0x118(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13b0
movq -0x118(%rbp), %rdi
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x1a8(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x1a0(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13b0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x1ac(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13b0
movss -0x1ac(%rbp), %xmm2
movsd -0x1a8(%rbp), %xmm1
movq -0x118(%rbp), %rdi
movaps %xmm0, %xmm3
movsd -0x1a0(%rbp), %xmm0
subss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x17c(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13d0
movq -0x118(%rbp), %rdi
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x188(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x190(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13d0
movq -0x118(%rbp), %rdi
movss %xmm0, -0x194(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdi
callq 0x13d0
movss -0x194(%rbp), %xmm3
movsd -0x190(%rbp), %xmm1
movsd -0x188(%rbp), %xmm2
movaps %xmm0, %xmm4
movss -0x17c(%rbp), %xmm0
subss %xmm4, %xmm3
cvtss2sd %xmm3, %xmm3
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
cvtsd2ss %xmm1, %xmm1
leaq -0x58(%rbp), %rdi
callq 0x2e80
leaq -0x70(%rbp), %rdi
callq 0x13f0
leaq -0x88(%rbp), %rdi
callq 0x13f0
jmp 0x26f3
movq -0x118(%rbp), %rdi
movslq -0x20(%rbp), %rsi
callq 0x2e10
movq %rax, %rdx
addq $0x8, %rdx
leaq -0xa4(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2f60
jmp 0x271c
movq -0x118(%rbp), %rdi
movslq -0x20(%rbp), %rsi
callq 0x2e10
movq %rax, %rsi
leaq -0xb4(%rbp), %rdi
leaq -0x28(%rbp), %rdx
callq 0x2f60
jmp 0x2741
movq -0x118(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rsi
leaq -0xc4(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x2f60
jmp 0x2766
movq -0x118(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
callq 0x2e10
movq %rax, %rdx
addq $0x8, %rdx
leaq -0xd4(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x2f60
jmp 0x278f
leaq -0xe4(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x28(%rbp), %rdx
callq 0x2f60
jmp 0x27a5
leaq -0x70(%rbp), %rdi
leaq -0xa4(%rbp), %rsi
callq 0x2fa0
jmp 0x27b7
movq -0x118(%rbp), %rdi
movl -0x20(%rbp), %eax
movl %eax, -0xe8(%rbp)
movl -0xe8(%rbp), %eax
movl %eax, -0x1b4(%rbp)
callq 0x2d10
movl %eax, -0x1b0(%rbp)
jmp 0x27e0
movl -0x1b4(%rbp), %eax
movl -0x1b0(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2839
movl $0x0, -0xe8(%rbp)
jmp 0x2848
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x2c09
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x3030
jmp 0x2c09
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x284a
movq -0x118(%rbp), %rdi
movl -0xe8(%rbp), %eax
movl %eax, -0x1bc(%rbp)
callq 0x2d10
movl %eax, -0x1b8(%rbp)
jmp 0x286a
movl -0x1bc(%rbp), %ecx
movl -0x1b8(%rbp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x1bd(%rbp)
jge 0x2894
movl -0xe8(%rbp), %eax
cmpl -0x3c(%rbp), %eax
setne %al
movb %al, -0x1bd(%rbp)
movb -0x1bd(%rbp), %al
testb $0x1, %al
jne 0x28a0
jmp 0x28d5
movq -0x118(%rbp), %rdi
movslq -0xe8(%rbp), %rsi
callq 0x2e10
movq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0x2fa0
jmp 0x28c1
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x284a
movq -0x118(%rbp), %rdi
movl -0xe8(%rbp), %eax
movl %eax, -0x1c8(%rbp)
callq 0x2d10
movl %eax, -0x1c4(%rbp)
jmp 0x28f5
movl -0x1c8(%rbp), %eax
movl -0x1c4(%rbp), %ecx
cmpl %ecx, %eax
jne 0x29a0
movl $0x0, -0xe8(%rbp)
movq -0x118(%rbp), %rdi
movl -0xe8(%rbp), %eax
movl %eax, -0x1d0(%rbp)
callq 0x2d10
movl %eax, -0x1cc(%rbp)
jmp 0x2933
movl -0x1d0(%rbp), %ecx
movl -0x1cc(%rbp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x1d1(%rbp)
jge 0x295d
movl -0xe8(%rbp), %eax
cmpl -0x3c(%rbp), %eax
setne %al
movb %al, -0x1d1(%rbp)
movb -0x1d1(%rbp), %al
testb $0x1, %al
jne 0x2969
jmp 0x299e
movq -0x118(%rbp), %rdi
movslq -0xe8(%rbp), %rsi
callq 0x2e10
movq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0x2fa0
jmp 0x298a
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x2913
jmp 0x29a0
leaq -0x70(%rbp), %rdi
leaq -0xc4(%rbp), %rsi
callq 0x2fa0
jmp 0x29b2
leaq -0x70(%rbp), %rdi
leaq -0xe4(%rbp), %rsi
callq 0x2fa0
jmp 0x29c4
movq -0x118(%rbp), %rdi
movl -0xe8(%rbp), %eax
movl %eax, -0x1dc(%rbp)
callq 0x2d10
movl %eax, -0x1d8(%rbp)
jmp 0x29e4
movl -0x1dc(%rbp), %eax
movl -0x1d8(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2a03
movl $0x0, -0xe8(%rbp)
jmp 0x2a12
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0xd4(%rbp), %rsi
callq 0x2fa0
jmp 0x2a27
jmp 0x2a29
movq -0x118(%rbp), %rdi
movl -0xe8(%rbp), %eax
movl %eax, -0x1e4(%rbp)
callq 0x2d10
movl %eax, -0x1e0(%rbp)
jmp 0x2a49
movl -0x1e4(%rbp), %ecx
movl -0x1e0(%rbp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x1e5(%rbp)
jge 0x2a73
movl -0xe8(%rbp), %eax
cmpl -0x20(%rbp), %eax
setne %al
movb %al, -0x1e5(%rbp)
movb -0x1e5(%rbp), %al
testb $0x1, %al
jne 0x2a7f
jmp 0x2ab7
movq -0x118(%rbp), %rdi
movslq -0xe8(%rbp), %rsi
callq 0x2e10
movq %rax, %rsi
leaq -0x88(%rbp), %rdi
callq 0x2fa0
jmp 0x2aa3
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x2a29
movq -0x118(%rbp), %rdi
movl -0xe8(%rbp), %eax
movl %eax, -0x1f0(%rbp)
callq 0x2d10
movl %eax, -0x1ec(%rbp)
jmp 0x2ad7
movl -0x1f0(%rbp), %eax
movl -0x1ec(%rbp), %ecx
cmpl %ecx, %eax
jne 0x2b85
movl $0x0, -0xe8(%rbp)
movq -0x118(%rbp), %rdi
movl -0xe8(%rbp), %eax
movl %eax, -0x1f8(%rbp)
callq 0x2d10
movl %eax, -0x1f4(%rbp)
jmp 0x2b15
movl -0x1f8(%rbp), %ecx
movl -0x1f4(%rbp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x1f9(%rbp)
jge 0x2b3f
movl -0xe8(%rbp), %eax
cmpl -0x20(%rbp), %eax
setne %al
movb %al, -0x1f9(%rbp)
movb -0x1f9(%rbp), %al
testb $0x1, %al
jne 0x2b4b
jmp 0x2b83
movq -0x118(%rbp), %rdi
movslq -0xe8(%rbp), %rsi
callq 0x2e10
movq %rax, %rsi
leaq -0x88(%rbp), %rdi
callq 0x2fa0
jmp 0x2b6f
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x2af5
jmp 0x2b85
leaq -0x88(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
callq 0x2fa0
jmp 0x2b9a
leaq -0xdc(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
leaq -0xe4(%rbp), %rdx
callq 0x2f60
jmp 0x2bb6
leaq -0x88(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x2fa0
jmp 0x2bcb
movq -0x130(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x88(%rbp), %rdx
callq 0x2fd0
jmp 0x2be4
leaq -0x88(%rbp), %rdi
callq 0x3030
leaq -0x70(%rbp), %rdi
callq 0x3030
movq -0x128(%rbp), %rax
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
leaq -0x70(%rbp), %rdi
callq 0x3030
movq -0x90(%rbp), %rdi
callq 0x1170
nop