def _build_fit(p, q, fitgeom):
    fit_matrix = np.vstack((p[:2], q[:2]))
    det = ((p[0] * q[1]) - (p[1] * q[0]))
    sdet = np.sign(det)
    proper = (sdet >= 0)
    wfit = fit_matrix.copy()
    skew = 0.0
    if (fitgeom == 'shift'):
        fit = {'shift': np.array([p[2], q[2]], dtype=np.double), 'shift_ld': np.array([p[2], q[2]], dtype=np.longdouble), 'matrix': np.array(fit_matrix, dtype=np.double), 'matrix_ld': np.array(fit_matrix, dtype=np.longdouble), 'proper_rot': 0.0, 'rot': (0.0, 0.0), '<rot>': 0.0, 'scale': (1.0, 1.0), '<scale>': 1.0, 'skew': 0.0, 'proper': proper, 'fitgeom': 'shift'}
        return fit
    s = np.sqrt(np.abs(det))
    if (fitgeom == 'general'):
        (sx, sy) = np.sqrt(((p[:2] ** 2) + (q[:2] ** 2)))
    else:
        sx = s
        sy = s
    wfit[:, 0] /= sx
    wfit[:, 1] /= sy
    prop_rot = np.rad2deg(np.arctan2((wfit[(0, 1)] - (sdet * wfit[(1, 0)])), (wfit[(0, 0)] + (sdet * wfit[(1, 1)]))))
    if (proper and (fitgeom == 'rscale')):
        rotx = prop_rot
        roty = prop_rot
        rot = prop_rot
    else:
        rotx = np.rad2deg(np.arctan2((- wfit[(1, 0)]), wfit[(0, 0)]))
        roty = np.rad2deg(np.arctan2(wfit[(0, 1)], wfit[(1, 1)]))
        rot = (0.5 * (rotx + roty))
        skew = (np.mod(((roty - rotx) - 180.0), 360.0) - 180.0)
    fit = {'shift': np.array([p[2], q[2]], dtype=np.double), 'shift_ld': np.array([p[2], q[2]], dtype=np.longdouble), 'matrix': np.array(fit_matrix, dtype=np.double), 'matrix_ld': np.array(fit_matrix, dtype=np.longdouble), 'proper_rot': float(prop_rot), 'rot': (float(rotx), float(roty)), '<rot>': float(rot), 'scale': (float(sx), float(sy)), '<scale>': float(s), 'skew': float(skew), 'proper': proper, 'fitgeom': fitgeom}
    return fit
def _build_fit(p, q, fitgeom):
    fit_matrix = np.vstack((p[:2], q[:2]))
    det = ((p[0] * q[1]) - (p[1] * q[0]))
    sdet = np.sign(det)
    proper = (sdet >= 0)
    wfit = fit_matrix.copy()
    skew = 0.0
    if (fitgeom == 'shift'):
        fit = {'shift': np.array([p[2], q[2]], dtype=np.double), 'shift_ld': np.array([p[2], q[2]], dtype=np.longdouble), 'matrix': np.array(fit_matrix, dtype=np.double), 'matrix_ld': np.array(fit_matrix, dtype=np.longdouble), 'proper_rot': 0.0, 'rot': (0.0, 0.0), '<rot>': 0.0, 'scale': (1.0, 1.0), '<scale>': 1.0, 'skew': 0.0, 'proper': proper, 'fitgeom': 'shift'}
        return fit
    s = np.sqrt(np.abs(det))
    if (fitgeom == 'general'):
        (sx, sy) = np.sqrt(((p[:2] ** 2) + (q[:2] ** 2)))
    else:
        sx = s
        sy = s
    wfit[:, 0] /= sx
    wfit[:, 1] /= sy
    prop_rot = np.rad2deg(np.arctan2((wfit[(0, 1)] - (sdet * wfit[(1, 0)])), (wfit[(0, 0)] + (sdet * wfit[(1, 1)]))))
    if (proper and (fitgeom == 'rscale')):
        rotx = prop_rot
        roty = prop_rot
        rot = prop_rot
    else:
        rotx = np.rad2deg(np.arctan2((- wfit[(1, 0)]), wfit[(0, 0)]))
        roty = np.rad2deg(np.arctan2(wfit[(0, 1)], wfit[(1, 1)]))
        rot = (0.5 * (rotx + roty))
        skew = (np.mod(((roty - rotx) - 180.0), 360.0) - 180.0)
    fit = {'shift': np.array([p[2], q[2]], dtype=np.double), 'shift_ld': np.array([p[2], q[2]], dtype=np.longdouble), 'matrix': np.array(fit_matrix, dtype=np.double), 'matrix_ld': np.array(fit_matrix, dtype=np.longdouble), 'proper_rot': float(prop_rot), 'rot': (float(rotx), float(roty)), '<rot>': float(rot), 'scale': (float(sx), float(sy)), '<scale>': float(s), 'skew': float(skew), 'proper': proper, 'fitgeom': fitgeom}
    return fit