// File: /workspace/github_cpp_makefile_star2_O3/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 %rbx
subq $0x20, %rsp
movq %rdi, %rbx
movsd 0xa0(%rdi), %xmm1
movsd %xmm1, (%rsp)
movsd (%rsi), %xmm0
movsd 0x8(%rsi), %xmm2
movsd %xmm2, 0x8(%rsp)
divsd %xmm1, %xmm0
callq 0x71c0
movapd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x71c0
movl 0x8(%rbx), %eax
cmpl $0x2, %eax
jl 0x26946
movapd 0x10(%rsp), %xmm14
movsd (%rsp), %xmm1
mulsd %xmm1, %xmm14
mulsd %xmm1, %xmm0
decl %eax
movq 0x10(%rbx), %rcx
movsd (%rcx), %xmm2
movsd 0x8(%rcx), %xmm3
shlq $0x4, %rax
movsd 0x17885(%rip), %xmm6    # 0x3e068
xorl %edx, %edx
xorpd %xmm4, %xmm4
movapd 0x178cf(%rip), %xmm5    # 0x3e0c0
movapd %xmm2, %xmm8
movapd %xmm3, %xmm9
movsd 0x10(%rcx,%rdx), %xmm2
movsd 0x18(%rcx,%rdx), %xmm3
movapd %xmm2, %xmm1
subsd %xmm8, %xmm1
movapd %xmm14, %xmm7
subsd %xmm8, %xmm7
movapd %xmm3, %xmm11
subsd %xmm9, %xmm11
movapd %xmm0, %xmm10
subsd %xmm9, %xmm10
mulsd %xmm10, %xmm11
movapd %xmm1, %xmm12
mulsd %xmm7, %xmm12
addsd %xmm11, %xmm12
ucomisd %xmm12, %xmm4
jbe 0x2685c
mulsd %xmm10, %xmm10
mulsd %xmm7, %xmm7
addsd %xmm10, %xmm7
jmp 0x26913
movapd %xmm8, %xmm12
subsd %xmm2, %xmm12
movapd %xmm14, %xmm7
subsd %xmm2, %xmm7
movapd %xmm9, %xmm10
subsd %xmm3, %xmm10
movapd %xmm0, %xmm11
subsd %xmm3, %xmm11
movapd %xmm11, %xmm13
mulsd %xmm10, %xmm13
mulsd %xmm7, %xmm12
addsd %xmm13, %xmm12
ucomisd %xmm12, %xmm4
jbe 0x268ae
mulsd %xmm11, %xmm11
mulsd %xmm7, %xmm7
addsd %xmm11, %xmm7
jmp 0x26913
movapd %xmm2, %xmm7
xorpd %xmm5, %xmm7
movapd %xmm0, %xmm11
unpcklpd %xmm9, %xmm11           # xmm11 = xmm11[0],xmm9[0]
movapd %xmm1, %xmm9
unpcklpd %xmm7, %xmm9            # xmm9 = xmm9[0],xmm7[0]
mulpd %xmm11, %xmm9
movapd %xmm10, %xmm7
unpcklpd %xmm8, %xmm7            # xmm7 = xmm7[0],xmm8[0]
movapd %xmm14, %xmm8
unpcklpd %xmm3, %xmm8            # xmm8 = xmm8[0],xmm3[0]
mulpd %xmm7, %xmm8
addpd %xmm9, %xmm8
movapd %xmm8, %xmm7
unpckhpd %xmm8, %xmm7            # xmm7 = xmm7[1],xmm8[1]
addsd %xmm8, %xmm7
mulsd %xmm7, %xmm7
mulsd %xmm1, %xmm1
mulsd %xmm10, %xmm10
addsd %xmm1, %xmm10
divsd %xmm10, %xmm7
movapd %xmm7, %xmm1
cmpunordsd %xmm7, %xmm1
movapd %xmm1, %xmm8
andpd %xmm6, %xmm8
minsd %xmm7, %xmm6
andnpd %xmm6, %xmm1
orpd %xmm8, %xmm1
addq $0x10, %rdx
movapd %xmm1, %xmm6
cmpq %rdx, %rax
jne 0x267f1
jmp 0x2694e
movsd 0x1771a(%rip), %xmm1    # 0x3e068
movapd %xmm1, %xmm0
addq $0x20, %rsp
popq %rbx
retq