def fitLine(pts):
 n = len(pts)
 if (n < 1):
  return ((0, 0), (0, 0))
 a = np.zeros(((n - 1), 2))
 for i in range((n - 1)):
  v = (pts[i] - pts[(i + 1)])
  a[i] = (v / np.linalg.norm(v))
 direction = np.mean(a[1:(-1)], axis=0)
 start = np.mean(pts[1:(-1)], axis=0)
 return (start, (start + direction))

def fitLine ( pts ) : n = len ( pts ) if ( n < 1 ) : return ( ( 0 , 0 ) , ( 0 , 0 ) ) a = np . zeros ( ( ( n - 1 ) , 2 ) ) for i in range ( ( n - 1 ) ) : v = ( pts [ i ] - pts [ ( i + 1 ) ] ) a [ i ] = ( v / np . linalg . norm ( v ) ) direction = np . mean ( a [ 1 : ( - 1 ) ] , axis = 0 ) start = np . mean ( pts [ 1 : ( - 1 ) ] , axis = 0 ) return ( start , ( start + direction ) )
returns a start vector and direction vector assumes points segments that already form a somewhat smooth line .