##################################################################
##################################################################
##################################################################

#parse the input into a list of Point objects
points = []
data = open("toy_data.txt", 'r')
for line in data:
	curr_args = list(map(lambda x : float(x), line.split()))
	curr_point = Point(*curr_args)
	points.append(curr_point)

runs_info = []
best_WCSS = -1
for run in range(20):
	centers, cluster_index, WCSS_list = KMeansClustering.k_means_clustering(points, 4, True)
	runs_info.append(WCSS_list)
	curr_WCSS = WCSS_list[-1]
	if best_WCSS == -1 or curr_WCSS < best_WCSS:
		best_cluster_index = cluster_index
		best_WCSS = curr_WCSS

# make first graph - points in clusters graph
x_clusters = [ [] for i in range(4) ] 
y_clusters = [ [] for i in range(4) ] 
for i in range(len(best_cluster_index)):
	x_clusters[best_cluster_index[i]].append(points[i].x)
	y_clusters[best_cluster_index[i]].append(points[i].y)

colors = ["bo", "go", "ro", "yo"]
for i in range(4):
#################################################################################
#################################################################################

# 128 * 128 pixel image
image = Image.open("bird_small.tiff")
pixels = image.load()
# initialize points in 3D space with coordinates as pixel's RGB values
points = []
for x in range(128):
	for y in range(128):
		R, G, B = pixels[x, y]	
		points.append( Point(R, G, B, x, y) )

centers = []
for i in range(16):
	while True:
		RGB = Point(randrange(0, 256), randrange(0, 256), randrange(0, 256), 0, 0)
		if RGB not in centers:
			centers.append(RGB)
			break	

centers, cluster_index = KMeansClustering.k_means_clustering(points, 16, False, True, centers)

for i in range(len(points)):
	pnt = points[i]
	R = centers[cluster_index[i]].R
	G = centers[cluster_index[i]].G
	B = centers[cluster_index[i]].B
	pixels[pnt.x, pnt.y] = (int(R), int(G), int(B))
image.save("output-bird.tiff")