// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
xorl %edi, %edi
callq 0x10c0
movl %eax, %edi
callq 0x10d0
leaq -0x128(%rbp), %rdi
callq 0x25c8
xorpd %xmm0, %xmm0
movapd %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x1348
movaps 0x1f0d(%rip), %xmm0     # 0x40d0
leaq -0x198(%rbp), %rsi
movups %xmm0, (%rsi)
movaps 0x1f0c(%rip), %xmm0     # 0x40e0
movups %xmm0, -0x158(%rbp)
movaps 0x1f0e(%rip), %xmm0     # 0x40f0
movups %xmm0, -0x148(%rbp)
movsd 0x1f0f(%rip), %xmm0     # 0x4100
movupd %xmm0, -0x138(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x1d52
leaq -0x50(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x1d52
leaq -0x50(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x1d52
leaq -0x50(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x1d52
leaq -0x128(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x2cf0
leaq -0xa8(%rbp), %rdi
callq 0x1348
leaq -0x188(%rbp), %rdi
callq 0x25e8
leaq -0x170(%rbp), %rbx
leaq -0xa8(%rbp), %r15
leaq -0xd8(%rbp), %r12
leaq -0x188(%rbp), %r13
leaq -0x128(%rbp), %rdi
callq 0x2dde
testq %rax, %rax
je 0x24be
movq -0xf8(%rbp), %rsi
cmpq -0xf0(%rbp), %rsi
jne 0x22ae
movq -0xe0(%rbp), %rax
movq -0x8(%rax), %rsi
movl $0x1f8, %eax            # imm = 0x1F8
addq %rax, %rsi
addq $-0x18, %rsi
movq %r15, %rdi
callq 0x26ca
leaq -0x128(%rbp), %rdi
callq 0x2fa4
movq %r15, %rdi
callq 0x14aa
movsd %xmm0, -0x30(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x16a6
movss -0xd8(%rbp), %xmm0
movss %xmm0, -0x38(%rbp)
leaq -0x90(%rbp), %rdi
movq %r15, %rsi
callq 0x16a6
cvtss2sd -0x38(%rbp), %xmm0
cvtss2sd -0x8c(%rbp), %xmm1
callq 0x2140
movsd 0x1d94(%rip), %xmm1     # 0x40a8
subsd %xmm0, %xmm1
mulsd 0x1d90(%rip), %xmm1     # 0x40b0
movsd -0x30(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x248a
xorl %r14d, %r14d
movb $0x1, %al
testb $0x1, %al
je 0x249b
cmpl $0x13, %r14d
ja 0x249b
movq %r12, %rdi
movq %r15, %rsi
callq 0x17a6
movq %r13, %rdi
movq %r12, %rsi
callq 0x2618
movq %r12, %rdi
callq 0x2642
movq %r12, %rdi
movq %r13, %rsi
callq 0x16a6
cvtss2sd -0xd8(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x16a6
cvtss2sd -0xd4(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x16a6
cvtss2sd -0xd8(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x16a6
cvtss2sd -0xd4(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movq %r13, %rdi
callq 0x14aa
movsd %xmm0, -0x88(%rbp)
movsd -0x30(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
callq 0x2140
movsd 0x1cbd(%rip), %xmm1     # 0x40a8
subsd %xmm0, %xmm1
mulsd 0x1cc1(%rip), %xmm1     # 0x40b8
movsd -0x88(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x247e
movq %rbx, %rdi
callq 0x14aa
movsd %xmm0, -0x30(%rbp)
movsd -0x80(%rbp), %xmm0
movsd -0x78(%rbp), %xmm1
callq 0x2140
movsd 0x1c7f(%rip), %xmm1     # 0x40a8
subsd %xmm0, %xmm1
mulsd 0x1c83(%rip), %xmm1     # 0x40b8
movsd -0x30(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x247e
movq %r13, %rdi
callq 0x15cc
testb %al, %al
je 0x247e
movq %rbx, %rdi
callq 0x15cc
testb %al, %al
je 0x247e
movq %r13, %rdi
movsd 0x1c5d(%rip), %xmm0     # 0x40c0
callq 0x165a
testb %al, %al
je 0x247e
movq %rbx, %rdi
movsd 0x1c49(%rip), %xmm0     # 0x40c0
callq 0x165a
jmp 0x2480
xorl %eax, %eax
xorb $0x1, %al
incl %r14d
jmp 0x2334
leaq -0x70(%rbp), %rdi
movq %r15, %rsi
callq 0x2662
jmp 0x2276
leaq -0x128(%rbp), %rdi
movq %r13, %rsi
callq 0x2cf0
leaq -0x128(%rbp), %rdi
movq %rbx, %rsi
callq 0x2cf0
jmp 0x2276
xorl %r14d, %r14d
pushq $0x18
popq %r15
movq 0x4b04(%rip), %rbx      # 0x6fd0
xorl %r12d, %r12d
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rax
subq %rsi, %rax
cqto
idivq %r15
cmpq %r12, %rax
jbe 0x2508
addq %r14, %rsi
movq %rbx, %rdi
callq 0x1c87
movq %rax, %rdi
callq 0x1070
movq %rax, %rdi
callq 0x1070
incq %r12
addq $0x18, %r14
jmp 0x24cf
leaq -0x188(%rbp), %rdi
callq 0x2642
leaq -0xa8(%rbp), %rdi
callq 0x1fa8
leaq -0x50(%rbp), %rdi
callq 0x1fa8
leaq -0x70(%rbp), %rdi
callq 0x269e
leaq -0x128(%rbp), %rdi
callq 0x28e4
xorl %eax, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x2595
jmp 0x255e
movq %rax, %rbx
jmp 0x25aa
movq %rax, %rbx
jmp 0x25a1
jmp 0x2586
jmp 0x2586
jmp 0x2586
jmp 0x2586
jmp 0x2586
jmp 0x2586
jmp 0x2586
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x2642
jmp 0x2589
jmp 0x2586
jmp 0x2586
movq %rax, %rbx
leaq -0x188(%rbp), %rdi
callq 0x2642
leaq -0xa8(%rbp), %rdi
callq 0x1fa8
leaq -0x50(%rbp), %rdi
callq 0x1fa8
leaq -0x70(%rbp), %rdi
callq 0x269e
leaq -0x128(%rbp), %rdi
callq 0x28e4
movq %rbx, %rdi
callq 0x1150
nop