// File: Hybryd[P]HyRoadGenerator/main.cpp
// Function: main
int main()
{
  srand(time(NULL));
  double Amin=0.05;
  double Amax=0.7;
  int iterMax=20;
  
  
  std::stack<Polygon> mustBeEmpty;
  std::vector<Polygon> mustBeFull;
  
  
  Polygon pInit;

  // DO NOT PUT NEGATIVE VALUES (for the algorithm for areas)  
  std::pair < Point,Point > e1 (Point(0,0), Point(10,0));
  std::pair < Point,Point > e2 (Point(10,0), Point(10,10));
  std::pair < Point,Point > e3 (Point(10,10), Point(0,10));
  std::pair < Point,Point > e4 (Point(0,10),Point(0,0));

  
  pInit.push_back(e1);
  pInit.push_back(e2);
  pInit.push_back(e3);
  pInit.push_back(e4);

//  std::pair < Point,Point > e1 (Point(0,0), Point(10,0));
//  std::pair < Point,Point > e2 (Point(10,0), Point(0,10));
//  std::pair < Point,Point > e3 (Point(0,10), Point(0,0));

//  
//  pInit.push_back(e1);
//  pInit.push_back(e2);
//  pInit.push_back(e3);


  mustBeEmpty.push(pInit);


  Polygon p;
  std::pair< Polygon,Polygon> pair;
  bool ok=true;
  int j=0;
  double xbar1=0;
  double ybar1=0;
  double xbar2=0;
  double ybar2=0;


  while(mustBeEmpty.size()!=0)
  {
    p=mustBeEmpty.top();
    mustBeEmpty.pop();
    if(p.area() > (1-density(p.getBary().x(),p.getBary().y()))*Amax)
    {
      ok=true;
      j=0;
      xbar1=0;
      ybar1=0;
      xbar2=0;
      ybar2=0;
      while(ok && j<iterMax)
      {
        pair = p.split();
        xbar1=pair.first.getBary().x();
        ybar1=pair.first.getBary().y();
        xbar2=pair.second.getBary().x();
        ybar2=pair.second.getBary().y();
        
        // constraints on the resulting polygons
        ok = !(pair.first.area() > (1-density(xbar1,ybar1))*Amin && pair.second.area() > (1-density(xbar2,ybar2))*Amin && pair.first.isClosed() && pair.second.isClosed() && (pair.first.isAcceptable(10)  && pair.second.isAcceptable(10)  ));
        ++j;
      }
      mustBeEmpty.push(pair.first);
      mustBeEmpty.push(pair.second);
    }
    else
    {
      mustBeFull.push_back(p);
    }
//    std::cerr << mustBeFull.size() << std::endl;
  }



  // display all the polygons
  for(int i=0;i<mustBeFull.size();++i)
    std::cout << mustBeFull[i] << std::endl << std::endl;

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movl $0x0, -0x4(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x10d0
movl %eax, %edi
callq 0x10e0
movabsq $0x3fa999999999999a, %rax # imm = 0x3FA999999999999A
movq %rax, -0x10(%rbp)
movabsq $0x3fe6666666666666, %rax # imm = 0x3FE6666666666666
movq %rax, -0x18(%rbp)
movl $0x14, -0x1c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x4a60
leaq -0x88(%rbp), %rdi
callq 0x4a90
leaq -0xa0(%rbp), %rdi
callq 0x13f0
jmp 0x4228
leaq -0xc4(%rbp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x2e80
jmp 0x423c
leaq -0xcc(%rbp), %rdi
movss 0x3e3d(%rip), %xmm0     # 0x8088
xorps %xmm1, %xmm1
callq 0x2e80
jmp 0x4255
leaq -0xbc(%rbp), %rdi
leaq -0xc4(%rbp), %rsi
leaq -0xcc(%rbp), %rdx
callq 0x4ab0
jmp 0x4271
leaq -0xe4(%rbp), %rdi
movss 0x3e08(%rip), %xmm0     # 0x8088
xorps %xmm1, %xmm1
callq 0x2e80
jmp 0x428a
leaq -0xec(%rbp), %rdi
movss 0x3def(%rip), %xmm1     # 0x8088
movaps %xmm1, %xmm0
callq 0x2e80
jmp 0x42a3
leaq -0xdc(%rbp), %rdi
leaq -0xe4(%rbp), %rsi
leaq -0xec(%rbp), %rdx
callq 0x4ab0
jmp 0x42bf
leaq -0x104(%rbp), %rdi
movss 0x3dba(%rip), %xmm1     # 0x8088
movaps %xmm1, %xmm0
callq 0x2e80
jmp 0x42d8
leaq -0x10c(%rbp), %rdi
xorps %xmm0, %xmm0
movss 0x3d9e(%rip), %xmm1     # 0x8088
callq 0x2e80
jmp 0x42f1
leaq -0xfc(%rbp), %rdi
leaq -0x104(%rbp), %rsi
leaq -0x10c(%rbp), %rdx
callq 0x4ab0
jmp 0x430d
leaq -0x124(%rbp), %rdi
xorps %xmm0, %xmm0
movss 0x3d69(%rip), %xmm1     # 0x8088
callq 0x2e80
jmp 0x4326
leaq -0x12c(%rbp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x2e80
jmp 0x433a
leaq -0x11c(%rbp), %rdi
leaq -0x124(%rbp), %rsi
leaq -0x12c(%rbp), %rdx
callq 0x4ab0
jmp 0x4356
leaq -0xa0(%rbp), %rdi
leaq -0xbc(%rbp), %rsi
callq 0x2fa0
jmp 0x436b
leaq -0xa0(%rbp), %rdi
leaq -0xdc(%rbp), %rsi
callq 0x2fa0
jmp 0x4380
leaq -0xa0(%rbp), %rdi
leaq -0xfc(%rbp), %rsi
callq 0x2fa0
jmp 0x4395
leaq -0xa0(%rbp), %rdi
leaq -0x11c(%rbp), %rsi
callq 0x2fa0
jmp 0x43aa
leaq -0x70(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x4af0
jmp 0x43bc
leaq -0x148(%rbp), %rdi
callq 0x13f0
jmp 0x43ca
leaq -0x178(%rbp), %rdi
callq 0x4b20
jmp 0x43d8
movb $0x1, -0x179(%rbp)
movl $0x0, -0x180(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x188(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x190(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x198(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x1a0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x4b70
movq %rax, -0x210(%rbp)
jmp 0x4427
movq -0x210(%rbp), %rax
cmpq $0x0, %rax
je 0x491d
leaq -0x70(%rbp), %rdi
callq 0x4b90
movq %rax, -0x218(%rbp)
jmp 0x444a
movq -0x218(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x4bb0
jmp 0x445f
leaq -0x70(%rbp), %rdi
callq 0x4be0
jmp 0x446a
leaq -0x148(%rbp), %rdi
callq 0x1550
movsd %xmm0, -0x220(%rbp)
jmp 0x4480
leaq -0x1a8(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x1dc0
jmp 0x4495
leaq -0x1a8(%rbp), %rdi
callq 0x13b0
movss %xmm0, -0x224(%rbp)
jmp 0x44ab
movss -0x224(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x230(%rbp)
leaq -0x1b0(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x1dc0
jmp 0x44d4
leaq -0x1b0(%rbp), %rdi
callq 0x13d0
movss %xmm0, -0x234(%rbp)
jmp 0x44ea
movsd -0x230(%rbp), %xmm0
movss -0x234(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
callq 0x4120
movaps %xmm0, %xmm2
movsd -0x220(%rbp), %xmm0
movsd 0x3b62(%rip), %xmm1     # 0x8078
subsd %xmm2, %xmm1
mulsd -0x18(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4901
movb $0x1, -0x179(%rbp)
movl $0x0, -0x180(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x188(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x190(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x198(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x1a0(%rbp)
xorl %eax, %eax
testb $0x1, -0x179(%rbp)
movb %al, -0x235(%rbp)
je 0x4589
movl -0x180(%rbp), %eax
cmpl -0x1c(%rbp), %eax
setl %al
movb %al, -0x235(%rbp)
movb -0x235(%rbp), %al
testb $0x1, %al
jne 0x4598
jmp 0x48db
leaq -0x1e0(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x2090
jmp 0x45ad
leaq -0x178(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x4c00
jmp 0x45c2
leaq -0x1e0(%rbp), %rdi
callq 0x4c40
leaq -0x1e8(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x1dc0
jmp 0x45e3
leaq -0x1e8(%rbp), %rdi
callq 0x13b0
movss %xmm0, -0x23c(%rbp)
jmp 0x45f9
movss -0x23c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x188(%rbp)
leaq -0x1f0(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x1dc0
jmp 0x4622
leaq -0x1f0(%rbp), %rdi
callq 0x13d0
movss %xmm0, -0x240(%rbp)
jmp 0x4638
movss -0x240(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x190(%rbp)
leaq -0x160(%rbp), %rsi
leaq -0x1f8(%rbp), %rdi
callq 0x1dc0
jmp 0x4661
leaq -0x1f8(%rbp), %rdi
callq 0x13b0
movss %xmm0, -0x244(%rbp)
jmp 0x4677
movss -0x244(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x198(%rbp)
leaq -0x160(%rbp), %rsi
leaq -0x200(%rbp), %rdi
callq 0x1dc0
jmp 0x46a0
leaq -0x200(%rbp), %rdi
callq 0x13d0
movss %xmm0, -0x248(%rbp)
jmp 0x46b6
movss -0x248(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x1a0(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x1550
movsd %xmm0, -0x250(%rbp)
jmp 0x46e0
movsd -0x188(%rbp), %xmm0
movsd -0x190(%rbp), %xmm1
callq 0x4120
movaps %xmm0, %xmm2
movsd -0x250(%rbp), %xmm0
movsd 0x3970(%rip), %xmm1     # 0x8078
subsd %xmm2, %xmm1
mulsd -0x10(%rbp), %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
movb %al, -0x251(%rbp)
jbe 0x4838
leaq -0x160(%rbp), %rdi
callq 0x1550
movsd %xmm0, -0x260(%rbp)
jmp 0x4739
movsd -0x198(%rbp), %xmm0
movsd -0x1a0(%rbp), %xmm1
callq 0x4120
movaps %xmm0, %xmm2
movsd -0x260(%rbp), %xmm0
movsd 0x3917(%rip), %xmm1     # 0x8078
subsd %xmm2, %xmm1
mulsd -0x10(%rbp), %xmm1
xorl %eax, %eax
ucomisd %xmm1, %xmm0
movb %al, -0x251(%rbp)
jbe 0x4838
leaq -0x178(%rbp), %rdi
callq 0x1c60
movb %al, -0x261(%rbp)
jmp 0x4790
movb -0x261(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x251(%rbp)
jne 0x47a8
jmp 0x4838
leaq -0x160(%rbp), %rdi
callq 0x1c60
movb %al, -0x262(%rbp)
jmp 0x47bc
movb -0x262(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x251(%rbp)
jne 0x47d1
jmp 0x4838
leaq -0x178(%rbp), %rdi
movsd 0x38a0(%rip), %xmm0     # 0x8080
callq 0x1d50
movb %al, -0x263(%rbp)
jmp 0x47ed
movb -0x263(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x264(%rbp)
jne 0x4802
jmp 0x482c
leaq -0x160(%rbp), %rdi
movsd 0x386f(%rip), %xmm0     # 0x8080
callq 0x1d50
movb %al, -0x265(%rbp)
jmp 0x481e
movb -0x265(%rbp), %al
movb %al, -0x264(%rbp)
jmp 0x482c
movb -0x264(%rbp), %al
movb %al, -0x251(%rbp)
movb -0x251(%rbp), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x179(%rbp)
movl -0x180(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x180(%rbp)
jmp 0x4566
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
jmp 0x4a32
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
jmp 0x4a26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
jmp 0x4a1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
jmp 0x4a0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x4c40
jmp 0x4a0e
leaq -0x70(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x4af0
jmp 0x48ed
leaq -0x160(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x4af0
jmp 0x48ff
jmp 0x4918
leaq -0x88(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x4c70
jmp 0x4916
jmp 0x4918
jmp 0x4415
movl $0x0, -0x204(%rbp)
movslq -0x204(%rbp), %rax
movq %rax, -0x270(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x4ce0
movq %rax, %rcx
movq -0x270(%rbp), %rax
cmpq %rcx, %rax
jae 0x49c2
movslq -0x204(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x4d10
movq %rax, %rsi
movq 0x8663(%rip), %rdi      # 0xcfd0
callq 0x2c20
movq %rax, -0x278(%rbp)
jmp 0x497b
movq -0x278(%rbp), %rdi
movq 0x8637(%rip), %rsi      # 0xcfc0
callq 0x1120
movq %rax, -0x280(%rbp)
jmp 0x4997
movq -0x280(%rbp), %rdi
movq 0x861b(%rip), %rsi      # 0xcfc0
callq 0x1120
jmp 0x49ac
jmp 0x49ae
movl -0x204(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x204(%rbp)
jmp 0x4927
movl $0x0, -0x4(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x4c40
leaq -0x148(%rbp), %rdi
callq 0x3030
leaq -0xa0(%rbp), %rdi
callq 0x3030
leaq -0x88(%rbp), %rdi
callq 0x4d30
leaq -0x70(%rbp), %rdi
callq 0x4d90
movl -0x4(%rbp), %eax
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
leaq -0x178(%rbp), %rdi
callq 0x4c40
leaq -0x148(%rbp), %rdi
callq 0x3030
leaq -0xa0(%rbp), %rdi
callq 0x3030
leaq -0x88(%rbp), %rdi
callq 0x4d30
leaq -0x70(%rbp), %rdi
callq 0x4d90
movq -0xa8(%rbp), %rdi
callq 0x1170
nopw %cs:(%rax,%rax)
nopl (%rax)