// File: thierry7100[P]CutOptim/src/Geometry.cpp
// Function: Polygon::distance(Point&)
double Polygon::distance(Point &p)
{
Point lP = Point(round(p.x/precision)*precision, round(p.y/precision)*precision);
double d1;
double dmin = 1e20;

    for ( int i = 0; i < nVertices-1; i++)
    {
        //  Compute distance from point lP to segment vertice[i], vertice[i+1]
        //  If dot product AB.AP is < 0 return distance(A, P)
        //  Indeed, if this is the case P is outer segment further than A
        if ( (_Vertices[i+1].x - _Vertices[i].x)*(lP.x - _Vertices[i].x) + (_Vertices[i+1].y-_Vertices[i].y)*(lP.y-_Vertices[i].y) < 0 )
            d1 = ( (lP.x - _Vertices[i].x)*(lP.x- _Vertices[i].x) + (lP.y-_Vertices[i].y)*(lP.y-_Vertices[i].y));
        //  If dot product BA.BP si > 0 return distance(B, P)
        else if ( (_Vertices[i].x - _Vertices[i+1].x)*(lP.x - _Vertices[i+1].x) + (_Vertices[i].y-_Vertices[i+1].y)*(lP.y-_Vertices[i+1].y) < 0 )
            d1 = ( (lP.x - _Vertices[i+1].x)*(lP.x- _Vertices[i+1].x) + (lP.y-_Vertices[i+1].y)*(lP.y-_Vertices[i+1].y));
        else
        //  Distance point to line is a * p.x + b * p.y + c)*(a * p.x + b * p.y + c)/(a*a + b*b
        //  line coefficients : a = yA - yB  b = xB - xA c = A.x * B.y - B.x * A.y
        {
            double a = _Vertices[i].y - _Vertices[i+1].y;
            double b = _Vertices[i+1].x - _Vertices[i].x;
            double c = _Vertices[i].x * _Vertices[i+1].y- _Vertices[i+1].x * _Vertices[i].y ;
            d1 = (a* lP.x + b * lP.y + c)*(a * lP.x + b * lP.y + c)/(a*a + b*b);
        }
        dmin = fmin(d1, dmin);
    }
    return(dmin);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rcx
movsd (%rcx), %xmm0
divsd 0xa0(%rax), %xmm0
callq 0x9220
movq -0x60(%rbp), %rax
mulsd 0xa0(%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x10(%rbp), %rcx
movsd 0x8(%rcx), %xmm0
divsd 0xa0(%rax), %xmm0
callq 0x9220
movq -0x60(%rbp), %rax
movaps %xmm0, %xmm1
movsd -0x58(%rbp), %xmm0
mulsd 0xa0(%rax), %xmm1
leaq -0x20(%rbp), %rdi
callq 0x5b520
movsd 0x2518c(%rip), %xmm0    # 0x840e8
movsd %xmm0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x60(%rbp), %rcx
movl -0x34(%rbp), %eax
movl 0x8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x5f585
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movq -0x60(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movsd -0x98(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movsd -0x90(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0x68(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movq -0x60(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x88(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movsd -0x88(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd 0x8(%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movsd -0x80(%rbp), %xmm3
movsd -0x78(%rbp), %xmm0
movsd -0x70(%rbp), %xmm1
movsd -0x68(%rbp), %xmm2
subsd 0x8(%rax), %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x5f173
movq -0x60(%rbp), %rdi
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movsd -0xd0(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movsd -0xc8(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movsd -0xc0(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd 0x8(%rax), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movsd -0xb8(%rbp), %xmm3
movsd -0xb0(%rbp), %xmm1
movsd -0xa8(%rbp), %xmm0
movsd -0xa0(%rbp), %xmm2
subsd 0x8(%rax), %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x5f54a
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movq -0x60(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x108(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movsd -0x108(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x100(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movsd -0x100(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0xd8(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movq -0x60(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0xf8(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movsd -0xf8(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd 0x8(%rax), %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0xf0(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movsd -0xf0(%rbp), %xmm3
movsd -0xe8(%rbp), %xmm0
movsd -0xe0(%rbp), %xmm1
movsd -0xd8(%rbp), %xmm2
subsd 0x8(%rax), %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x5f39f
movq -0x60(%rbp), %rdi
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x140(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movsd -0x140(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0x118(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x138(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movsd -0x138(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0x110(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x130(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movsd -0x130(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd 0x8(%rax), %xmm0
movsd %xmm0, -0x120(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x128(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movsd -0x128(%rbp), %xmm3
movsd -0x120(%rbp), %xmm1
movsd -0x118(%rbp), %xmm0
movsd -0x110(%rbp), %xmm2
subsd 0x8(%rax), %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x5f548
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movq -0x60(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x168(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movsd -0x168(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd 0x8(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movq -0x60(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x160(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movsd -0x160(%rbp), %xmm0
movq -0x60(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movq -0x60(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x150(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movq -0x60(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x148(%rbp)
addq $0x10, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movq -0x60(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x158(%rbp)
addq $0x10, %rdi
movslq -0x34(%rbp), %rsi
callq 0x63990
movsd -0x158(%rbp), %xmm1
movsd -0x150(%rbp), %xmm0
movsd -0x148(%rbp), %xmm2
mulsd 0x8(%rax), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x40(%rbp), %xmm0
movsd -0x20(%rbp), %xmm2
movsd -0x48(%rbp), %xmm1
mulsd -0x18(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
addsd -0x50(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movsd -0x20(%rbp), %xmm3
movsd -0x48(%rbp), %xmm2
mulsd -0x18(%rbp), %xmm2
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
addsd -0x50(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd -0x40(%rbp), %xmm1
movsd -0x40(%rbp), %xmm3
movsd -0x48(%rbp), %xmm2
mulsd -0x48(%rbp), %xmm2
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x5f54a
movsd -0x28(%rbp), %xmm3
movsd -0x30(%rbp), %xmm2
movaps %xmm2, %xmm4
movaps %xmm3, %xmm0
cmpunordsd %xmm0, %xmm0
movaps %xmm0, %xmm1
pand %xmm4, %xmm1
minsd %xmm3, %xmm2
pandn %xmm2, %xmm0
por %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5ef68
movsd -0x30(%rbp), %xmm0
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopw %cs:(%rax,%rax)