def addWithLeastLength(loops, point, shortestAdditionalLength):
 shortestLoop = None
 shortestPointIndex = None
 for loop in loops:
  if (len(loop) > 2):
   for pointIndex in xrange(len(loop)):
    additionalLength = getAdditionalLoopLength(loop, point, pointIndex)
    if (additionalLength < shortestAdditionalLength):
     shortestAdditionalLength = additionalLength
     shortestLoop = loop
     shortestPointIndex = pointIndex
 if (shortestPointIndex != None):
  afterCenterComplex = shortestLoop[shortestPointIndex]
  afterEndComplex = shortestLoop[((shortestPointIndex + 1) % len(shortestLoop))]
  isInlineAfter = isInline(point, afterCenterComplex, afterEndComplex)
  beforeCenterComplex = shortestLoop[(((shortestPointIndex + len(shortestLoop)) - 1) % len(shortestLoop))]
  beforeEndComplex = shortestLoop[(((shortestPointIndex + len(shortestLoop)) - 2) % len(shortestLoop))]
  isInlineBefore = isInline(point, beforeCenterComplex, beforeEndComplex)
  if (isInlineAfter or isInlineBefore):
   shortestLoop.insert(shortestPointIndex, point)

def addWithLeastLength ( loops , point , shortestAdditionalLength ) : shortestLoop = None shortestPointIndex = None for loop in loops : if ( len ( loop ) > 2 ) : for pointIndex in xrange ( len ( loop ) ) : additionalLength = getAdditionalLoopLength ( loop , point , pointIndex ) if ( additionalLength < shortestAdditionalLength ) : shortestAdditionalLength = additionalLength shortestLoop = loop shortestPointIndex = pointIndex if ( shortestPointIndex != None ) : afterCenterComplex = shortestLoop [ shortestPointIndex ] afterEndComplex = shortestLoop [ ( ( shortestPointIndex + 1 ) % len ( shortestLoop ) ) ] isInlineAfter = isInline ( point , afterCenterComplex , afterEndComplex ) beforeCenterComplex = shortestLoop [ ( ( ( shortestPointIndex + len ( shortestLoop ) ) - 1 ) % len ( shortestLoop ) ) ] beforeEndComplex = shortestLoop [ ( ( ( shortestPointIndex + len ( shortestLoop ) ) - 2 ) % len ( shortestLoop ) ) ] isInlineBefore = isInline ( point , beforeCenterComplex , beforeEndComplex ) if ( isInlineAfter or isInlineBefore ) : shortestLoop . insert ( shortestPointIndex , point )
insert a point into a loop .