#!/usr/bin/env python

# These are the key vertices of "The Cuttle"
VERTICES = [
    # T
    [
        (15, 1),
        (15, 51)
    ],
    [
        ( 4, 51),
        (30, 51)
    ],
    # h
    [
        (35, 54),
        (35, 2)
    ],
    [
        (35, 23),
        (40, 32),
        (49, 33),
        (49,  2)
    ],
    # e
    [
        (60, 12),
        (77, 26),
        (77, 33),
        (67, 37),
        (59, 28),
        (59, 14),
        (63,  4),
        (69,  1),
        (75,  3),
        (79,  9)
    ],
    # C
    [
        (122, 11),
        (115,  3),
        (106,  3),
        ( 99, 13),
        ( 99, 33),
        (106, 46),
        (114, 52),
        (121, 49)
    ],
    # u
    [
        (131, 33),
        (131,  4),
        (136,  1),
        (142,  5),
        (145,  1),
        (142,  5),
        (142, 36)
    ],
    # t
    [
        (151, 35),
        (167, 35)
    ],
    [
        (159, 48),
        (159,  1)
    ],
    # t
    [
        (182, 48),
        (182,  1)
    ],
    [
        (174, 35),
        (189, 35)
    ],
    # l
    [
        (199, 57),
        (199,  1)
    ],
    # e
    [
        (209 , 12),
        (226 , 26),
        (226 , 33),
        (216 , 37),
        (210 , 28),
        (210 , 14),
        (212 ,  4),
        (218 ,  1),
        (224 ,  3),
        (228 ,  9)
    ]
]

LINE_STEP = 3
ANGLE_STEP = 3

points = []


def insert_point(x, y):
    points.append((x, y))


def interpolate(x1, y1, x2, y2):


    if x1 == x2:
        for y in range(y1, y2, LINE_STEP if y2 > y1 else -LINE_STEP):
            insert_point(x1, y)
    elif y1 == y2:
        for x in range(x1, x2, LINE_STEP if x2 > x1 else -LINE_STEP):
            insert_point(x, y1)
    else:
        for x in range(x1, x2, ANGLE_STEP if x2 > x1 else -ANGLE_STEP):
            y = int(y1 + (y2 - y1)/(float(x2) - x1)  * (x - x1))
            insert_point(x, y)


def outputPreamble():
    print (
        '// auto-generated by make_data.py\n\n'
        '#ifndef Points_h\n'
        '#define Points_h\n\n'
        '// x,y coordinates for plotting\n'
        'const static byte VERTICES[][2]  = {'
    )


def outputPoints():
    data = '  {}'.format(
        ',\n  '.join([
            '{{{}, {}}}'.format(point[0], point[1])
            for point in points
        ])
    )
    print data


def outputEpilogue():
    print (
        '};\n\n'
        'int NUM_POINTS = sizeof(VERTICES) / 2;\n\n'
        '#endif'
    )


def build():
    for segment in VERTICES:
        for p in range(len(segment) - 1):
            interpolate(
                segment[p][0],
                segment[p][1],
                segment[p + 1][0],
                segment[p + 1][1]
            )
    outputPreamble()
    outputPoints()
    outputEpilogue()


if __name__ == '__main__':
    build()
