日期:2014-05-20 浏览次数:20690 次
// 在两点所在直线上,以从startPoint到endPoint为方向,离startPoint的距离disToStartPoint的点 public static Point2D extentPoint(Point2D startPoint, Point2D endPoint, double disToStartPoint) { double disX = endPoint.getX() - startPoint.getX(); double disY = endPoint.getY() - startPoint.getY(); double dis = Math.sqrt(disX * disX + disY * disY); double sin = (endPoint.getY() - startPoint.getY()) / dis; double cos = (endPoint.getX() - startPoint.getX()) / dis; double deltaX = disToStartPoint * cos; double deltaY = disToStartPoint * sin; return new Point2D.Double(startPoint.getX() + deltaX, startPoint.getY() + deltaY); }
------解决方案--------------------
// 求点与x轴正向的夹角 public static double degreeOfPoint(Point2D p) { return degreeOfPoint(p.getX(), p.getY()); } public static double degreeOfPoint(double x, double y) { // 先求在一或者四象限的角度值,再判断是否在二三象限 double radian = Math.atan(y / x); if ((x < 0 && y < 0) || (x < 0 && y > 0)) { radian += Math.PI; } return Math.toDegrees(radian); } public static void main(String[] args) { System.out.println(degreeOfPoint(20, 20)); System.out.println(degreeOfPoint(-20, 20)); System.out.println(degreeOfPoint(-20, -20)); System.out.println(degreeOfPoint(20, -20)); System.out.println(degreeOfPoint(40, 20)); System.out.println(degreeOfPoint(-20, 30)); System.out.println(degreeOfPoint(-50, -230)); System.out.println(degreeOfPoint(120, -60)); System.out.println(degreeOfPoint(120, -0)); }