Exemplo n.º 1
0
def draw_data(points=[], lines=[]):

    #points = shift_points( points )
    line_print(lines)
    lines = shift_lines(lines, min_max_points(points)[:2])
    points = shift_points(points)
    line_print(lines)

    im = svgwrite.drawing.Drawing()
    for point in points:
        im.add( im.circle(center = point,\
                          r      = 5,\
                          fill   = 'blue'))
    #im.add( im.circle(center = find_right( points ),\
    #                  r      = 5,\
    #                  fill   = 'red'))


#
#    #im.add( im.circle(center = points[ smallest_angle( find_right(points), points) ],\
#    #                  r      = 5,\
#                  fill   = 'purple'))

    for line in lines:
        #print line
        im.add( im.line(start  = line[:2],\
                        end    = line[2:],\
                        stroke = 'green'))

    im.saveas('img/data.svg')
    return
Exemplo n.º 2
0
def draw_data(points=[], lines=[]):

    # points = shift_points( points )
    line_print(lines)
    lines = shift_lines(lines, min_max_points(points)[:2])
    points = shift_points(points)
    line_print(lines)

    im = svgwrite.drawing.Drawing()
    for point in points:
        im.add(im.circle(center=point, r=5, fill="blue"))
    # im.add( im.circle(center = find_right( points ),\
    #                  r      = 5,\
    #                  fill   = 'red'))
    #
    #    #im.add( im.circle(center = points[ smallest_angle( find_right(points), points) ],\
    #    #                  r      = 5,\
    #                  fill   = 'purple'))

    for line in lines:
        # print line
        im.add(im.line(start=line[:2], end=line[2:], stroke="green"))

    im.saveas("img/data.svg")
    return
Exemplo n.º 3
0
def drawIrregularPolygon(points, filename=''):

    a, b, size_x, size_y = min_max_points(points)

    size_x += a
    size_y += b
    im      = Image.new("RGB", (size_x, size_y), (255,255,255))
    pixels  = irregularPolygon( points, size_x, size_y)

    for pixel in pixels:
        im.putpixel( pixel, (0,0,0) )

    if filename:
        im.save('img/'+ filename +'.png')
    else:
        im.show()

    return
Exemplo n.º 4
0
def drawIrregularPolygon(points, filename=''):

    a, b, size_x, size_y = min_max_points(points)

    size_x += a
    size_y += b
    im = Image.new("RGB", (size_x, size_y), (255, 255, 255))
    pixels = irregularPolygon(points, size_x, size_y)

    for pixel in pixels:
        im.putpixel(pixel, (0, 0, 0))

    if filename:
        im.save('img/' + filename + '.png')
    else:
        im.show()

    return
Exemplo n.º 5
0
def k_means(data, k, sorted_centers=False):

    data = lists_to_pairs(data)
    if sorted_centers:
        data = [[x[0], x[1], dist([0, 0], [x[0], x[1]])] for x in data]
        data.sort(key=operator.itemgetter(2))
        n = len(data) - 1
        tmp_data_A = data[::]
        centers = []
        for i in xrange(k):
            tmp_data_B = tmp_data_A[:n / k]
            centers.append(random.choice(tmp_data_B))
            tmp_data_A = tmp_data_A[n / k:]
    else:
        anchor = find_center(data)
        dif = max(min_max_points(data)[2:]) / k**2
        centers = []
        for i in xrange(k):
            centers.append(
                list(anchor + array([uniform(-dif, dif),
                                     uniform(-dif, dif)])))

    classes = [[] for i in xrange(k)]
    tmp_class = []
    all_centers = list(centers)
    iteration = 0

    while tmp_class != classes:
        tmp_class = list(classes)
        classes = [[] for i in xrange(k)]

        for point in data:
            classes[nearest_center(centers, point)].append(point)

        for i, center in enumerate(centers):
            centers[i] = find_center(classes[i], center)

        iteration += 1
        all_centers.extend(centers)

    return classes, all_centers
Exemplo n.º 6
0
def k_means(data, k, sorted_centers=False):

    data        = lists_to_pairs( data )
    if sorted_centers:
        data = [[x[0], x[1], dist([0,0], [x[0],x[1]])] for x in data]
        data.sort(key=operator.itemgetter(2))
        n = len( data ) - 1
        tmp_data_A = data[::]
        centers = []
        for i in xrange(k):
            tmp_data_B = tmp_data_A[:n / k]
            centers.append( random.choice( tmp_data_B ) )
            tmp_data_A = tmp_data_A[n / k:]
    else:        
        anchor      = find_center( data )
        dif         = max( min_max_points( data )[2:]) / k**2
        centers     = []
        for i in xrange(k):
            centers.append(list(anchor + array([uniform(-dif, dif), uniform(-dif, dif)])))

    classes     = [ [] for i in xrange(k) ]
    tmp_class   = []
    all_centers = list(centers)
    iteration   = 0

    while tmp_class != classes:
        tmp_class = list(classes)
        classes   = [ [] for i in xrange(k) ]

        for point in data:
            classes[ nearest_center(centers, point) ].append( point )

        for i, center in enumerate(centers):
            centers[ i ] = find_center( classes[ i ], center )

        iteration += 1
        all_centers.extend( centers )

    return classes, all_centers