Repository navigation
Expand file tree
/
Copy pathPPMap.java
More file actions
128 lines (112 loc) · 3.17 KB
/
Copy pathPPMap.java
File metadata and controls
128 lines (112 loc) · 3.17 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package org.bk.ass.path;
import org.bk.ass.grid.Grid;
public interface PPMap extends Grid<Boolean> {
short left(int x, int y);
short down(int x, int y);
short up(int x, int y);
short right(int x, int y);
static PPMap fromBooleanArray(boolean[][] map) {
return fromMap(new Grid<Boolean>() {
@Override
public Boolean get(int x, int y) {
return y >= 0 && y < map[0].length && x >= 0 && x < map.length && map[x][y];
}
@Override
public int getWidth() {
return map.length;
}
@Override
public int getHeight() {
return map[0].length;
}
});
}
static PPMap fromMap(Grid<Boolean> map) {
short[][] left = new short[map.getHeight()][map.getWidth()];
short[][] right = new short[map.getHeight()][map.getWidth()];
short[][] up = new short[map.getHeight()][map.getWidth()];
short[][] down = new short[map.getHeight()][map.getWidth()];
for (int y = 0; y < map.getHeight(); y++) {
short pos = -1;
for (int x = 0; x < map.getWidth(); x++) {
left[y][x] = pos;
if (map.get(x, y)) {
if (map.get(x - 1, y - 1) && !map.get(x, y - 1)
|| map.get(x - 1, y + 1) && !map.get(x, y + 1)) {
pos = (short) x;
}
} else {
pos = (short) (-x - 2);
}
}
pos = (short) -map.getWidth();
for (int x = map.getWidth() - 1; x >= 0; x--) {
right[y][x] = pos;
if (map.get(x, y)) {
if (map.get(x + 1, y - 1) && !map.get(x, y - 1)
|| map.get(x + 1, y + 1) && !map.get(x, y + 1)) {
pos = (short) x;
}
} else {
pos = (short) -x;
}
}
}
for (int x = 0; x < map.getWidth(); x++) {
short pos = -1;
for (int y = 0; y < map.getHeight(); y++) {
up[y][x] = pos;
if (map.get(x, y)) {
if (map.get(x - 1, y - 1) && !map.get(x - 1, y)
|| map.get(x + 1, y - 1) && !map.get(x + 1, y)) {
pos = (short) y;
}
} else {
pos = (short) (-y - 2);
}
}
pos = (short) -map.getHeight();
for (int y = map.getHeight() - 1; y >= 0; y--) {
down[y][x] = pos;
if (map.get(x, y)) {
if (map.get(x - 1, y + 1) && !map.get(x - 1, y)
|| map.get(x + 1, y + 1) && !map.get(x + 1, y)) {
pos = (short) y;
}
} else {
pos = (short) -y;
}
}
}
return new PPMap() {
@Override
public short left(int x, int y) {
return left[y][x];
}
@Override
public short down(int x, int y) {
return down[y][x];
}
@Override
public short up(int x, int y) {
return up[y][x];
}
@Override
public short right(int x, int y) {
return right[y][x];
}
@Override
public Boolean get(int x, int y) {
return map.get(x, y);
}
@Override
public int getHeight() {
return map.getHeight();
}
@Override
public int getWidth() {
return map.getWidth();
}
};
}
}