// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %rbx
subq $0x30, %rsp
movq %rdi, %rbx
movsd 0xa0(%rdi), %xmm1
movsd %xmm1, (%rsp)
movsd (%rsi), %xmm0
movsd 0x8(%rsi), %xmm2
movsd %xmm2, 0x18(%rsp)
divsd %xmm1, %xmm0
callq 0x81a0
movsd (%rsp), %xmm1
mulsd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
movsd 0x18(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x81a0
movapd %xmm0, %xmm12
mulsd (%rsp), %xmm12
movl 0x8(%rbx), %eax
movq 0x10(%rbx), %rbx
cmpl $0x2, %eax
pushq $0x1
popq %r14
cmovgel %eax, %r14d
decl %r14d
shlq $0x4, %r14
movsd 0x13cbf(%rip), %xmm1    # 0x36068
xorl %r15d, %r15d
xorpd %xmm13, %xmm13
movapd %xmm12, (%rsp)
cmpq %r15, %r14
je 0x224f2
movsd 0x10(%rbx,%r15), %xmm4
movsd (%rbx,%r15), %xmm3
movsd 0x8(%rbx,%r15), %xmm7
movapd %xmm4, %xmm2
subsd %xmm3, %xmm2
movapd 0x20(%rsp), %xmm11
movapd %xmm11, %xmm0
subsd %xmm3, %xmm0
movsd 0x18(%rbx,%r15), %xmm5
movapd %xmm5, %xmm8
subsd %xmm7, %xmm8
movapd %xmm12, %xmm6
subsd %xmm7, %xmm6
mulsd %xmm6, %xmm8
movapd %xmm2, %xmm9
mulsd %xmm0, %xmm9
addsd %xmm8, %xmm9
ucomisd %xmm9, %xmm13
jbe 0x22432
mulsd %xmm6, %xmm6
mulsd %xmm0, %xmm0
addsd %xmm6, %xmm0
jmp 0x224d5
movapd %xmm3, %xmm9
subsd %xmm4, %xmm9
movapd %xmm11, %xmm0
subsd %xmm4, %xmm0
movapd %xmm7, %xmm6
subsd %xmm5, %xmm6
movapd %xmm12, %xmm8
subsd %xmm5, %xmm8
movapd %xmm8, %xmm10
mulsd %xmm6, %xmm10
mulsd %xmm0, %xmm9
addsd %xmm10, %xmm9
ucomisd %xmm9, %xmm13
jbe 0x22482
mulsd %xmm8, %xmm8
mulsd %xmm0, %xmm0
addsd %xmm8, %xmm0
jmp 0x224d5
xorpd 0x13c36(%rip), %xmm4    # 0x360c0
unpcklpd %xmm7, %xmm12           # xmm12 = xmm12[0],xmm7[0]
movapd %xmm2, %xmm7
unpcklpd %xmm4, %xmm7            # xmm7 = xmm7[0],xmm4[0]
mulpd %xmm12, %xmm7
movapd %xmm6, %xmm0
unpcklpd %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0]
movapd %xmm11, %xmm3
unpcklpd %xmm5, %xmm3            # xmm3 = xmm3[0],xmm5[0]
mulpd %xmm0, %xmm3
addpd %xmm7, %xmm3
movapd %xmm3, %xmm0
unpckhpd %xmm3, %xmm0            # xmm0 = xmm0[1],xmm3[1]
addsd %xmm3, %xmm0
mulsd %xmm0, %xmm0
mulsd %xmm2, %xmm2
mulsd %xmm6, %xmm6
addsd %xmm2, %xmm6
divsd %xmm6, %xmm0
callq 0x8260
movapd %xmm0, %xmm1
addq $0x10, %r15
movapd (%rsp), %xmm12
xorpd %xmm13, %xmm13
jmp 0x223b7
movapd %xmm1, %xmm0
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
retq