| | |
| | | |
| | | import androidx.appcompat.app.AppCompatActivity; |
| | | |
| | | import android.app.ProgressDialog; |
| | | import android.graphics.Point; |
| | | import android.os.Bundle; |
| | | import android.view.View; |
| | |
| | | int height = planPathView.getMeasuredHeight(); |
| | | int width = planPathView.getMeasuredWidth(); |
| | | |
| | | 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)); |
| | | planPathView.setPoints(places); |
| | | planPathView.setPoints(randomList(width,height)); |
| | | |
| | | planPathView.getViewTreeObserver().removeOnPreDrawListener(this); |
| | | return true; |
| | |
| | | btnDrawC.setEnabled(true); |
| | | } |
| | | }; |
| | | |
| | | /** |
| | | * 创建随机位置 |
| | | * @param width 范围宽度 |
| | | * @param height 范围高度 |
| | | * @return |
| | | */ |
| | | public ArrayList<PathPlan.Place> randomList(int width,int height){ |
| | | |
| | | ArrayList<PathPlan.Place> places = new ArrayList<>(); |
| | | places.add(new PathPlan.Place("甲",random(width,height),0)); |
| | | places.add(new PathPlan.Place("乙",random(width,height),0)); |
| | | places.add(new PathPlan.Place("丙",random(width,height),0)); |
| | | places.add(new PathPlan.Place("丁",random(width,height),0)); |
| | | places.add(new PathPlan.Place("戊",random(width,height),0)); |
| | | places.add(new PathPlan.Place("己",random(width,height),0)); |
| | | places.add(new PathPlan.Place("庚",random(width,height),0)); |
| | | places.add(new PathPlan.Place("辛",random(width,height),0)); |
| | | places.add(new PathPlan.Place("壬",random(width,height),0)); |
| | | places.add(new PathPlan.Place("癸",random(width,height),0)); |
| | | return places; |
| | | } |
| | | |
| | | /** |
| | | * 创建随机位置 |
| | |
| | | } |
| | | |
| | | int index = 0 ; |
| | | |
| | | ArrayList<ArrayList<PathPlan.Place>> customLists; |
| | | /** |
| | | * 自定义算法画线 |
| | | * @param view |
| | |
| | | public void drawLineCustom(View view){ |
| | | planPathView.clearLines(); |
| | | System.out.println("drawLineCustom " ); |
| | | HashMap<PathPlan.Place, PathPlan.Distance> distanceHashMap = PathPlan.intPoints(planPathView.getPoints()); |
| | | ArrayList<ArrayList<PathPlan.Place>> arrayLists = new DijkstraUtils(distanceHashMap).initCustomPathPlan(planPathView.getPoints().get(2), null); |
| | | if(arrayLists.size()>1){ |
| | | Toast.makeText(this,"当前有"+arrayLists.size()+"路线规划",Toast.LENGTH_SHORT).show(); |
| | | if(customLists == null || customLists.size() == 0) { |
| | | ProgressDialog progressDialog = new ProgressDialog(this); |
| | | progressDialog.setMessage("正在規劃路綫"); |
| | | progressDialog.show(); |
| | | new Thread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | |
| | | HashMap<PathPlan.Place, PathPlan.Distance> distanceHashMap = PathPlan.intPoints(planPathView.getPoints()); |
| | | customLists = new DijkstraUtils(distanceHashMap).initCustomPathPlan(planPathView.getPoints().get(2), null); |
| | | runOnUiThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | System.out.println("drawLineCustom " ); |
| | | progressDialog.dismiss(); |
| | | drawLineCustome(); |
| | | } |
| | | }); |
| | | } |
| | | }).start(); |
| | | }else{ |
| | | drawLineCustome(); |
| | | } |
| | | } |
| | | |
| | | |
| | | public void drawLineCustome(){ |
| | | if(customLists.size()>1){ |
| | | Toast.makeText(MainActivity.this,"当前有"+customLists.size()+"路线规划",Toast.LENGTH_SHORT).show(); |
| | | } |
| | | ArrayList<PlanPathView.Line> lines = new ArrayList<>(); |
| | | DecimalFormat df = new DecimalFormat("#.00"); |
| | | double totalLength = 0 ; |
| | | ArrayList<PathPlan.Place> arrayList = arrayLists.get(index % arrayLists.size()); |
| | | ArrayList<PathPlan.Place> arrayList = customLists.get(index % customLists.size()); |
| | | for(int i = 0 ; i < arrayList.size() -1; i ++){ |
| | | lines.add(new PlanPathView.Line(arrayList.get(i).point,arrayList.get(i+1).point,arrayList.get(i+1).distance)); |
| | | totalLength +=Double.parseDouble(df.format(arrayList.get(i).distance)); |
| | | System.out.println(arrayList.get(i)+" 距离:"+(i == arrayList.size() -1?"": TriangleUtils.getLengthOfSide(arrayList.get(i).point,arrayList.get(i+1).point)) +" "+arrayList.get(i).distance); |
| | | } |
| | | index++; |
| | | if(animC){ |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | 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("H",random(width,height),0)); |
| | | animD = false; |
| | | animC = false; |
| | | planPathView.setAnimPoints(places,listener); |
| | | planPathView.setAnimPoints(randomList(width,height),listener); |
| | | } |
| | | } |