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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| public class DiamondSquare {
public static void main(String[] args) {
int n=128;
int s=2*n+1;
int h = 1000;
int[][] tab = _gen(s,h);
BufferedImage image = new BufferedImage(s, s, BufferedImage.TYPE_INT_RGB);
Color[] palette = createPalette();
for(int i=0; i<tab.length; i++) {
for(int j=0; j<tab[i].length; j++) {
final int comp = Math.max(0,Math.min((int)(((tab[i][j]+h/2d)/h)*255),255));
final Color color = palette[comp];
image.setRGB(i, j, color.getRGB());
}
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static Color[] createPalette() {
Map<Color,Color> colors = new HashMap<>();
Color[] palette = new Color[256];
for(int i=0; i<256; i++) {
//palette[i]= new Color(i,i,i); // niveaux de gris
if ( i>250 ) {
palette[i] = colors.computeIfAbsent(Color.WHITE, c->c);
}
else if ( i>225 ) {
palette[i] = colors.computeIfAbsent(Color.GRAY, c->c);
}
else if ( i>200 ) {
palette[i] = colors.computeIfAbsent(Color.GREEN.darker().darker(), c->c);
}
else if ( i>175 ) {
palette[i] = colors.computeIfAbsent(Color.GREEN.darker(), c->c);
}
else if ( i>155 ) {
palette[i] = colors.computeIfAbsent(Color.GREEN, c->c);
}
else if ( i>105 ) {
palette[i] = colors.computeIfAbsent(Color.YELLOW.darker(), c->c);
}
else if ( i>95 ) {
palette[i] = colors.computeIfAbsent(Color.ORANGE, c->c);
}
else if ( i>85 ) {
palette[i] = colors.computeIfAbsent(new Color(106,223,230), c->c);
}
else if ( i>70 ) {
palette[i] = colors.computeIfAbsent(new Color(43,120,206), c->c);
}
else {
palette[i] = colors.computeIfAbsent(new Color(52,63,197), c->c);
}
}
return palette;
}
/** implémentation algo wikipedia */
public static int[][] gen(int s, int h) {
int[][] t = new int[s][s];
int h2 = h/2;
int i = s - 1;
t[0][0] = (int)rand(-h2,h2);
t[0][i] = (int)rand(-h2, h2);
t[i][i] = (int)rand(-h2, h2);
t[i][0] = (int)rand(-h2, h2);
while (i > 1) {
int id = i/2;
for(int x = id; x<s; x+=i) {
for(int y = id; y<s; y+=i) {
if ( x-id>=0 && y-id>=0 && x+id<s && y+id<s ) {
double moyenne = ( t[x - id][y - id] + t[x - id][y + id] + t[x + id][y + id] + t[x + id][ y - id] ) / 4d;
t[x][y] = (int)(moyenne + rand(-h2, h2));
}
}
}
for(int x=0; x<s ; x+=id) {
int decalage;
if ( x % i == 0 ) {
decalage = id;
} else {
decalage = 0;
}
for( int y=decalage; y<s; y+=i ) {
double somme = 0;
int n = 0;
if (x >= id) {
somme += t[x - id][y];
n++;
}
if (x + id < s) {
somme += t[x + id][y];
n++;
}
if (y >= id) {
somme += t[x][y - id];
n++;
}
if (y + id < s) {
somme += t[x][y + id];
n++;
}
t[x][y] = (int)(somme / n + rand(-h2, h2));
}
}
i = id;
h2 = h2/2;
}
return t;
}
private static double rand(int min, int max) {
if ( min<max ) {
double rand=ThreadLocalRandom.current().nextDouble(min, max);
return rand;
}
else {
return 0;
}
}
/* ton implémentation corrigée */
public static int[][] _gen(int sizeX, int hightness) {
Random rand = new Random();
int level[][] = new int[sizeX][sizeX] ;
/* inutile : un tableau est rempli de à à l'initialisation
for ( int xx = 0 ; xx < sizeX ; xx ++ ){
for ( int yy = 0 ; yy < sizeX ; yy ++ ){
level[xx][yy] = (int) (0);
}
}*/
// ici : initialiser ces valeurs aléatoirement (ou faire une première passage sur un maillage plus fin pour initialiser une forme de paysage contrainte)
level[0][0] = 100 ;
level[sizeX-1][0] = 100 ;
level[sizeX-1][sizeX-1] = 100 ;
level[0][sizeX-1] = 100 ;
int s = sizeX;
while ( s > 1 ){
int half = s/2 ;
hightness = hightness/2 ;
for ( int xx = half ; xx < sizeX ; xx += s ){
for ( int yy = half ; yy < sizeX; yy += s ){
int moyenne = level[xx-half][yy-half] + level[xx+half][yy-half] + level[xx+half][yy+half] + level[xx-half][yy+half] ;
level[xx][yy] = moyenne / 4 + (int)(((rand.nextFloat()*2-1) * (float)hightness) ) ;
}
}
int modulor = 1;
for (int xx = 0; xx < sizeX; xx += half) {
for (int yy = modulor * half; yy < sizeX; yy += s) {
/*int moyenne = 255 / 2;
int divisor = 1;*/
int moyenne = 0;
int divisor = 0;
if (xx >= 0 && xx < sizeX && yy - half >= 0 && yy - half < sizeX) {
moyenne += level[xx][yy - half];
divisor++;
}
if (xx + half >= 0 && xx + half < sizeX && yy >= 0 && yy < sizeX) {
moyenne += level[xx + half][yy];
divisor++;
}
if (xx >= 0 && xx < sizeX && yy + half >= 0 && yy + half < sizeX) {
moyenne += level[xx][yy + half];
divisor++;
}
if (xx - half >= 0 && xx - half < sizeX && yy >= 0 && yy < sizeX) {
moyenne += level[xx - half][yy];
divisor++;
}
if (xx >= 0 && xx < sizeX && yy + s / 2 >= 0 && yy + s / 2 < sizeX) {
moyenne = moyenne / divisor;
moyenne = moyenne + (int)(((rand.nextFloat()*2-1) * (float)hightness) ) ;
/*if (moyenne < 0) {
moyenne = 0;
} else if (moyenne > 255) {
moyenne = 255;
}*/
level[xx][yy] = moyenne ;
}
}
if (modulor == 1) {
modulor = 0;
} else if (modulor == 0) {
modulor = 1;
}
}
s = half ;
}
return level;
}
} |