-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreatePolyhedra.java
127 lines (106 loc) · 3.22 KB
/
CreatePolyhedra.java
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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class CreatePolyhedra {
/**
* Main function
*
* @param argv[1] polyhedron input file
* @param argv[2] scaling factor
*/
public static void main(String[] argv)
{
int argc = argv.length;
// Validate number of command line arguments
if (argc < 2) {
System.err.println("Usage: java -jar CreatePolyhedra.jar inputFile scalingFactor");
System.exit(1);
}
// Validate Scaling Factor
int scalingFactor = 1;
try {
scalingFactor = Integer.parseInt(argv[1]);
}
catch (Exception e) {
System.err.println("Scaling Factor must be >= 1");
System.exit(1);
}
if (scalingFactor < 1) {
System.err.println("Scaling Factor must be >= 1");
System.exit(1);
}
Scanner polyIn = null;
try {
polyIn = new Scanner(new FileReader(argv[0]));
}
catch (Exception e) {
System.err.println("Could not open " + argv[0]);
System.exit(2);
}
ArrayList<Polyhedron> polyhedra = new ArrayList<Polyhedron>();
readPolyhedra(polyIn, polyhedra);
ArrayList<Polyhedron> scaledCopies = new ArrayList<Polyhedron>();
duplicateAndScale(polyhedra, scaledCopies, scalingFactor);
System.out.println("Original Polyhedra");
printDivider('-', 60);
printPolyhedra(polyhedra);
System.out.println();
System.out.println();
System.out.println("Scaled Polyhedra (Clones)");
printDivider('-', 60);
printPolyhedra(scaledCopies);
}
/**
* Construct a list of polyhedra from
* an input stream
*/
static void readPolyhedra(Scanner inf, ArrayList<Polyhedron> polyhedra)
{
Polyhedron p = Polyhedron.createAndRead(inf);
while (p != null) {
polyhedra.add(p);
p = Polyhedron.createAndRead(inf);
}
}
/**
* Print a collection of polyhedra
*/
static void printPolyhedra(ArrayList<Polyhedron> polyhedra)
{
for (Polyhedron p : polyhedra) {
System.out.println(p);
}
}
/**
* Copy each polyhedron and apply
* the scaling factor to each copy
*
* @param polyhedra orignal polyhedron collection
* @param scaledCopies collection in whcih to store the scled copies
* @param s scaling factor
*/
static void duplicateAndScale(ArrayList<Polyhedron> polyhedra,
ArrayList<Polyhedron> scaledCopies,
double s)
{
for (Polyhedron original : polyhedra) {
Polyhedron copy = original.clone();
copy.scale(s);
scaledCopies.add(copy);
}
}
/**
* Generate a divider
*/
static void printDivider(char symbol, int length)
{
StringBuilder bld = new StringBuilder();
for (int i = 0; i < length; i++) {
bld.append(symbol);
}
System.out.println(bld.toString());
}
}