-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphic.pde
135 lines (105 loc) · 3.05 KB
/
graphic.pde
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
double distance;
int gen;
int city_number;
PFont f;
BufferedReader reader;
String line;
String lx, ly;
int x_max, y_max;
class City {
int x, y;
public void setCoord(int x_pos, int y_pos){
x = x_pos;
y = y_pos;
}
};
City[] cities_map;
int[] current_path;
// The statements in the setup() function
// run once when the program begins
void setup(){
f = createFont("Arial", 16, true);
//Use the absolute path to data.txt file
reader = createReader("/home/user/data.txt");
//Read the grid size
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
line = null;
}
String[] grid_coords = split(line, ',');
//Read the city count
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
line = null;
}
city_number = Integer.parseInt(line);
surface.setSize(Integer.parseInt(grid_coords[0]), Integer.parseInt(grid_coords[1]));
stroke(255); // Set stroke color to white
noLoop();
cities_map = new City[city_number];
current_path = new int[city_number];
for (int i = 0; i < city_number; i++){
cities_map[i] = new City();
try {
line = reader.readLine();
String[] list = split(line, ',');
if (list.length == 2){
println("X: " + list[0] + " Y: " + list[1]);
int x = Integer.parseInt(list[0]);
int y = Integer.parseInt(list[1]);
cities_map[i].setCoord(x,y);
}
} catch (IOException e) {
e.printStackTrace();
line = null;
}
}
}
// The statements in draw() are run until the
// program is stopped. Each statement is run in
// sequence and after the last line is read, the first
// line is run again.
void draw(){
background(0); // Set the background to black
textFont(f);
textAlign(LEFT);
for (int i = 0; i < city_number; i++){
ellipse(cities_map[i].x, cities_map[i].y, 5, 5);
}
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
line = null;
}
if (line == null){
noLoop();
} else {
//println(line);
String[] list = split(line, ' ');
//println("1 : " + list[0] + " 2 : " + list[1]);
gen = Integer.parseInt(list[0]);
distance = Double.parseDouble(list[1]);
for (int i = 2; i < city_number+2; i++){
current_path[i-2] = Integer.parseInt(list[i]);
}
}
text("Current generation: " + gen + " | Best length: " + distance , 10, 20);
for (int i = 1; i < city_number; i++){
int id1 = current_path[i-1];
int id2 = current_path[i];
line(cities_map[id1].x, cities_map[id1].y, cities_map[id2].x, cities_map[id2].y);
}
delay(100);
}
void mousePressed(){
if (looping){
noLoop();
} else {
loop();
}
}