// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Polygon.cpp
// Function: gaps::R2Polygon::Tangent(int, double) const
R2Line R2Polygon::
Tangent(int k, RNLength radius) const
{
  // Just checking
  if (npoints < 3) return R2null_line;

#if 0
  // Find first point
  R2Point *pointA = &points[(k - 1 + npoints) % npoints];
  if (radius > 0) {
    R2Point *prev = pointA;
    RNLength distance = R2Distance(*pointA, points[k]);
    if (distance < radius) {
      for (int i = 2;  i < npoints/3; i++) {
        pointA = &points[(k - i + npoints) % npoints];
        distance += R2Distance(*pointA, *prev);
        if (distance >= radius) break;
        prev = pointA;
      }
    }
  }

  // Find second point
  R2Point *pointB = &points[(k + 1) % npoints];
  if (radius > 0) {
    R2Point *prev = pointB;
    RNLength distance = R2Distance(*pointB, points[k]);
    if (distance < radius) {
      for (int i = 2;  i < npoints/3; i++) {
        pointB = &points[(k + i) % npoints];
        distance += R2Distance(*pointB, *prev);
        if (distance >= radius) break;
        prev = pointB;
      }
    }
  }

  // Compute vector along line
  R2Vector vector = *pointB - *pointA;
  vector.Normalize();
#else
  // Create array with points of patch within radius
  RNArray<R2Point *> patch;
  patch.Insert(&points[k]);

  // Add points searching one way
  RNLength distance = 0;
  R2Point *prev = &points[k];
  for (int i = 1; i < npoints/3; i++) {
    R2Point *current = &points[(k - i + npoints) % npoints];
    patch.Insert(current);
    distance += R2Distance(*current, *prev);
    if (distance >= radius) break;
    prev = current;
  }

  // Add points searching other way
  distance = 0;
  prev = &points[k];
  for (int i = 1; i < npoints/3; i++) {
    R2Point *current = &points[(k + i) % npoints];
    patch.Insert(current);
    distance += R2Distance(*current, *prev);
    if (distance >= radius) break;
    prev = current;
  }

  // Compute best fitting line to points in patch
  R2Point centroid = R2Centroid(patch);
  R2Diad diad = R2PrincipleAxes(centroid, patch);
  R2Vector vector = diad.Axis(0);

  // Flip vector if facing wrong direction
  R2Point& p1 = points[(k - 1 + npoints) % npoints];
  R2Point& p2 = points[(k + 1) % npoints];
  if (vector.Dot(p2 - p1) < 0) vector.Flip();
#endif

  // Flip if polygon is clockwise
  if (clockwise) vector = -vector;

  // Return tangent line
  return R2Line(points[k], vector);
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x60(%rsp)
movq %rdi, %rax
movq %rax, 0x68(%rsp)
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movl %edx, 0x144(%rsp)
movsd %xmm0, 0x138(%rsp)
movq 0x148(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpl $0x3, 0x10(%rax)
jge 0xbb0dd
movq 0x60(%rsp), %rdi
leaq 0x4bfc7d(%rip), %rsi    # 0x57ad50
callq 0xc2740
jmp 0xbb522
leaq 0x128(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x3a060
movq 0x70(%rsp), %rax
movq 0x58(%rsp), %rdi
movq 0x8(%rax), %rsi
movslq 0x144(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x3a090
jmp 0xbb113
movq 0x70(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x110(%rsp)
movq 0x8(%rax), %rax
movslq 0x144(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x108(%rsp)
movl $0x1, 0x104(%rsp)
movq 0x70(%rsp), %rax
movl 0x104(%rsp), %ecx
movl %ecx, 0x54(%rsp)
movl 0x10(%rax), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0xbb25e
movq 0x70(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, 0x48(%rsp)
movl 0x144(%rsp), %eax
movl 0x104(%rsp), %edx
subl %edx, %eax
movl 0x10(%rcx), %ecx
addl %ecx, %eax
cltd
idivl %ecx
movq 0x48(%rsp), %rax
movslq %edx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x3a090
jmp 0xbb1c7
movq 0xf8(%rsp), %rdi
movq 0x108(%rsp), %rsi
callq 0xcb540
movsd %xmm0, 0x40(%rsp)
jmp 0xbb1e4
movsd 0x40(%rsp), %xmm0
addsd 0x110(%rsp), %xmm0
movsd %xmm0, 0x110(%rsp)
movsd 0x110(%rsp), %xmm0
ucomisd 0x138(%rsp), %xmm0
jb 0xbb238
jmp 0xbb25e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x3a3d0
jmp 0xbb52f
movq 0xf8(%rsp), %rax
movq %rax, 0x108(%rsp)
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0xbb14a
movq 0x70(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x110(%rsp)
movq 0x8(%rax), %rax
movslq 0x144(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x108(%rsp)
movl $0x1, 0xf4(%rsp)
movq 0x70(%rsp), %rax
movl 0xf4(%rsp), %ecx
movl %ecx, 0x3c(%rsp)
movl 0x10(%rax), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0xbb381
movq 0x70(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, 0x30(%rsp)
movl 0x144(%rsp), %eax
movl 0xf4(%rsp), %edx
addl %edx, %eax
movl 0x10(%rcx), %ecx
cltd
idivl %ecx
movq 0x30(%rsp), %rax
movslq %edx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x3a090
jmp 0xbb310
movq 0xe8(%rsp), %rdi
movq 0x108(%rsp), %rsi
callq 0xcb540
movsd %xmm0, 0x28(%rsp)
jmp 0xbb32d
movsd 0x28(%rsp), %xmm0
addsd 0x110(%rsp), %xmm0
movsd %xmm0, 0x110(%rsp)
movsd 0x110(%rsp), %xmm0
ucomisd 0x138(%rsp), %xmm0
jb 0xbb35b
jmp 0xbb381
movq 0xe8(%rsp), %rax
movq %rax, 0x108(%rsp)
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0xbb295
xorl %eax, %eax
movl %eax, %edx
leaq 0xd8(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xcdf80
jmp 0xbb39c
xorl %eax, %eax
movl %eax, %r8d
leaq 0xb8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
leaq 0x128(%rsp), %rdx
movq %r8, %rcx
callq 0xce0a0
jmp 0xbb3c3
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0xbd250
movq %rax, 0x20(%rsp)
jmp 0xbb3d9
movq 0x20(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0xc3c60
jmp 0xbb3ed
movq 0x70(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, 0x10(%rsp)
movl 0x144(%rsp), %edx
movl 0x10(%rcx), %esi
movl %edx, %eax
movl %esi, %edx
leal -0x1(%rax,%rdx), %eax
cltd
idivl %esi
movq 0x10(%rsp), %rax
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rax, 0xa0(%rsp)
movq 0x8(%rcx), %rax
movq %rax, 0x18(%rsp)
movl 0x144(%rsp), %eax
incl %eax
movl 0x10(%rcx), %ecx
cltd
idivl %ecx
movq 0x18(%rsp), %rax
movslq %edx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x88(%rsp), %rdi
callq 0xc3830
jmp 0xbb475
leaq 0xa8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0xc3f40
movsd %xmm0, 0x8(%rsp)
jmp 0xbb492
movsd 0x8(%rsp), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0xbb4b2
leaq 0xa8(%rsp), %rdi
callq 0xc40f0
jmp 0xbb4b0
jmp 0xbb4b2
movq 0x70(%rsp), %rax
cmpl $0x0, 0x40(%rax)
je 0xbb4e7
leaq 0x78(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xc4700
jmp 0xbb4d1
leaq 0xa8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xc4350
jmp 0xbb4e5
jmp 0xbb4e7
movq 0x60(%rsp), %rdi
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rsi
movslq 0x144(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0xa8(%rsp), %rdx
xorl %ecx, %ecx
callq 0xc2890
jmp 0xbb515
leaq 0x128(%rsp), %rdi
callq 0x3a3d0
movq 0x68(%rsp), %rax
addq $0x158, %rsp            # imm = 0x158
retq
movq 0x120(%rsp), %rdi
callq 0xf890
nopl (%rax)