public bool IsEqual(IClusterableItem item) { bool b1 = width == item.Width; bool b2 = height == item.Height; bool b3 = red == item.Red(); bool b4 = green == item.Green(); bool b5 = blue == item.Blue(); return (b1 && b2 && b3 && b4 && b5); }
public Dictionary<string, List<ArtImage>> Cluster() { IClusterableItem[] clusterPoints = new IClusterableItem[CLUSTER_COUNT]; for (int i = 0; i < CLUSTER_COUNT; i++) { clusterPoints[i] = ClusterPoint.getRandomClusterPoint(); } int loopCount = 0; bool ChangeOccurred = false; do { ChangeOccurred = false; foreach (ArtImage art_image in imagesToCluster) { if (art_image != null) { double bestDistance = 10000; string clusterName = "cluster0"; for (int i = 0; i < CLUSTER_COUNT; i++) { double currentDistance = getDistance(clusterPoints[i], art_image); if (currentDistance <= bestDistance) { bestDistance = currentDistance; clusterName = "cluster" + (i + 1); } } clusters[clusterName].Add(art_image); } } IClusterableItem[] newClusterPoints = new IClusterableItem[CLUSTER_COUNT]; for (int i = 0; i < CLUSTER_COUNT; i++) { String clusterNum = "cluster" + (i+1); newClusterPoints[i] = getAverageClusterPointFromArtImageList(clusters[clusterNum]); } for (int i = 0; i < CLUSTER_COUNT; i++) { if (!newClusterPoints[i].IsEqual(clusterPoints[i])) { ChangeOccurred = true; clusterPoints[i] = newClusterPoints[i]; } } loopCount++; if (ChangeOccurred || loopCount < 200) { clusters = new Dictionary<string, List<ArtImage>>(); CreateClusters(); } } while (ChangeOccurred || loopCount < 200); return clusters; }
public double getDistance(IClusterableItem image1, IClusterableItem image2) { double distance = 0; double heightDistance = Math.Pow((image2.Height - image1.Height), 2); double widthDistance = Math.Pow((image2.Width - image1.Width), 2); double redDistance = Math.Pow((image2.Red() - image1.Red()), 2); double greenDistance = Math.Pow((image2.Green() - image1.Green()), 2); double blueDistance = Math.Pow((image2.Blue() - image1.Blue()), 2); distance = Math.Sqrt((heightDistance + widthDistance + redDistance + greenDistance + blueDistance)); return distance; }