// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movsd (%rsi), %xmm0
movsd 0x8(%rsi), %xmm2
movsd %xmm2, 0x18(%rsp)
divsd %xmm1, %xmm0
callq 0x71c0
movsd %xmm0, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x71c0
movl 0x8(%rbx), %eax
cmpl $0x2, %eax
jl 0x26b6e
movsd 0x10(%rsp), %xmm13
movsd 0x8(%rsp), %xmm1
mulsd %xmm1, %xmm13
mulsd %xmm1, %xmm0
decl %eax
movq 0x10(%rbx), %rcx
shlq $0x4, %rax
movsd 0x1867b(%rip), %xmm3    # 0x3f0b8
xorl %edx, %edx
xorpd %xmm2, %xmm2
movsd 0x10(%rcx,%rdx), %xmm7
movsd (%rcx,%rdx), %xmm5
movsd 0x8(%rcx,%rdx), %xmm6
movapd %xmm7, %xmm1
subsd %xmm5, %xmm1
movapd %xmm13, %xmm4
subsd %xmm5, %xmm4
movsd 0x18(%rcx,%rdx), %xmm9
movapd %xmm9, %xmm10
subsd %xmm6, %xmm10
movapd %xmm0, %xmm8
subsd %xmm6, %xmm8
mulsd %xmm8, %xmm10
movapd %xmm1, %xmm11
mulsd %xmm4, %xmm11
addsd %xmm10, %xmm11
ucomisd %xmm11, %xmm2
jbe 0x26aae
mulsd %xmm8, %xmm8
mulsd %xmm4, %xmm4
addsd %xmm8, %xmm4
jmp 0x26b3e
movapd %xmm5, %xmm11
subsd %xmm7, %xmm11
movapd %xmm13, %xmm4
subsd %xmm7, %xmm4
movapd %xmm6, %xmm8
subsd %xmm9, %xmm8
movapd %xmm0, %xmm10
subsd %xmm9, %xmm10
movapd %xmm10, %xmm12
mulsd %xmm8, %xmm12
mulsd %xmm4, %xmm11
addsd %xmm12, %xmm11
ucomisd %xmm11, %xmm2
jbe 0x26b00
mulsd %xmm10, %xmm10
mulsd %xmm4, %xmm4
addsd %xmm10, %xmm4
jmp 0x26b3e
mulsd %xmm7, %xmm6
mulsd %xmm9, %xmm5
subsd %xmm6, %xmm5
movapd %xmm0, %xmm6
mulsd %xmm1, %xmm6
movapd %xmm8, %xmm4
mulsd %xmm13, %xmm4
addsd %xmm6, %xmm4
addsd %xmm5, %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm1, %xmm1
mulsd %xmm8, %xmm8
addsd %xmm1, %xmm8
divsd %xmm8, %xmm4
movapd %xmm4, %xmm1
cmpunordsd %xmm4, %xmm1
movapd %xmm1, %xmm5
andpd %xmm3, %xmm5
minsd %xmm4, %xmm3
andnpd %xmm3, %xmm1
orpd %xmm5, %xmm1
addq $0x10, %rdx
movapd %xmm1, %xmm3
cmpq %rdx, %rax
jne 0x26a43
jmp 0x26b76
movsd 0x18542(%rip), %xmm1    # 0x3f0b8
movapd %xmm1, %xmm0
addq $0x20, %rsp
popq %rbx
retq