-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvexHullSolver.cs
executable file
·243 lines (212 loc) · 7.65 KB
/
ConvexHullSolver.cs
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using _1_convex_hull;
using System.Linq;
namespace _2_convex_hull {
class ConvexHullSolver {
System.Drawing.Graphics g;
System.Windows.Forms.PictureBox pictureBoxView;
public ConvexHullSolver(System.Drawing.Graphics g, System.Windows.Forms.PictureBox pictureBoxView) {
this.g = g;
this.pictureBoxView = pictureBoxView;
}
public void Refresh() {
// Use this especially for debugging and whenever you want to see what you have drawn so far
pictureBoxView.Refresh();
}
public void Pause(int milliseconds) {
// Use this especially for debugging and to animate your algorithm slowly
pictureBoxView.Refresh();
System.Threading.Thread.Sleep(milliseconds);
}
public void Solve(List<System.Drawing.PointF> pointList) {
// TODO: Insert your code here
//order points by x value
pointList = pointList.OrderBy(p => p.X).ToList();//O(nlogn)
Hull solution = getHull(pointList,0,"start");
Pen redPen = new Pen(Color.Red, 2.0f);
drawLines(solution.getPoints(), redPen);
Refresh();
}
public void drawLines(List<PointF> points, Pen pen) {
PointF[] pointArray = points.ToArray();
g.DrawLines(pen, pointArray);
g.DrawLine(pen, pointArray[0], pointArray[points.Count - 1]);
}
public Hull getHull(List<PointF> pointList, int recurLevel, String side) {
if (pointList.Count <= 1) {
Hull result = new Hull(pointList);
result.setRightMost(pointList[pointList.Count - 1]);
return result;
}
else {
List<List<PointF>> sets = divideSet(pointList);
Hull leftHull = getHull(sets[0], recurLevel + 1, side += " left");
Hull rightHull = getHull(sets[1], recurLevel + 1, side += " right");
//Console.WriteLine(recurLevel + " " + side);
//Console.WriteLine("\tSize of left: " + leftHull.getPoints().Count);
//Console.WriteLine("\tSize of right: " + rightHull.getPoints().Count);
return merge(leftHull, rightHull);
}
}
public Hull merge(Hull left, Hull right) {
int rightMost = left.getRightMostIndex();
int leftMost = right.getLeftMostIndex();
int currentLeftIndex = rightMost;
int currentRightIndex = leftMost;
int upperLeft = -1;
int upperRight = -1;
int lowerLeft = -1;
int lowerRight = -1;
bool leftIndexChanged = false;
bool rightIndexChanged = false;
//iterate through at least once
bool firstRight = true;
bool firstLeft = true;
//get upper common tangent
while(leftIndexChanged || rightIndexChanged || firstLeft || firstRight){
if (firstRight || leftIndexChanged) {
firstRight = false;
upperRight = getRightUpper(left, right, currentLeftIndex, currentRightIndex);
if (upperRight == currentRightIndex) {
leftIndexChanged = false;
rightIndexChanged = false;
}
else {
rightIndexChanged = true;
currentRightIndex = upperRight;
}
}
if(firstLeft || rightIndexChanged){
firstLeft = false;
upperLeft = getLeftUpper(left, right, currentLeftIndex, currentRightIndex);
if(upperLeft == currentLeftIndex){
leftIndexChanged = false;
rightIndexChanged = false;
}
else {
leftIndexChanged = true;
currentLeftIndex = upperLeft;
}
}
}
//get lower common tangentt
currentLeftIndex = rightMost;
currentRightIndex = leftMost;
leftIndexChanged = false;
rightIndexChanged = false;
//iterate through at least once
firstRight = true;
firstLeft = true;
while (leftIndexChanged || rightIndexChanged || firstLeft || firstRight) {
if (firstLeft || rightIndexChanged) {
firstLeft = false;
lowerLeft = getLeftLower(left, right, currentLeftIndex, currentRightIndex);
if (lowerLeft == currentLeftIndex) {
leftIndexChanged = false;
rightIndexChanged = false;
}
else {
leftIndexChanged = true;
currentLeftIndex = lowerLeft;
}
}
if (firstRight || leftIndexChanged) {
firstRight = false;
lowerRight = getRightLower(left, right, currentLeftIndex, currentRightIndex);
if (lowerRight == currentRightIndex) {
leftIndexChanged = false;
rightIndexChanged = false;
}
else {
rightIndexChanged = true;
currentRightIndex = lowerRight;
}
}
}
//join points
List<PointF> resultPoints = new List<PointF>();
//add up to (and including) upperLeft
for (int i = 0; i <= upperLeft;i++){
resultPoints.Add(left.getPoints()[i]);
}
//add up to lowerRight
for (int i = upperRight; i != lowerRight; i = right.getNextIndex(i)){
resultPoints.Add(right.getPoints()[i]);
}
//add lowerRight
resultPoints.Add(right.getPoints()[lowerRight]);
//add from lowerLeft to beginning
for (int i = lowerLeft; i != 0; i = left.getNextIndex(i)){
resultPoints.Add(left.getPoints()[i]);
}
return new Hull(resultPoints);
}
private int getLeftUpper(Hull left, Hull right, int leftIndex, int rightIndex){ //O(n)
List<PointF> leftPoints = left.getPoints();
List<PointF> rightPoints = right.getPoints();
while(calculateSlope(rightPoints[rightIndex], leftPoints[left.getPrevIndex(leftIndex)]) <
calculateSlope(rightPoints[rightIndex], leftPoints[leftIndex])){
leftIndex = left.getPrevIndex(leftIndex);
}
return leftIndex;
}
private int getRightUpper(Hull left, Hull right, int leftIndex, int rightIndex){ //O(n)
List<PointF> rightPoints = right.getPoints();
List<PointF> leftPoints = left.getPoints();
while(calculateSlope(leftPoints[leftIndex], rightPoints[right.getNextIndex(rightIndex)]) >
calculateSlope(leftPoints[leftIndex], rightPoints[rightIndex])){
rightIndex = right.getNextIndex(rightIndex);
}
return rightIndex;
}
private int getLeftLower(Hull left, Hull right, int leftIndex, int rightIndex){ //O(n)
List<PointF> leftPoints = left.getPoints();
List<PointF> rightPoints = right.getPoints();
while(calculateSlope(rightPoints[rightIndex], leftPoints[left.getNextIndex(leftIndex)]) >
calculateSlope(rightPoints[rightIndex], leftPoints[leftIndex])){
leftIndex = left.getNextIndex(leftIndex);
}
return leftIndex;
}
private int getRightLower(Hull left, Hull right, int leftIndex, int rightIndex){ //O(n)
List<PointF> rightPoints = right.getPoints();
List<PointF> leftPoints = left.getPoints();
while(calculateSlope(leftPoints[leftIndex], rightPoints[right.getPrevIndex(rightIndex)]) <
calculateSlope(leftPoints[leftIndex], rightPoints[rightIndex])){
rightIndex = right.getPrevIndex(rightIndex);
}
return rightIndex;
}
private int getIndexForPoint(PointF point, Hull hull){
List<PointF> points = hull.getPoints();
for (int i = 0; i < points.Count;i++){
if(points[i].Equals(point)){
return i;
}
}
return -100;
}
//return a list of a list of points.
//The first list will be the left side.
//The second will be the right side
public List<List<System.Drawing.PointF>> divideSet(List<PointF> points) {
List<PointF> leftSide = points.Take(points.Count / 2).ToList();
List<PointF> rightSide = points.Skip(points.Count / 2).ToList();
List<List<PointF>> result = new List<List<PointF>>();
result.Add(leftSide);
result.Add(rightSide);
return result;
}
public Double calculateSlope(PointF left, PointF right) {
return -(right.Y - left.Y) / (right.X - left.X);
}
private void printPointInformation(List<PointF> pointList) {
foreach (PointF point in pointList) {
Console.WriteLine("[" + point.X + "," + point.Y + "]");
}
}
}
}