-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvexMain.java
More file actions
180 lines (150 loc) · 5.68 KB
/
ConvexMain.java
File metadata and controls
180 lines (150 loc) · 5.68 KB
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Random;
public class ConvexMain {
public static void main(String[] args) {
ArrayList<Points> TotalList=new ArrayList<Points>(AddPoints());
SortListBaseX(TotalList);
long startTime=System.currentTimeMillis();
ArrayList<Points> ResultList=new ArrayList<Points>();
GetResultList(ResultList,TotalList);
long endTime=System.currentTimeMillis();
ArrayList<Points> Result=new ArrayList<Points>();
for(int i=0; i<ResultList.size();i++){
if(!Result.contains(ResultList.get(i)))
Result.add(ResultList.get(i));
}
System.out.println("There are total "+(TotalList.size()+2)+" points in the set.");
System.out.println("Total time "+(endTime-startTime));
System.out.println("There are "+Result.size()+" points on the convexhull, they are:");
for(int i=0;i<Result.size();i++){
System.out.println("Point "+(i+1)+": "+Result.get(i));
}
}
//Method of getting the result list.
public static ArrayList<Points> GetResultList (ArrayList<Points> ResultList,ArrayList<Points> TotalList){
ResultList.add(TotalList.get(0));
ResultList.add(TotalList.get(TotalList.size()-1));
TotalList.remove(0);
TotalList.remove(TotalList.size()-1);
ArrayList<Points> LeftList=new ArrayList<Points>(SideList(ResultList.get(0),ResultList.get(1),TotalList,"left"));//Left Side List
FindConvexHull(ResultList.get(0),ResultList.get(1),LeftList,ResultList);
ArrayList<Points> RightList=new ArrayList<Points>(SideList(ResultList.get(0),ResultList.get(1),TotalList,"right"));//Right Side List
FindConvexHull(ResultList.get(1),ResultList.get(0),RightList,ResultList);
return ResultList;
}
//Sorting a point array based on the value of X-coodinator.
public static ArrayList<Points> SortListBaseX (ArrayList<Points> SortList){
Comparator<Points> comparator = new Comparator<Points>(){
public int compare(Points a, Points b) {
if (a.x!=b.x){
return a.x-b.x;
}
else{
return a.y-b.y;
}
}
};
Collections.sort(SortList,comparator);
return SortList;
}
//Find ConvexHull based on Deviding and Conquer recurrsively.
public static void FindConvexHull(Points a, Points b, ArrayList<Points> SideList, ArrayList<Points> ResultList){
Iterator<Points> it = SideList.iterator();
ArrayList<Points> LeftSideList=new ArrayList<Points>();
ArrayList<Points> RightSideList=new ArrayList<Points>();
Points MaxPoint=null;
int max=0;
while(it.hasNext()){
Points temp=it.next();
int compute= compute(temp,a,b);
if (compute>max){
MaxPoint=temp;
}
}
if (MaxPoint!=null){
ResultList.add(MaxPoint);
SideList.remove(MaxPoint);
Iterator<Points> it2 = SideList.iterator();
while (it2.hasNext()){
Points temp=it2.next();
if (onLeft(temp,a,MaxPoint)==1){
LeftSideList.add(temp);
}
if (onLeft(temp,MaxPoint,b)==1){
RightSideList.add(temp);
}
}
FindConvexHull(a,MaxPoint,LeftSideList,ResultList);
FindConvexHull(MaxPoint,b,RightSideList,ResultList);
}
}
//Devide the whole dot set as two parts.
public static ArrayList<Points> SideList(Points a, Points b, ArrayList<Points> TotalList,String side){
Iterator<Points> it = TotalList.iterator();
ArrayList<Points> LeftList=new ArrayList<Points>();
ArrayList<Points> RightList=new ArrayList<Points>();
while(it.hasNext()){
Points temp=it.next();
if (onLeft(temp,a,b)==1){
LeftList.add(temp);
}
else if (onLeft(temp,a,b)==2){
RightList.add(temp);
}
}
if (side.equals("left")){
return LeftList;
}
else if (side.equals("right")){
return RightList;
}
else{
return null;
}
}
//Determine if the point is on the left/right of the line, if left ->1, right ->2, on the line ->3.
public static int onLeft(Points target,Points p1,Points p2){
int compute=compute(target,p1,p2);
if(compute > 0)
return 1;//left
else if(compute < 0)
return 2;//right
else
return 3;//on the line
}
//Compute the vector product of two edges of a triangle.
public static int compute(Points target, Points p1, Points p2){
int x1 = p1.x,y1 = p1.y;
int x2 = p2.x,y2 = p2.y;
int x3 = target.x,y3 = target.y;
int compute = x1*y2 + x3*y1 + x2*y3 - x3*y2 - x2*y1 - x1*y3;
return compute;
}
//Add points to the whole list.
public static ArrayList<Points> AddPoints(){
ArrayList<Points> TotalList=new ArrayList<Points>();
TotalList.add(new Points(1,1));
TotalList.add(new Points(1,2));
TotalList.add(new Points(3,7));
TotalList.add(new Points(8,1));
TotalList.add(new Points(3,1));
TotalList.add(new Points(1,4));
TotalList.add(new Points(2,5));
TotalList.add(new Points(13,2));
TotalList.add(new Points(27,8));
TotalList.add(new Points(9,8));
TotalList.add(new Points(6,34));
TotalList.add(new Points(51,66));
TotalList.add(new Points(88,93));
TotalList.add(new Points(74,48));
TotalList.add(new Points(36,61));
TotalList.add(new Points(32,85));
TotalList.add(new Points(55,77));
TotalList.add(new Points(2,1));
TotalList.add(new Points(7,5));
return TotalList;
}
}