📄 point.java
字号:
import java.util.Vector;/*********************************************************** A public class the stores the representation of a single * data point in the database. Currently it stores 2D data.*/public class Point{ double x; // store the x-coordinate double y; // store the y-coordinate /** Constructor * Creates a new Point with the given coordinates * @param xVal Value of x coordinate * @param yVal Value of y coordinate */ public Point(double xVal, double yVal) { x = xVal; y = yVal; } /** Method to return the Euclidean distance between this point and the given point * @param p The point to measure the distance to * @return The euclidean distance */ public double EuclideanDistance(Point p) { return Math.sqrt((p.x - x)*(p.x - x) + (p.y - y)*(p.y -y)); } /** Returns the value of the x-coordinate of this point * @return The value of x coordinate */ public double xVal() { return x; } /** Returns the value of the y-coordinate of this point * @return The value of y coordinate */ public double yVal() { return y; } /** Method to calculate the mean point of a given vector of points * @param clust the Vector of points to find the mean of * @return A point which is the mean of the given vector. Returns (0,0) * if the Vector is empty */ static public Point mean(Vector clust) { double xsum=0.0; double ysum=0.0; int size = clust.size(); if (size == 0) return new Point (0,0); // no points in Vector, return 0,0 // calcuate the sum of x and y coordinates for (int j= 0; j < size; j++) { Point p = (Point) clust.elementAt(j); xsum += p.x; ysum += p.y; } return new Point(xsum/size,ysum/size); } /** Returns a string representation of the point * @return x and y coordinates in a string */ public String toString() { return x + " " + y; } /** Method to return the value of the n-th requested dimension * @param index The dimension to return the value of. Must be 0 or 1 * @return The value of the requested dimension */ public double value(int index) { if (index == 0) return x; else return y; } /** Method to return the number of dimensions in the current point * @param The number of dimensions. Currently is always 2 */ public static int nAttribs() { return 2; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -