/** * 蛮力求平面中距离最近的两点 * @param points 所有点的坐标点 * @return */ public static DataBean bruteForceClosestPoint(List<Pair> points) { Doubl ...
/** * 蛮力求平面中距离最近的两点 * @param points 所有点的坐标点 * @return */ public static DataBean bruteForceClosestPoint(List<Pair> points) { Double minDistance = Double.MAX_VALUE; //遍历数据,获取x,y //获取map的迭代器 DataBean dbOld = new DataBean(); dbOld.setPointDistance(minDistance); for(int i = 0; i < points.size(); ++i) { Pair point1 = points.get(i); for(int j = i + 1; j < points.size(); ++j) { Pair point2 = points.get(j); //求两点距离 DataBean db = new DataBean(); db.setX1(point1.x); db.setY1(point1.y); db.setX2(point2.x); db.setY2(point2.y); db.setPointDistance(Found.getDistance(point1.x, point1.y, point2.x, point2.y)); dbOld = Found.getMinDouble(db, dbOld); } } return dbOld; } public static Double getDistance(Long x1, Long y1, Long x2, Long y2) { Double distance = Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2)); return distance; } public static DataBean getMinDouble(DataBean d1, DataBean d2) { if (d1 == null || d2 == null) return null; if (d1.getPointDistance() < d2.getPointDistance()) { return d1; } else { return d2; } }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:【算法设计与分析基础】7、蛮力求平面中距离最近的两点
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。