Newer
Older
package GraphClasses;
import Graph.*;
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
int maxVertex;
public Graph graph;
public int abscissaOfVertex(int vertex) {
return vertex % width;
}
public int ordinateOfVertex(int vertex) {
return vertex / width;
}
private int vertexOfCoordinate(int abscissa, int ordinate) {
return ordinate * width + abscissa;
}
public Grid(int width, int height) {
this.width = width;
this.height = height;
maxVertex = width * height - 1;
graph = new Graph(maxVertex + 1);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (i < width - 1)
graph.addEdge(new Edge(
vertexOfCoordinate(i, j),
vertexOfCoordinate(i + 1, j),
0.0
));
if (j < height - 1)
graph.addEdge(new Edge(
vertexOfCoordinate(i, j),
vertexOfCoordinate(i, j + 1),
0.0
));
}
}
}
public boolean isHorizontal(Edge e) {
return Math.abs(e.source - e.dest) == 1;
}
public boolean isVertical(Edge e) {
return Math.abs(e.source - e.dest) == width;
}
private void drawLine(int h, BitSet right) {
for (int i = 0; i < width - 1; i++) {
System.out.print("o");
if (right.get(vertexOfCoordinate(i, h))) System.out.print("--");
else System.out.print(" ");
}
System.out.println("o");
}
private void drawInterline(int h, BitSet up) {
for (int i = 0; i < width; i++) {
if (up.get(vertexOfCoordinate(i, h))) System.out.print("|");
else System.out.print(" ");
if (i < width - 1) System.out.print(" ");
}
System.out.println();
}
public void drawSubgrid(Iterable<Edge> edges) {
BitSet up = new BitSet(maxVertex);
BitSet right = new BitSet(maxVertex);
for (Edge e : edges) {
// System.out.println(e.fromVertex + " -- " + e.toVertex);
if (isHorizontal(e))
right.set(Math.min(e.source, e.dest));
if (isVertical(e))
up.set(Math.min(e.source, e.dest));
}
for (int j = 0; j < height; j++) {
drawLine(j, right);
if (j < height - 1) drawInterline(j, up);
}
}