classpoint{floatx, y; intK; Point (floatX1,floaty1) {x=x1;y=Y1; }}classtype{intK; intNum=0; floatX_old, Y_old; floatx_new, y_new; intR = 0, g = 0, b = 0;}intnum = 500;floatDX = 1, dy = 1;//intK = 3;intK_max = 5;intLen = 0;intIritation = 10000;Booleanflag=true; Point [] Points=NewPoint [num]; Type [] Types=NewType [K_max];voidNeark () { for(inti = 0; i < Len; i++){ floatMin = width *height; intindex =-1; for(intj = 0; J < K; J + +){ floatR =Dist (points[i].x,points[i].y,types[j].x_old,types[j].y_old); if(R <min) {min=R; Index=J; } } if(Index = =-1) Print ("What??"); Else{POINTS[I].K=index; Types[index].num++; } }}voidRenew () { for(inti = 0; i < Len; i++){ intu=POINTS[I].K; println ("Now" +u); Types[u].x_new+=points[i].x; Types[u].y_new+=points[i].y; } for(inti = 0; I < K; i++) {println (i+ "num" +types[i].num); Types[i].x_new=types[i].x_new/Types[i].num; Types[i].y_new=types[i].y_new/Types[i].num; println ("Xnew" +types[i].x_new, "ynew" +types[i].y_new); Types[i].num=1; if(ABS (Types[i].x_new-types[i].x_old) >DX) {Flag=true; Types[i].x_old=types[i].x_new; } if(ABS (Types[i].y_new-types[i].y_old) >dy) {Flag=true; Types[i].y_old=types[i].y_new; } }}voidorigin () { for(inti = 0; I < K; i++){ intU =int(Random (0,len-1)); intv =int(Random (0,255)); Type T=NewType (); T.x_old=points[u].x; T.y_old=points[u].y; T.R=v; V=int(Random (0,255)); T.G=v; V=int(Random (0,255)); T.B=v; Types[i]=T; } //print (types.length);}voidmousepressed () {point P=NewPoint (MouseX, Mousey); Points[len]=p;//print (points[len].x);Rect (mousex,mousey,5,5); Len++;}voidSetup () {Size (500,500); Background (255,255,255);}voidDraw () {if(keypressed) {if(Key = = ' s ' | | key = = ' s ')){ intIre=0; Background (255,255,255); Origin (); Neark (); Renew (); for(inti = 0; I <iritation; i++) {IRE++; if(flag) {flag=false; Neark (); Renew (); } Else Break; } for(inti = 0; i < Len; i++){ intu=POINTS[I].K; Fill (types[u].r,types[u].g,types[u].b); Rect (POINTS[I].X,POINTS[I].Y,7,7); TextSize (20); Text (u,points[i].x+9,points[i].y+10); } Print ("IRE" +IRE); } }}voidkeypressed () {intP=0; if(Key = = ' L ' | | key = = ' L ') &&flag = =true&& p<iritation) { //Boolean flag=false; intIre=0; Background (255,255,255); P++; Flag=false; Origin (); Neark (); Renew (); for(intj=0; j<k;j++) {Fill (255); Rect (Types[j].x_old,types[j].y_old,10,10); } for(inti = 0; i < Len; i++){ intu=POINTS[I].K; Fill (types[u].r,types[u].g,types[u].b); Rect (POINTS[I].X,POINTS[I].Y,7,7); TextSize (20); Text (u,points[i].x+9,points[i].y+10); } for(inti=0; i<k;i++) {Fill (255); Rect (Types[i].x_old,types[i].y_old,10,10); } Print ("IRE" +IRE); }}
The language of this Code is processing: Website link https://processing.org/reference/
Because it is a beta version, it has potential.
The K-mean algorithm is implemented. The initial centroid is randomly generated. The number is fixed, the node wants to how many points.
Operation algorithm:
The K-mean algorithm is implemented by processing