Runt
2020-12-26 60df583cc43c99f3ce366d04b15b92964da21c26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.demo.navtogether;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.app.ProgressDialog;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.Toast;
 
import com.demo.navtogether.utils.DijkstraUtils;
import com.demo.navtogether.utils.PathPlan;
import com.demo.navtogether.utils.TriangleUtils;
 
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
 
public class MainActivity extends AppCompatActivity {
    PlanPathView planPathView;
    Button btnRefresh,btnDrawD,btnDrawC;
    boolean animD,animC;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        planPathView = findViewById(R.id.planpath);
        btnRefresh = findViewById(R.id.btn_refresh);
        btnDrawD = findViewById(R.id.btn_draw_dijkstra);
        btnDrawC = findViewById(R.id.btn_draw_custom);
 
        ViewTreeObserver vto = planPathView.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                int height = planPathView.getMeasuredHeight();
                int width = planPathView.getMeasuredWidth();
 
                planPathView.setPoints(randomList(width,height));
 
                planPathView.getViewTreeObserver().removeOnPreDrawListener(this);
                return true;
            }
        });
    }
    PlanPathView.AnimatorListener listener = new PlanPathView.AnimatorListener() {
        @Override
        public void onAnimation(int value) {
            btnRefresh.setEnabled(false);
            btnDrawD.setEnabled(false);
            btnDrawC.setEnabled(false);
        }
 
        @Override
        public void onFinish() {
            btnRefresh.setEnabled(true);
            btnDrawD.setEnabled(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;
    }
 
    /**
     * 创建随机位置
     * @param maxX
     * @param maxY
     * @return
     */
    public Point random(int maxX,int maxY){
        int marginX = (int) (getResources().getDisplayMetrics() .density*15)*2;
        Point point = new Point();
 
        point.x  = (int) (Math.random()*(maxX-marginX))+(marginX/2);
        point.y  = (int) (Math.random()*(maxY-marginX))+(marginX/2);
 
        return point;
    }
 
    /**
     * 迪杰斯特拉算法画线
     * @param view
     */
    public void drawLine(View view){
        planPathView.clearLines();
        //执行算法,返回位置路线集合
        ArrayList<PathPlan.Place> excute = new DijkstraUtils(PathPlan.intPoints(planPathView.getPoints())).excute(planPathView.getPoints().get(2));
        ArrayList<PlanPathView.Line> lines = new ArrayList<>();
        for(int i = 0 ; i < excute.size() -1; i ++){
            //生成路线
            lines.add(new PlanPathView.Line(excute.get(i).point,excute.get(i+1).point,excute.get(i+1).distance));
            System.out.println(excute.get(i)+" 距离:"+(i == excute.size() -1?"": TriangleUtils.getLengthOfSide(excute.get(i).point,excute.get(i+1).point)) +" "+excute.get(i).distance);
        }
        if(animD){
            planPathView.drawLine(lines);
        }else {
            animD = true;
            planPathView.drawAnimLine(lines, listener);
        }
    }
 
    int index = 0 ;
    ArrayList<ArrayList<PathPlan.Place>> customLists;
    /**
     * 自定义算法画线
     * @param view
     */
    public void drawLineCustom(View view){
        planPathView.clearLines();
        System.out.println("drawLineCustom " );
        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<>();
        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));
        }
        index++;
        if(animC){
            planPathView.drawLine(lines);
        }else {
            animC = true;
            planPathView.drawAnimLine(lines, listener);
        }
    }
 
    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);
    }
}