// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Span.cpp
// Function: gaps::R2Span::Clip(gaps::R2Line const&)
RNClassID R2Span::
Clip(const R2Line& line)
{
    // Characterize endpoints with respect to line
    RNScalar d1 = R2SignedDistance(line, Start());
    RNScalar d2 = R2SignedDistance(line, End());

    // Clip span to line
    if (RNIsNegative(d1)) {
	if (RNIsNegative(d2)) {
	    // Both points are below line ???
	    Reset(Start(), Start());
	    return RN_NULL_CLASS_ID;
	}
	else if (RNIsPositive(d2)) {
	    // Start is below, end is above -- move start to line
	    Reset((Start() * d2 + End() * -d1) / (d2 - d1), End());
	    return R2_SPAN_CLASS_ID;
	}
	else {
	    // Start is below, end is on -- move start to end
	    Reset(End(), End());
	    return R2_POINT_CLASS_ID;
	}
    }
    else if (RNIsPositive(d1)) {
	if (RNIsNegative(d2)) {
	    // Start is above, end is below -- move end to line
	    Reset(Start(), (Start() * -d2 + End() * d1) / (d1 - d2));
	    return R2_SPAN_CLASS_ID;
	}
	else {
	    // Start is above, end is on or above
	    return R2_SPAN_CLASS_ID;
	}
    }
    else {
	if (RNIsNegative(d2)) {
	    // Start is on, end is below -- move end to start
	    Reset(Start(), Start());
	    return R2_POINT_CLASS_ID;
	}
	else {
	    // Start is on, end is on or above
	    return R2_SPAN_CLASS_ID;
	}
    }
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq 0xd8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x28(%rsp)
callq 0xa3500
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0xcb900
movq 0x30(%rsp), %rdi
movsd %xmm0, 0xc8(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x38(%rsp)
callq 0xa3510
movq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0xcb900
movsd %xmm0, 0xc0(%rsp)
movsd 0xc8(%rsp), %xmm0
leaq 0xb67e7(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0xc1e91
movsd 0xc0(%rsp), %xmm0
leaq 0xb67c5(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0xc1d64
movq 0x30(%rsp), %rdi
callq 0xa3500
movq 0x30(%rsp), %rdi
movq %rax, 0x20(%rsp)
callq 0xa3500
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rax, %rdx
callq 0xc1c00
movl $0x0, 0xe4(%rsp)
jmp 0xc1fff
movsd 0xc0(%rsp), %xmm0
leaq 0xb676c(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0xc1e56
movq 0x30(%rsp), %rdi
callq 0xa3500
movq %rax, %rsi
movsd 0xc0(%rsp), %xmm0
leaq 0x90(%rsp), %rdi
callq 0xc3a60
movq 0x30(%rsp), %rdi
callq 0xa3510
movq %rax, %rsi
movsd 0xc8(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq 0x80(%rsp), %rdi
callq 0xc3a60
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0xc3750
movsd 0xc0(%rsp), %xmm0
subsd 0xc8(%rsp), %xmm0
leaq 0xb0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0xc3ad0
movq 0x30(%rsp), %rdi
callq 0xa3510
movq 0x30(%rsp), %rdi
movq %rax, %rdx
leaq 0xb0(%rsp), %rsi
callq 0xc1c00
movl $0x4, 0xe4(%rsp)
jmp 0xc1fff
movq 0x30(%rsp), %rdi
callq 0xa3510
movq 0x30(%rsp), %rdi
movq %rax, 0x18(%rsp)
callq 0xa3510
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rdx
callq 0xc1c00
movl $0x1, 0xe4(%rsp)
jmp 0xc1fff
movsd 0xc8(%rsp), %xmm0
leaq 0xb663f(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0xc1f9e
movsd 0xc0(%rsp), %xmm0
leaq 0xb661d(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0xc1f91
movq 0x30(%rsp), %rdi
callq 0xa3500
movq 0x30(%rsp), %rdi
movq %rax, 0x10(%rsp)
callq 0xa3500
movq %rax, %rsi
movsd 0xc0(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq 0x50(%rsp), %rdi
callq 0xc3a60
movq 0x30(%rsp), %rdi
callq 0xa3510
movq %rax, %rsi
movsd 0xc8(%rsp), %xmm0
leaq 0x40(%rsp), %rdi
callq 0xc3a60
leaq 0x60(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0xc3750
movsd 0xc8(%rsp), %xmm0
subsd 0xc0(%rsp), %xmm0
leaq 0x70(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xc3ad0
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0xc1c00
movl $0x4, 0xe4(%rsp)
jmp 0xc1fff
movl $0x4, 0xe4(%rsp)
jmp 0xc1fff
movsd 0xc0(%rsp), %xmm0
leaq 0xb6532(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0xc1ff4
movq 0x30(%rsp), %rdi
callq 0xa3500
movq 0x30(%rsp), %rdi
movq %rax, 0x8(%rsp)
callq 0xa3500
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0xc1c00
movl $0x1, 0xe4(%rsp)
jmp 0xc1fff
movl $0x4, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
addq $0xe8, %rsp
retq
nop