查找最近的点

来源:互联网 发布:淘宝直播是镜像吗 编辑:程序博客网 时间: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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 学位网没有英文认证出国留学怎么办 4-5岁幼儿记忆不好该怎么办 2个月宝宝不拉大便怎么办 论文已查重了要下载查重报告怎么办 华为畅享7手机媒体音量小怎么办 杂志投稿投到了假的网站怎么办 支付宝骗了5万多怎么办 微信转账到银行卡被骗了怎么办 给对方转账到银行卡疑似被骗怎么办 谷歌邮箱收不到邮件了怎么办 我在微信上做兼职被骗了钱怎么办 要是微信里面做兼职被骗了怎么办 在打离婚管斯死亡赔常金怎么办 编辑部回复我文章没有被录用怎么办 父亲死后教师资格被别人顶替怎么办 刚发的论文影响因子变化了怎么办 奥鹏毕业论文过了提交时间了怎么办 手机电池被拿出来后时间不准怎么办 高中的孩子与同学相处不好怎么办 孩子在幼儿园不敢和老师说话怎么办 注册过万方医学网忘了用户名怎么办 狗咬了没破皮没出血有点疼怎么办 狗咬了没出血肿起来了怎么办 广州已经过了幼儿园报名时间怎么办 大专学费没交学校开始查了怎么办 如果申请留学的本科成绩不够怎么办 硕士读了三年无法毕业怎么办啊 孩子在美国读研读不下去怎么办? 在地割草中过失死亡法律^怎么办 高中毕业申请国外大学没录取怎么办 英国研究生老师没给写推荐信怎么办 申请悉尼大学博士奖学金被拒怎么办 硕士延迟毕业但被博士录取怎么办 考完初级职称还是不会做账怎么办 美国留学生在中国办日本签证怎么办 留学雅思过了gpa低怎么办澳洲 护士电子化注册用户名忘记了怎么办 护士电子化注册证书编码错误怎么办 澳洲预科上半学期成绩不达标怎么办 美国大学绩点不够怎么办学习证明 研一想换导师导师不答应怎么办