查找最近的点
来源:互联网 发布:淘宝直播是镜像吗 编辑:程序博客网 时间:2024/06/08 14:54
查找一个点的最近点可以应用到很多的地方,下面是我写的一个实例,找到一个点的最近点。
package com.test;import java.util.List;/*********************************************** ** 创建人: 黎荣恒 ** 日 期 : 2015-9-21 上午11:08:02 ** 描述 : 点信息 ** ** 版 本 : 1.0 修改人: 日 期 : 修改记录: ************************************************/public class Point{private int id;private String name;private int hasLogo;//1:有logo 0:没有private String description;//商店描述private String phoneNumber;//电话号码private int type;private Coords coords;//商品位置信息private List<Coords> list;//商品边缘位置信息public int getId(){return id;}public void setId(int id){this.id = id;}public String getName(){return name;}public void setName(String name){this.name = name;}public int getHasLogo(){return hasLogo;}public void setHasLogo(int hasLogo){this.hasLogo = hasLogo;}public String getDescription(){return description;}public void setDescription(String description){this.description = description;}public String getPhoneNumber(){return phoneNumber;}public void setPhoneNumber(String phoneNumber){this.phoneNumber = phoneNumber;}public int getType(){return type;}public void setType(int type){this.type = type;}public Coords getCoords(){return coords;}public void setCoords(Coords coords){this.coords = coords;}public List<Coords> getList(){return list;}public void setList(List<Coords> list){this.list = list;}}
package com.test;/*********************************************** ** 创建人: 黎荣恒 ** 日 期 : 2015-9-21 上午11:21:20 ** 描述 : 位置坐标 ** ** 版 本 : 1.0 修改人: 日 期 : 修改记录: ************************************************/public class Coords{private int x;private int y;public int getX(){return x;}public void setX(int x){this.x = x;}public int getY(){return y;}public void setY(int y){this.y = y;}}
这是匹配的核心代码:
package com.test;import java.util.ArrayList;import java.util.List;/** * 匹配算法 * @author 黎荣恒 * */public class Match{private int square = 2;private MatchInfo shopMatchInfo;public void setShopMatchInfo(MatchInfo shopMatchInfo){this.shopMatchInfo = shopMatchInfo;}/** * 匹配方法 * @param shopList 商铺信息队列 * @param coords 点击位置 */public void match(List<Point> shopList, Coords coords){List<Double> list = calculate(shopList, coords);compare(list);}public List<Double> calculate(List<Point> shopList, Coords coords){int coordsX = coords.getX();int coordsY = coords.getY();List<Double> list = new ArrayList<Double>();for (int i = 0; i < shopList.size(); i++){int coordsX1 = shopList.get(i).getCoords().getX();int coordsY1 = shopList.get(i).getCoords().getY();double sum = Math.pow(Math.abs(coordsX - coordsX1), square)+ Math.pow(Math.abs(coordsY - coordsY1), square);double value = Math.sqrt(sum);list.add(value);}return list;}/** * 比较方法 * @param list */private void compare(List<Double> list){Double temp = 0.0;for (int i = 0; i < list.size(); i++){if (i == 0){temp = list.get(i);} else{temp = temp < list.get(i) ? temp : list.get(i);}}for (int i = 0; i < list.size(); i++){if (list.get(i) == temp){shopMatchInfo.matchSuccess(i);break;}}}}
匹配成功回调接口:
package com.test;/*********************************************** ** 创建人: 黎荣恒 ** 日 期 : 2015-9-21 下午3:55:43 ** 描述 : 匹配接口 ** ** 版 本 : 1.0 修改人: 日 期 : 修改记录: ************************************************/public interface MatchInfo{public void matchSuccess(int index);}
主方法:
package com.test;import java.util.ArrayList;import java.util.List;/** * 测试类 * @author 黎荣恒 */public class Main implements MatchInfo{static List<Point> pointList;public static void main(String[] args){new Main();}public Main(){Match match = new Match();pointList = new ArrayList<Point>();Point point = null;//生成20个点for (int i = 0; i < 20; i++){point = new Point();point.setId(i);Coords coords = new Coords();coords.setX(i);coords.setY(i);point.setCoords(coords);pointList.add(point);}Coords coords = new Coords();coords.setX(5);coords.setY(3);match.setShopMatchInfo(Main.this);match.match(pointList, coords);}/** * 查找成功回调 */@Overridepublic void matchSuccess(int index){System.out.println("最近点id为:"+pointList.get(index).getId());}}
案例代码下载:http://download.csdn.net/detail/u013043346/9334005
0 0
- 查找最近的点
- 基于MongoDB 2dSphere索引查找最近的点
- 查找最近的实体
- HDU1007 查找平面最近点对
- HDU1007 查找平面最近点对
- 最近的学习点
- POJ4134查找最近的元素
- K个最近的点
- K个最近的点
- 最近总结的几点
- 点到线段的最近点
- 点的查找
- 点集中的最近点 最远点对
- VB + MapX 查找最近的图元
- Linux查找最近修改的文件
- find 查找最近修改的文件
- jquery查找最近的父节点
- C++ 二分查找 求最近的值
- 用哈希法组织关键字
- C/C++语言中函数参数传递的三种方式
- android上传文件到服务器
- 15周—项目1—哈希表及其运算的实现
- 第15周【项目2 - 用哈希法组织关键字】
- 查找最近的点
- 欢迎使用CSDN-markdown编辑器
- 第14周项目2 二叉树排序树中查找的路径
- 调用
- 第15周、项目3—B-树的基本操作
- Android Studio(as)调试总结
- 第十三周项目2 Kruskal算法
- 第十二周 项目二 操作用邻接表储存的表
- 第十五周项目2-用哈希法组建关键字