-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheHull.java
More file actions
115 lines (93 loc) · 3.27 KB
/
TheHull.java
File metadata and controls
115 lines (93 loc) · 3.27 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
/*
* A convex hull solving algorithm Graham Scan
* Author: Scott Gramig
*/
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Stack;
import java.io.*;
public class TheHull
{
private static Scanner input;// Scanner object
public static String[] currLine;//holds the split string
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException
{
Scanner kb = new Scanner(System.in);//input from keyboard
String line = null;//var to hold input from text file
String fileName, newFile;//file to read and file to save output
ArrayList<String> allNodes = new ArrayList<String>();//.size() will provide the total number of vertices
System.out.print("\nEnter the file name to use: ");//my file location - /home/p0wder/workspace/gramigsConvexHull/src/vertices.txt
fileName = kb.nextLine();
System.out.print("\nEnter the desired output file(.txt): ");
newFile = kb.nextLine();
PrintWriter pw = new PrintWriter(newFile, "UTF-8");
input = new Scanner(new File(fileName));//read input file
HashMap<String, MyPoint> allPoints = new HashMap<String, MyPoint>();
//this while loop puts all input into a HashMap and ArrayList of all nodes
while(input.hasNext())
{
line = input.nextLine();
currLine = line.split(", ");
MyPoint currPoint = new MyPoint(currLine[0],Integer.parseInt(currLine[1]), Integer.parseInt(currLine[2]));
allNodes.add(currLine[0]);
allPoints.put(currLine[0], currPoint);
}
//lets find the left most point on the cartesian plane
String leftMost = allNodes.get(0);
for(int i = 0; i < allNodes.size(); i++)
{
String currKey = allNodes.get(i);
if(allPoints.get(currKey).x < allPoints.get(leftMost).x)
leftMost = currKey;
}
//now lets find the slopes of 2 lines lines and compare them
MyLine allLines[] = new MyLine[allNodes.size()-1];
int j = 0;//indexing for allLines
for(int i = 0; i < allNodes.size(); i++)
{
if(leftMost == allNodes.get(i))
continue;
MyPoint fromPoint = allPoints.get(leftMost);
MyPoint toPoint = allPoints.get(allNodes.get(i));
MyLine currentLine = new MyLine(fromPoint, toPoint);
allLines[j] = currentLine;
//System.out.println(allLines[j].slope);
j++;
}
//sort in acsending order
Arrays.sort(allLines);
//this will have the final result for convex hull
Stack<String> ourStack = new Stack<String>();
ourStack.push(leftMost);
String elementBeforeLast = ourStack.peek();
ourStack.push(allLines[0].point2.name);
for(int i = 0; i < allLines.length-1; i++)
{
String lastElementInStack = ourStack.peek();
MyLine line1 = new MyLine(allPoints.get(elementBeforeLast), allPoints.get(lastElementInStack));
MyLine line2 = new MyLine(allPoints.get(elementBeforeLast), allPoints.get(allLines[i+1].point2.name));
if(!line1.isItClockwise(line2))
{
elementBeforeLast = ourStack.peek();
ourStack.push(allLines[i+1].point2.name);
}
else
{
ourStack.pop();
lastElementInStack = ourStack.pop();
elementBeforeLast = ourStack.pop();
ourStack.push(elementBeforeLast);
ourStack.push(lastElementInStack);
i--;
}
}
while(!ourStack.empty())
{
pw.println(ourStack.pop());
}
pw.close();
kb.close();
}
}