Debian Codegraph @

src/dr/geo/IslandLattice.java

raw symbols SCIP
1 /*
2 * IslandLattice.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Alexei Drummond
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 * @param n the number of pixels to be flipped
114 * @return the locations of unique islands formed, since some may merge
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 * @param n the number of pixels to move from 0 -> 1
138 * @param random
139 * @return the number of attempts taken to grow the current islands the
140 * perscribed amount of land
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 }