/*
 * Represents an abstraction for a data point in two dimensional space
 *
 * Manas Somaiya
 * Computer and Information Science and Engineering
 * University of Florida
 *
 * Created: October 29, 2003
 * Last updated: October 30, 2003
 *
 */
package medusa.georgios.ClusteringAlgorithms;

/**
 * Represents an abstraction for a data point in two dimensional space
 * @author	Manas Somaiya	mhs@cise.ufl.edu
 */
public class KMeansPoint {

    /** Label */
    private String label;
    private double[] coordinates;
    /** Value in dimension x */
    private double x;
    /** Value in dimension y */
    private double y;
    /** Assigned cluster */
    private int clusterNumber;

    /**
     * Creates a new instance of data point
     *
     * @param	_x	value in dimension x
     * @param	_y	value in dimension y
     */
    public KMeansPoint(String label, double _x, double _y) {
        this.label = label;
        coordinates = new double[2];
        coordinates[0] = _x;
        coordinates[1] = _y;

        this.clusterNumber = 0;
    } // end of kMeansPoint()

    public KMeansPoint(String label, double[] coordinates) {
        this.label = label;
        this.coordinates = coordinates;

        this.clusterNumber = 0;
    } // end of kMeansPoint()

    public double[] getCoordinates() {
        return coordinates;
    }

    public double getCoordinate(int i) {
        return coordinates[i];
    }
    
    public int size(){
        return coordinates.length;
    }

    public void setCoordinates(double[] coordinates) {
        this.coordinates = coordinates;
    }

    /**
     * Assigns the data point to a cluster
     *
     * @param	_clusterNumber	the cluster to which this data point is to be assigned
     */
    public void assignToCluster(int _clusterNumber) {

        this.clusterNumber = _clusterNumber;

    } // end of assignToCluster()

    /**
     * Returns the cluster to which the data point belongs
     *
     * @return	the cluster number to which the data point belongs
     */
    public int getClusterNumber() {

        return this.clusterNumber;

    } // end of getClusterNumber()

    /**
     * Returns the value of data point in x dimension
     *
     * @return	the value in x dimension
     */
    public double getX() {

        return this.x;

    } // end of getX()

    /**
     * Returns the value of data point in y dimension
     *
     * @return	the value in y dimension
     */
    public double getY() {

        return this.y;

    } // end of getY()

    /**
     * Returns the distance between two data points
     *
     * @param	dp1 	the first data point
     * @param	dp2 	the second data point
     * @return	the distance between the two data points
     */
    public static double distance(KMeansPoint dp1, KMeansPoint dp2) {
     //   System.out.println("Getting distance between "+dp1.getLabel()+" and "+dp2.getLabel());
        double result = 0;
        for (int i = 0; i < dp1.getCoordinates().length; i++) {
            result += (dp1.getCoordinate(i) - dp2.getCoordinate(i)) * (dp1.getCoordinate(i) - dp2.getCoordinate(i));
        }
      //  System.out.println("---- "+Math.sqrt(result));
        return Math.sqrt(result);

    } // end of distance()

    /**
     * Returns a string representation of this kMeansPoint
     *
     * @return	a string representation of this data point
     */
    public String toString() {

        //return "(" + this.x + "," + this.y + ")[" + this.clusterNumber + "]";
        return this.getLabel() + "\t" + Integer.toString(this.clusterNumber);

    } // end of toString()

    public String toDataString() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < getCoordinates().length; i++) {
            sb.append(getCoordinate(i));
            sb.append(" ");
        }
        //return "(" + this.x + "," + this.y + ")[" + this.clusterNumber + "]";
        return sb.toString();

    } // end of toString()

    /**
     * Main method -- to test the kMeansPoint class
     *
     * @param	args	command line arguments
     */
    public static void main(String[] args) {

        KMeansPoint dp1 = new KMeansPoint("weng", -0.3f, -0.4f);
        KMeansPoint dp2 = new KMeansPoint("foo", 0.2f, 0.4f);
        System.out.println(KMeansPoint.distance(dp1, dp2));
        System.out.println(dp1.getX());
        System.out.println(dp2.getY());
        dp1.assignToCluster(7);
        System.out.println(dp1.getClusterNumber());
        dp1.assignToCluster(17);
        System.out.println(dp1.getClusterNumber());
        System.out.println(dp2.getClusterNumber());
        System.out.println(dp1);

    } // end of main()

    public String getLabel() {
        return label;
    }
} // end of class
 

Ads help maintain this website.