// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Dist.cpp
// Function: gaps::R2Distance(gaps::R2Point const&, gaps::R2Arc const&)
RNLength R2Distance(const R2Point& point, const R2Arc& arc)
{
    // Figure sector - positive ds are inside arc sector
    RNLength d1 = R2SignedDistance(R2Line(arc.StartPoint(), arc.Center()), point);
    RNLength d2 = R2SignedDistance(R2Line(arc.Center(), arc.StopPoint()), point);
    if (d1 > 0.0) {
        if (d2 > 0.0) {
  	    // Point is in arc sector
	    RNLength d = R2Distance(arc.Center(), point) - arc.Radius();
	    return (d < 0) ? -d : d;
	}
        else {
	    // Point may be in arc sector 
	    if (arc.SweepAngle() > RN_PI) {
	        // Point is in arc sector
	        RNLength d = R2Distance(arc.Center(), point) - arc.Radius();
		return (d < 0) ? -d : d;
	    }
	    else {
	        // Point is closest to stop point
	        return R2Distance(arc.StopPoint(), point);
	    }
        }
    }
    else {
        if (d2 > 0.0) {
	    // Point may be in arc sector 
	    if (arc.SweepAngle() > RN_PI) {
	        // Point is in arc sector
	        RNLength d = R2Distance(arc.Center(), point) - arc.Radius();
		return (d < 0) ? -d : d;
	    }
	    else {
	        // Point is closest to start point
	        return R2Distance(arc.StartPoint(), point);
	    }
        }
        else {
  	    // Point is outside arc sector
	    RNLength d = R2Distance(arc.Center(), point) - arc.Radius();
	    return (d < 0) ? -d : d;
        }
    }
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
movq 0x110(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0xcb420
movq 0x110(%rsp), %rdi
callq 0xcb470
movq %rax, %rdx
leaq 0xe0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0xc29c0
movq 0x118(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0xcb900
movsd %xmm0, 0x108(%rsp)
movq 0x110(%rsp), %rdi
callq 0xcb470
movq %rax, 0x48(%rsp)
movq 0x110(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0xcb4b0
movq 0x48(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rdx
callq 0xc29c0
movq 0x118(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0xcb900
movsd %xmm0, 0xc8(%rsp)
movsd 0x108(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xcc0c8
movsd 0xc8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xcbfd9
movq 0x110(%rsp), %rdi
callq 0xcb470
movq %rax, %rdi
movq 0x118(%rsp), %rsi
callq 0xcb540
movsd %xmm0, 0x40(%rsp)
movq 0x110(%rsp), %rdi
callq 0xcb490
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x88(%rsp), %xmm0
jbe 0xcbfb6
movsd 0x88(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x38(%rsp)
jmp 0xcbfc5
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x120(%rsp)
jmp 0xcc24f
movq 0x110(%rsp), %rdi
callq 0xcd160
leaq 0x6a8eb(%rip), %rax     # 0x1368d8
ucomisd (%rax), %xmm0
jbe 0xcc096
movq 0x110(%rsp), %rdi
callq 0xcb470
movq %rax, %rdi
movq 0x118(%rsp), %rsi
callq 0xcb540
movsd %xmm0, 0x30(%rsp)
movq 0x110(%rsp), %rdi
callq 0xcb490
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x80(%rsp), %xmm0
jbe 0xcc073
movsd 0x80(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0xcc082
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x120(%rsp)
jmp 0xcc24f
movq 0x110(%rsp), %rsi
leaq 0x70(%rsp), %rdi
callq 0xcb4b0
movq 0x118(%rsp), %rsi
leaq 0x70(%rsp), %rdi
callq 0xcb540
movsd %xmm0, 0x120(%rsp)
jmp 0xcc24f
movsd 0xc8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xcc1c1
movq 0x110(%rsp), %rdi
callq 0xcd160
leaq 0x6a7e6(%rip), %rax     # 0x1368d8
ucomisd (%rax), %xmm0
jbe 0xcc18f
movq 0x110(%rsp), %rdi
callq 0xcb470
movq %rax, %rdi
movq 0x118(%rsp), %rsi
callq 0xcb540
movsd %xmm0, 0x20(%rsp)
movq 0x110(%rsp), %rdi
callq 0xcb490
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x68(%rsp), %xmm0
jbe 0xcc16f
movsd 0x68(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0xcc17b
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x120(%rsp)
jmp 0xcc24f
movq 0x110(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0xcb420
movq 0x118(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0xcb540
movsd %xmm0, 0x120(%rsp)
jmp 0xcc24f
movq 0x110(%rsp), %rdi
callq 0xcb470
movq %rax, %rdi
movq 0x118(%rsp), %rsi
callq 0xcb540
movsd %xmm0, 0x10(%rsp)
movq 0x110(%rsp), %rdi
callq 0xcb490
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x50(%rsp), %xmm0
jbe 0xcc234
movsd 0x50(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0xcc240
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x120(%rsp)
movsd 0x120(%rsp), %xmm0
addq $0x128, %rsp            # imm = 0x128
retq