Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

convert_svg_matrix

def delta_transform_point(matrix, point):
    dx = point['x'] * matrix['a'] + point['y'] * matrix['c'] + 0
    dy = point['x'] * matrix['b'] + point['y'] * matrix['d'] + 0

    return {'x': dx, 'y': dy}


def decompose_matrix(matrix):
    px = delta_transform_point(matrix, {'x': 0, 'y': 1})
    py = delta_transform_point(matrix, {'x': 1, 'y': 0})

    skew_x = ((180 / math.pi) * math.atan2(px['y'], px['x']) - 90)
    skew_y = ((180 / math.pi) * math.atan2(py['y'], py['x']))

    values = {
        'translate_x': matrix['e'],
        'translate_y': matrix['f'],
        'scale_x': math.sqrt(matrix['a'] * matrix['a'] + matrix['b'] * matrix['b']),
        'scale_y': math.sqrt(matrix['c'] * matrix['c'] + matrix['d'] * matrix['d']),
        'skew_x': skew_x,
        'skew_y': skew_y,
        'rotation': skew_x
    }

    return 'translate({}, {}) scale({}, {}) rotate({} {} {})'.format(
        values['translate_x'],
        values['translate_y'],
        values['scale_x'],
        values['scale_y'],
        values['skew_x'],
        values['skew_y'],
        values['rotation'])


print(decompose_matrix({'a': 1, 'b': 0, 'c': 0,
                        'd': -1, 'e': 0, 'f': 1556}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment