public class Point { private double x; private double y; /** * 返回横坐标 * * @return */ public double getX() { return x; } /** * 移动点:向右移动dx单位,向上移动dy单位; * @param dx * @param dy */ public void translate(double dx, double dy) { x += dx; y += dy; } /** * 将点沿逆时针方向旋转ta角度 * * @param ta */ public void ratate(double ta) { double x1, y1; x1 = x * Math.cos(ta) - y * Math.sin(ta); y1 = x * Math.sin(ta) + y * Math.cos(ta); x = x1; y = y1; } /** *返回该点与点p2的距 *return */ public double distance(Point p2) { return Math.sqrt(Math.pow(p2.x - x, 2) + Math.pow(p2.y - y, 2)); } /** * 返回纵坐标 * * @return */ public double getY() { return y; } /** * @param args */ public static void main(String[] args) { Point a = new Point(0,0); Point b = new Point(2,2); b.translate(-1,-1); System.out.println(a.distance(b)); System.out.println(b); } public String toString() { // TODO Auto-generated method stub return "(" + x + "," + y + ")"; } /** * 二维点的构建 * @param x * @param y */ public Point(double x, double y) { super(); // TODO Auto-generated constructor stub this.x = x; this.y = y; } } |