// ------------------------------------------------------------------------ // This program is complementary material for the book: // // Frank Nielsen // // Visual Computing: Geometry, Graphics, and Vision // // ISBN: 1-58450-427-7 // // Charles River Media, Inc. // // // All programs are available at http://www.charlesriver.com/visualcomputing/ // // You may use this program for ACADEMIC and PERSONAL purposes ONLY. // // // The use of this program in a commercial product requires EXPLICITLY // written permission from the author. The author is NOT responsible or // liable for damage or loss that may be caused by the use of this program. // // Copyright (c) 2005. Frank Nielsen. All rights reserved. // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ // File: kmeans.cpp // // Description: LLoyd iterative optimization clustering procedure // At each step the sum of the squared differences decrease // ------------------------------------------------------------------------ #include "stdafx.h" #include "windows.h" #include #include #include using namespace std; #define square(x) ((x)*(x)) struct point2d { public: double x,y; int cluster; // membership }; struct color{public: float r,g,b;}; int k; point2d * centroid; point2d * circumcenter; color* clustercolor; point2d * set; int n; int *nb, *nbc; void init() {int i; n=500; set=new point2d[n]; k=10; centroid=new point2d[k]; clustercolor=new color[k]; nb=new int[k]; nbc=new int[k]; // Uniform distribution for(i=0;i0) {nbc[j]=nbc[j-1]+nb[j];} if (nb[j]>0) {centroid[j].x/=nb[j];centroid[j].y/=nb[j];} cout <<"Class "<