查找一个点的最近点可以应用到很多的地方,下面是我写的一个实例,找到一个点的最近点。
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);
}
/**
* 查找成功回调
*/
@Override
public void matchSuccess(int index)
{
System.out.println("最近点id为:"+pointList.get(index).getId());
}
}
案例代码下载: http://download.csdn.net/detail/u013043346/9334005