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
| public class DiamondSquare {
public static void main(String[] args) {
int t=41;
int n=16;
int s=2*n+1;
int tn = t/s+(t%s==0?0:1);
int h = 1000;
int[][] tab = gen(s,h,tn,t);
BufferedImage image = new BufferedImage(t, t, 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 tn, int t) {
int[][] array = new int[tn*s][tn*s];
for(int i=0; i<tn; i++) {
for(int j=0; j<tn; j++) {
init(array, i*s, j*s, s, h);
}
}
// for(int i=0; i<tn; i++) {
// for(int j=0; j<tn; j++) {
// gen(array, i*s, j*s, s, h);
// }
// }
gen(array,0,0, s*tn, h);
int[][] rarray = new int[t][t];
for(int i=0;i<t; i++) {
for(int j=0;j<t; j++) {
rarray[i][j]=array[i][j];
}
}
return rarray;
}
private static void init(int[][] t, int dx, int dy, int s, int h) {
int h2 = h/2;
int i = s - 1;
t[dx+0][dy+0] = (int)rand(-h2,h2);
t[dx+0][dy+i] = (int)rand(-h2, h2);
t[dx+i][dy+i] = (int)rand(-h2, h2);
t[dx+i][dy+0] = (int)rand(-h2, h2);
}
public static void gen(int[][] t, int dx, int dy, int s, int h) {
int h2 = h/2;
int i = s - 1;
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[dx + x - id][dy + y - id] + t[dx + x - id][dy + y + id] + t[dx + x + id][dy + y + id] + t[dx + x + id][ dy + y - id] ) / 4d;
t[dx+x][dy+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[dx+x - id][dy+y];
n++;
}
if (x + id < s) {
somme += t[dx+x + id][dy+y];
n++;
}
if (y >= id) {
somme += t[dx+x][dy+y - id];
n++;
}
if (y + id < s) {
somme += t[dx+x][dy+y + id];
n++;
}
t[dx+x][dy+y] = (int)(somme / n + rand(-h2, h2));
}
}
i = id;
h2 = h2/2;
}
}
private static double rand(int min, int max) {
if ( min<max ) {
double rand=ThreadLocalRandom.current().nextDouble(min, max);
return rand;
}
else {
return 0;
}
}
} |
Partager