// File: wrighteagle2d[P]wrighteaglebase/Debug/../src/Geometry.cpp
// Function: Rectangular::Intersection(Ray const&, Vector&) const
bool Rectangular::Intersection(const Ray &r, Vector &point) const {
  if (!IsWithin(r.Origin())) {
    return false; // do not deal with this condition
  }

  int n = 0;
  Array<Vector, 4> points; // there may be 4 intersections

  if (TopEdge().Intersection(r, points[n])) {
    if (IsWithin(points[n])) {
      n++;
    }
  }

  if (BottomEdge().Intersection(r, points[n])) {
    if (IsWithin(points[n])) {
      n++;
    }
  }

  if (LeftEdge().Intersection(r, points[n])) {
    if (IsWithin(points[n])) {
      n++;
    }
  }

  if (RightEdge().Intersection(r, points[n])) {
    if (IsWithin(points[n])) {
      n++;
    }
  }

  if (n == 0) {
    return false;
  } else if (n == 1) {
    point = points[0];
    return true;
  } else if (n >= 2) {
    int max_idx = 0;
    double max_dist2 = points[0].Dist2(r.Origin());

    for (int j = 1; j < n; ++j) {
      double dist2 = points[j].Dist2(r.Origin());
      if (dist2 > max_dist2) {
        max_idx = j;
        max_dist2 = dist2;
      }
    }

    point = points[max_idx];
    return true;
  }

  return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x18(%rbp), %rdi
callq 0x32c70
movq -0x178(%rbp), %rdi
movq %rax, %rsi
movsd 0x7931b(%rip), %xmm0    # 0xc1008
movsd %xmm0, -0x28(%rbp)
leaq -0x28(%rbp), %rdx
callq 0x1d850
xorb $-0x1, %al
testb $0x1, %al
jne 0x47d03
jmp 0x47d0c
movb $0x0, -0x1(%rbp)
jmp 0x4813f
movl $0x0, -0x2c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x49120
movq -0x178(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x49180
movq -0x18(%rbp), %rax
movq %rax, -0x180(%rbp)
movslq -0x2c(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x491e0
movq -0x180(%rbp), %rsi
movq %rax, %rdx
leaq -0x88(%rbp), %rdi
callq 0x47af0
testb $0x1, %al
jne 0x47d71
jmp 0x47dc3
movslq -0x2c(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x491e0
movq -0x178(%rbp), %rdi
movq %rax, %rsi
movsd 0x7926a(%rip), %xmm0    # 0xc1008
movsd %xmm0, -0xa0(%rbp)
leaq -0xa0(%rbp), %rdx
callq 0x1d850
testb $0x1, %al
jne 0x47db8
jmp 0x47dc1
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x47dc3
movq -0x178(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x49200
movq -0x18(%rbp), %rax
movq %rax, -0x188(%rbp)
movslq -0x2c(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x491e0
movq -0x188(%rbp), %rsi
movq %rax, %rdx
leaq -0xb8(%rbp), %rdi
callq 0x47af0
testb $0x1, %al
jne 0x47e18
jmp 0x47e6a
movslq -0x2c(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x491e0
movq -0x178(%rbp), %rdi
movq %rax, %rsi
movsd 0x791c3(%rip), %xmm0    # 0xc1008
movsd %xmm0, -0xd0(%rbp)
leaq -0xd0(%rbp), %rdx
callq 0x1d850
testb $0x1, %al
jne 0x47e5f
jmp 0x47e68
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x47e6a
movq -0x178(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x49260
movq -0x18(%rbp), %rax
movq %rax, -0x190(%rbp)
movslq -0x2c(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x491e0
movq -0x190(%rbp), %rsi
movq %rax, %rdx
leaq -0xe8(%rbp), %rdi
callq 0x47af0
testb $0x1, %al
jne 0x47ebf
jmp 0x47f11
movslq -0x2c(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x491e0
movq -0x178(%rbp), %rdi
movq %rax, %rsi
movsd 0x7911c(%rip), %xmm0    # 0xc1008
movsd %xmm0, -0x100(%rbp)
leaq -0x100(%rbp), %rdx
callq 0x1d850
testb $0x1, %al
jne 0x47f06
jmp 0x47f0f
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x47f11
movq -0x178(%rbp), %rsi
leaq -0x118(%rbp), %rdi
callq 0x492c0
movq -0x18(%rbp), %rax
movq %rax, -0x198(%rbp)
movslq -0x2c(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x491e0
movq -0x198(%rbp), %rsi
movq %rax, %rdx
leaq -0x118(%rbp), %rdi
callq 0x47af0
testb $0x1, %al
jne 0x47f66
jmp 0x47fb8
movslq -0x2c(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x491e0
movq -0x178(%rbp), %rdi
movq %rax, %rsi
movsd 0x79075(%rip), %xmm0    # 0xc1008
movsd %xmm0, -0x130(%rbp)
leaq -0x130(%rbp), %rdx
callq 0x1d850
testb $0x1, %al
jne 0x47fad
jmp 0x47fb6
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x47fb8
cmpl $0x0, -0x2c(%rbp)
jne 0x47fc7
movb $0x0, -0x1(%rbp)
jmp 0x4813f
cmpl $0x1, -0x2c(%rbp)
jne 0x48006
movq $0x0, -0x138(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x491e0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x4813f
cmpl $0x2, -0x2c(%rbp)
jl 0x48137
movl $0x0, -0x13c(%rbp)
movq $0x0, -0x150(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x491e0
movq %rax, -0x1a0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x32c70
movq -0x1a0(%rbp), %rdi
movq %rax, %rsi
callq 0x49040
movsd %xmm0, -0x148(%rbp)
movl $0x1, -0x154(%rbp)
movl -0x154(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x480fe
movslq -0x154(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x491e0
movq %rax, -0x1a8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x32c70
movq -0x1a8(%rbp), %rdi
movq %rax, %rsi
callq 0x49040
movsd %xmm0, -0x160(%rbp)
movsd -0x160(%rbp), %xmm0
ucomisd -0x148(%rbp), %xmm0
jbe 0x480e8
movl -0x154(%rbp), %eax
movl %eax, -0x13c(%rbp)
movsd -0x160(%rbp), %xmm0
movsd %xmm0, -0x148(%rbp)
jmp 0x480ea
movl -0x154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
jmp 0x48066
movslq -0x13c(%rbp), %rax
movq %rax, -0x170(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x491e0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x4813f
jmp 0x48139
jmp 0x4813b
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
nopl (%rax)