1 files added
3 files modified
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project version="4"> |
| | | <component name="VcsDirectoryMappings"> |
| | | <mapping directory="$PROJECT_DIR$" vcs="Git" /> |
| | | </component> |
| | | </project> |
| | |
| | | } |
| | | |
| | | /** |
| | | * 创建随机位置 |
| | | * 创建一个随机位置 坐标限制在 x 和 y内 |
| | | * @param maxX |
| | | * @param maxY |
| | | * @return |
| | | */ |
| | | public Point random(int maxX,int maxY){ |
| | | //设计边框距离,已避免部分地点名称出界 |
| | | int marginX = (int) (getResources().getDisplayMetrics() .density*15)*2; |
| | | Point point = new Point(); |
| | | |
| | |
| | | } |
| | | |
| | | int index = 0 ; |
| | | ArrayList<ArrayList<PathPlan.Place>> customLists; |
| | | ArrayList<ArrayList<PathPlan.Place>> customLists = new ArrayList<>(); |
| | | /** |
| | | * 自定义算法画线 |
| | | * @param view |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 自定义算法画线 |
| | | */ |
| | | public void drawLineCustome(){ |
| | | if(customLists.size()>1){ |
| | | Toast.makeText(MainActivity.this,"当前有"+customLists.size()+"路线规划",Toast.LENGTH_SHORT).show(); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 刷新 重新加载地点 |
| | | * @param view |
| | | */ |
| | | public void refresh(View view) { |
| | | planPathView.clear(); |
| | | customLists.clear(); |
| | | int width = planPathView.getWidth(); |
| | | int height = planPathView.getHeight(); |
| | | ArrayList<PathPlan.Place> places = new ArrayList<>(); |
| | | places.add(new PathPlan.Place("A",random(width,height),0)); |
| | | places.add(new PathPlan.Place("B",random(width,height),0)); |
| | | places.add(new PathPlan.Place("C",random(width,height),0)); |
| | | places.add(new PathPlan.Place("D",random(width,height),0)); |
| | | places.add(new PathPlan.Place("E",random(width,height),0)); |
| | | places.add(new PathPlan.Place("F",random(width,height),0)); |
| | | places.add(new PathPlan.Place("G",random(width,height),0)); |
| | | places.add(new PathPlan.Place("H",random(width,height),0)); |
| | | animD = false; |
| | | animC = false; |
| | | planPathView.setAnimPoints(randomList(width,height),listener); |
| | |
| | | |
| | | public class DijkstraUtils { |
| | | |
| | | HashMap<PathPlan.Place, PathPlan.Distance> distances; |
| | | HashMap<PathPlan.Place, PathPlan.Distance> distances;//每个点的信息集合 |
| | | |
| | | public DijkstraUtils(HashMap<PathPlan.Place, PathPlan.Distance> distances) { |
| | | this.distances = distances; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 执行 |
| | | * 执行算法 |
| | | * @param start 起点 |
| | | * @param end |
| | | */ |
| | |
| | | ArrayList<PathPlan.Place> places = new ArrayList<>(); |
| | | places.add(start); |
| | | while (distances.size()>0){//循环遍历 |
| | | PathPlan.Place nearWay = findNearWay(startDis); |
| | | startDis = distances.get(nearWay); |
| | | deletePlace(nearWay); |
| | | places.add(nearWay); |
| | | PathPlan.Place nearWay = findNearWay(startDis);//获取与其他点的最短个那一个 |
| | | startDis = distances.get(nearWay);//距离信息 |
| | | deletePlace(nearWay);//从集合中删除,避免下次循环再次出现该地点 |
| | | places.add(nearWay);//添加到线路集合记录中 |
| | | } |
| | | if(end != null){ |
| | | if(end != null){//如果有终点则最后追加 |
| | | end.distance = endDis.distanceMap.get(places.get(places.size()-1)); |
| | | places.add(end); |
| | | } |
| | | return places; |
| | | return places;//返回规划的路线集合 |
| | | |
| | | } |
| | | |
| | |
| | | ArrayList<ArrayList<PathPlan.Place>> arrayLists = new ArrayList<>();//路线记录 |
| | | /** |
| | | * 自定义规划路线算法 |
| | | * @param start 起始地点 |
| | | * @param end 末尾地点 |
| | | */ |
| | | public ArrayList<ArrayList<PathPlan.Place>> initCustomPathPlan( PathPlan.Place start,PathPlan.Place end){ |
| | | arrayLists.clear();//清空记录 |
| | | ArrayList<PathPlan.Place > places = new ArrayList<>(distances.keySet()); |
| | | ArrayList<PathPlan.Place > places = new ArrayList<>(distances.keySet());//所有地点集合 |
| | | for(int i=1;i<=places.size();i++){ |
| | | sortPlace(places,new ArrayList<PathPlan.Place>(),i,start,end); |
| | | } |
| | |
| | | |
| | | /** |
| | | * 组合 |
| | | * @param datas |
| | | * @param target |
| | | * @param num |
| | | * @param start 起始 |
| | | * @param end 末尾 |
| | | * @param datas 地点数据 |
| | | * @param target 整理的新路线 |
| | | * @param num 循环的下标位置,即路线规划的地点数量 |
| | | * @param start 起始地点 |
| | | * @param end 末尾地点 |
| | | */ |
| | | private void sortPlace(ArrayList<PathPlan.Place> datas, ArrayList<PathPlan.Place> target,int num,PathPlan.Place start,PathPlan.Place end) { |
| | | if (target.size() == num) { |
| | | ArrayList newDatas = new ArrayList( ); |
| | | ArrayList<PathPlan.Place > places = new ArrayList<>(distances.keySet()); |
| | | //数量不够,起始和末尾不对应 |
| | | ArrayList<PathPlan.Place > places = new ArrayList<>(distances.keySet());//所有地点集合 |
| | | //数量不够,起始和末尾不对应,则不再执行 |
| | | if(places.size()> num || start!=null && !target.get(0).equals(start) || end!=null && !target.get(target.size()-1).equals(end)){ |
| | | return; |
| | | } |
| | |
| | | //记录最短距离 |
| | | if(dist == -1 || dist >= tempDist){ |
| | | if(dist > tempDist){ |
| | | arrayLists.clear(); |
| | | arrayLists.clear();//清除之前的数据 |
| | | } |
| | | dist = tempDist; |
| | | arrayLists.add(newDatas); |
| | |
| | | sortPlace(newDatas, newTarget,num,start,end); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建一个随机位置 坐标在0-100内 |
| | | * @return |
| | | */ |
| | | public static Point random(){ |
| | | Point point = new Point(); |
| | | |
| | |
| | | import java.util.TreeMap; |
| | | |
| | | /** |
| | | * My father is Object, ites purpose of 线路规划 |
| | | * My father is Object, ites purpose of 距离 |
| | | * |
| | | * @purpose Created by Runt (qingingrunt2010@qq.com) on 2020-12-23. |
| | | */ |
| | |
| | | public Point point; |
| | | //权重,优先权 |
| | | public int priority; |
| | | //上一站距離 |
| | | //上一站距離,计算时补充,初始化为0 |
| | | public double distance; |
| | | |
| | | public Place(Place place){ |
| | |
| | | * 距离 |
| | | */ |
| | | public static class Distance{ |
| | | public Place place; |
| | | public HashMap<Place,Double> distanceMap = new HashMap(); |
| | | public Place place;//地点 |
| | | public HashMap<Place,Double> distanceMap = new HashMap();//保存与其他所有地点的距离集合 |
| | | public Distance(Place place ) { |
| | | this.place = place; |
| | | } |