// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movsd %xmm0, 0x28(%rsp)
movq %rdi, %rbx
cmpl $0x2, 0x10(%rsi)
jg 0x5b8d0
leaq 0x4818fd(%rip), %rsi    # 0x4dd1c0
movq %rbx, %rdi
callq 0x5f050
jmp 0x5bad6
movl %edx, %r12d
movq %rsi, %r14
movq %rbx, 0x40(%rsp)
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x6630a
movslq %r12d, %r15
shlq $0x4, %r15
movq 0x8(%r14), %rsi
addq %r15, %rsi
movq %rbx, %rdi
callq 0x22bfc
movq 0x8(%r14), %rbx
movq %r15, 0x18(%rsp)
addq %r15, %rbx
movq %r12, 0x20(%rsp)
decl %r12d
xorpd %xmm0, %xmm0
pushq $0x1
popq %rbp
pushq $0x3
popq %r15
movl 0x10(%r14), %ecx
movl %ecx, %eax
cltd
idivl %r15d
movq 0x8(%r14), %r13
cmpl %eax, %ebp
jge 0x5b97e
leal (%r12,%rcx), %eax
cltd
idivl %ecx
movslq %edx, %rax
shlq $0x4, %rax
addq %rax, %r13
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
movsd %xmm0, (%rsp)
callq 0x22bfc
movq %r13, %rdi
movq %rbx, %rsi
callq 0x63469
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
incl %ebp
decl %r12d
ucomisd 0x28(%rsp), %xmm1
movq %r13, %rbx
movapd %xmm1, %xmm0
jb 0x5b91d
movq 0x8(%r14), %r13
addq 0x18(%rsp), %r13
xorpd %xmm0, %xmm0
pushq $0x1
popq %r12
leaq 0x8(%rsp), %rbp
movl 0x10(%r14), %ecx
movl %ecx, %eax
cltd
idivl %r15d
cmpl %eax, %r12d
jge 0x5b9eb
movq 0x20(%rsp), %rax
addl %r12d, %eax
cltd
idivl %ecx
movslq %edx, %rbx
shlq $0x4, %rbx
addq 0x8(%r14), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
movsd %xmm0, (%rsp)
callq 0x22bfc
movq %rbx, %rdi
movq %r13, %rsi
callq 0x63469
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
incl %r12d
ucomisd 0x28(%rsp), %xmm1
movq %rbx, %r13
movapd %xmm1, %xmm0
jb 0x5b990
leaq 0x58(%rsp), %rdi
leaq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x6451c
leaq 0x68(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x645bf
movq 0x40(%rsp), %rbx
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r12
leaq 0x30(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x5f8be
movq 0x8(%r14), %rdi
movl 0x10(%r14), %esi
leal (%r12,%rsi), %eax
decl %eax
cltd
idivl %esi
movslq %edx, %rcx
shlq $0x4, %rcx
addq %rdi, %rcx
incl %r12d
movl %r12d, %eax
cltd
idivl %esi
movslq %edx, %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
leaq 0x48(%rsp), %rdi
movq %rcx, %rdx
callq 0x5f7c9
leaq 0x30(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x5fa20
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x5ba91
leaq 0x30(%rsp), %rdi
callq 0x5fada
cmpl $0x0, 0x40(%r14)
je 0x5bab6
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5fd59
leaq 0x30(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x5fbfa
addq 0x8(%r14), %r15
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x5f116
leaq 0x8(%rsp), %rdi
callq 0x66378
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5bafb
jmp 0x5bafb
jmp 0x5bafb
jmp 0x5bafb
jmp 0x5bafb
jmp 0x5bafb
jmp 0x5bafb
jmp 0x5bafb
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x66378
movq %rbx, %rdi
callq 0xe810