Public feed #891285

Untitled

public text v1 · immutable
Raw Edit
·published 2008-02-04 14:51 UTC
rendered paste body
public Cluster[] clusterize( Point[] points ) {
    // means is the number of points
    clusters = new ArrayList<Cluster>( means );
    Storer s = new Storer();

    // make clusters at random locations
    for ( int i = 0; i < means; i++ )
        s.add( Cluster( random(), random() ) );

    for ( Point p : points ) {
        Cluster closestCluster = s.closestTo(p);
        // remove cluster from s, because its center is changed
        s.rem( closestCluster );
        // center of the cluster is automatically adjusted when a point is added
        closestCluster.add( p );
        // put the cluster back to s to its new location
        s.add( closestCluster );
    }

    // clusters is a field of type ArrayList<Cluster>
    // this conversion is actually impossible to do this easily, because Storer stores everything as Positioned's, and Java's generics suck
    clusters = s.getAll();

    return clusters;
}