| 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 |
package dr.geo; |
| 27 |
|
| 28 |
import javax.swing.*; |
| 29 |
import java.awt.*; |
| 30 |
import java.util.*; |
| 31 |
import java.util.List; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class IslandLattice extends JComponent implements Lattice { |
| 37 |
|
| 38 |
private int[][] lattice; |
| 39 |
private final int width; |
| 40 |
private final int height; |
| 41 |
|
| 42 |
public IslandLattice(int width, int height) { |
| 43 |
lattice = new int[width][height]; |
| 44 |
this.width = width; |
| 45 |
this.height = height; |
| 46 |
} |
| 47 |
|
| 48 |
public Dimension getPreferredSize() { |
| 49 |
return new Dimension(width, height); |
| 50 |
} |
| 51 |
|
| 52 |
public void paintLattice(Graphics g) { |
| 53 |
g.setColor(Color.blue); |
| 54 |
g.fillRect(0, 0, width, height); |
| 55 |
g.setColor(Color.green); |
| 56 |
|
| 57 |
for (int i = 0; i < lattice.length; i++) { |
| 58 |
for (int j = 0; j < lattice.length; j++) { |
| 59 |
if (lattice[i][j] > 0) { |
| 60 |
g.drawRect(i, j, 1, 1); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
public void paintComponent(Graphics g) { |
| 68 |
paintLattice(g); |
| 69 |
} |
| 70 |
|
| 71 |
public final int latticeWidth() { |
| 72 |
return width; |
| 73 |
} |
| 74 |
|
| 75 |
public final int latticeHeight() { |
| 76 |
return height; |
| 77 |
} |
| 78 |
|
| 79 |
public void setState(int i, int j, int state) { |
| 80 |
lattice[i][j] = state; |
| 81 |
} |
| 82 |
|
| 83 |
public int getState(int i, int j) { |
| 84 |
return lattice[i][j]; |
| 85 |
} |
| 86 |
|
| 87 |
public int getState(Location loc) { |
| 88 |
return lattice[loc.i][loc.j]; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
public void smooth(int boundary) { |
| 93 |
|
| 94 |
int[][] newLattice = new int[width][height]; |
| 95 |
|
| 96 |
for (int i = 0; i < lattice.length; i++) { |
| 97 |
for (int j = 0; j < lattice.length; j++) { |
| 98 |
|
| 99 |
int ncount = getNeighbourSum(i, j); |
| 100 |
|
| 101 |
if (ncount > boundary) { |
| 102 |
newLattice[i][j] = 1; |
| 103 |
} else { |
| 104 |
newLattice[i][j] = 0; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
lattice = newLattice; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public List<Location> addIslands(int n, Random random) { |
| 117 |
|
| 118 |
List<Location> locations = new ArrayList<Location>(); |
| 119 |
|
| 120 |
for (int k = 0; k < n; k++) { |
| 121 |
|
| 122 |
int i = random.nextInt(width); |
| 123 |
int j = random.nextInt(height); |
| 124 |
|
| 125 |
if (getState(i, j) == 0) { |
| 126 |
|
| 127 |
setState(i, j, 1); |
| 128 |
if (getNeighbourSum(i, j) == 0) { |
| 129 |
locations.add(new Location(i, j)); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
return locations; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public int growIslands(int n, Random random) { |
| 143 |
int count = 0; |
| 144 |
int attempts = 0; |
| 145 |
while (count < n) { |
| 146 |
int i = random.nextInt(width); |
| 147 |
int j = random.nextInt(height); |
| 148 |
|
| 149 |
if (getState(i, j) == 0) { |
| 150 |
|
| 151 |
if (getNeighbourSum(i, j) > 0) { |
| 152 |
setState(i, j, 1); |
| 153 |
count += 1; |
| 154 |
} |
| 155 |
} |
| 156 |
attempts += 1; |
| 157 |
|
| 158 |
} |
| 159 |
return attempts; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
public int sum() { |
| 164 |
int sum = 0; |
| 165 |
for (int i = 0; i < lattice.length; i++) { |
| 166 |
for (int j = 0; j < lattice.length; j++) { |
| 167 |
|
| 168 |
sum += lattice[i][j]; |
| 169 |
} |
| 170 |
} |
| 171 |
return sum; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
int getNeighbourSum(int i, int j) { |
| 176 |
|
| 177 |
int sum = 0; |
| 178 |
if (i > 0) { |
| 179 |
sum += getState(i - 1, j); |
| 180 |
} |
| 181 |
if (i < width - 1) { |
| 182 |
sum += getState(i + 1, j); |
| 183 |
} |
| 184 |
|
| 185 |
if (j > 0) { |
| 186 |
sum += getState(i, j - 1); |
| 187 |
} |
| 188 |
if (j < height - 1) { |
| 189 |
sum += getState(i, j + 1); |
| 190 |
} |
| 191 |
return sum; |
| 192 |
} |
| 193 |
|
| 194 |
private List<Location> getNeighbours(Location loc) { |
| 195 |
|
| 196 |
List<Location> neighbours = new ArrayList<Location>(); |
| 197 |
|
| 198 |
if (loc.i > 0) { |
| 199 |
neighbours.add(new Location(loc.i - 1, loc.j)); |
| 200 |
} |
| 201 |
if (loc.i < width - 1) { |
| 202 |
neighbours.add(new Location(loc.i + 1, loc.j)); |
| 203 |
} |
| 204 |
if (loc.j > 0) { |
| 205 |
neighbours.add(new Location(loc.i, loc.j - 1)); |
| 206 |
} |
| 207 |
if (loc.j < height - 1) { |
| 208 |
neighbours.add(new Location(loc.i, loc.j + 1)); |
| 209 |
} |
| 210 |
return neighbours; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
private Set<Location> getEmptyNeighboursOfIsland(Location loc) { |
| 215 |
List<Location> toProcess = new ArrayList<Location>(); |
| 216 |
Set<Location> processed = new HashSet<Location>(); |
| 217 |
Set<Location> empty = new HashSet<Location>(); |
| 218 |
toProcess.add(loc); |
| 219 |
|
| 220 |
while (toProcess.size() > 0) { |
| 221 |
Location l = toProcess.remove(toProcess.size() - 1); |
| 222 |
List<Location> neighbours = getNeighbours(l); |
| 223 |
for (Location neighbour : neighbours) { |
| 224 |
if (getState(neighbour) == 0) { |
| 225 |
empty.add(neighbour); |
| 226 |
} else if (!processed.contains(neighbour)) { |
| 227 |
toProcess.add(neighbour); |
| 228 |
} |
| 229 |
} |
| 230 |
processed.add(l); |
| 231 |
} |
| 232 |
|
| 233 |
return empty; |
| 234 |
} |
| 235 |
|
| 236 |
public static void main(String[] args) { |
| 237 |
|
| 238 |
Random random = new Random(); |
| 239 |
|
| 240 |
IslandLattice lattice = new IslandLattice(500, 500); |
| 241 |
List<Location> islands = lattice.addIslands(50, random); |
| 242 |
System.out.println("Created " + islands.size() + " islands"); |
| 243 |
|
| 244 |
int growth = 4950; |
| 245 |
|
| 246 |
int attempts = lattice.growIslands(growth, random); |
| 247 |
System.out.println("Took " + attempts + " attempts to grow islands by " + growth); |
| 248 |
|
| 249 |
lattice.smooth(2); |
| 250 |
|
| 251 |
JFrame frame = new JFrame(); |
| 252 |
frame.getContentPane().add(lattice, BorderLayout.CENTER); |
| 253 |
frame.pack(); |
| 254 |
frame.setVisible(true); |
| 255 |
|
| 256 |
} |
| 257 |
} |