java - Fractal Terrain always has a Mountain on the bottom -
i working on project, , i'm done terrain generation, have small problem. bottom of terrain mountain. nothing have tried has worked. efficiency improvements welcome well.
here algorithm:
private void generateterrain() { random rand = new random(); int seed = rand.nextint(panel.maxseed - panel.minseed) + panel.minseed; int offset = 10000; int sidelength = panel.mapsize - 1; int halfsidelength; int average; int ytop; int ybottom; panel.map[0][0] = seed; panel.map[panel.mapsize - 1][0] = seed; panel.map[0][panel.mapsize - 1] = seed; panel.map[panel.mapsize - 1][panel.mapsize - 1] = seed; while (sidelength > 0) { halfsidelength = sidelength / 2; (int x = 0; x < panel.mapsize - 1; x += sidelength) { (int y = 0; y < panel.mapsize - 1; y += sidelength) { average = panel.map[x][y] + panel.map[x + sidelength][y] + panel.map[x][y + sidelength] + panel.map[x + sidelength][y + sidelength]; average /= 4; if (rand.nextboolean()) { average += rand.nextint(offset); } else { average -= rand.nextint(offset); } panel.map[x + halfsidelength][y + halfsidelength] = average; ytop = y - halfsidelength; ybottom = y + halfsidelength; if (ytop < 0) ytop = y + halfsidelength; if (ybottom > panel.mapsize - 1) ybottom = y - halfsidelength; average = panel.map[x][y] + panel.map[x + sidelength][y] + panel.map[x + halfsidelength][ytop] + panel.map[x + halfsidelength][ybottom]; average /= 4; panel.map[x + halfsidelength][y] = average; } } (int x = 0; x < panel.mapsize; x += sidelength) { (int y = 0; y < panel.mapsize - 1; y += sidelength) { int xright = x + halfsidelength; if (xright > panel.mapsize - 1) xright = x - halfsidelength; average = panel.map[x][y] + panel.map[x][y + sidelength] + panel.map[xright][y + halfsidelength] + panel.map[xright][y + halfsidelength]; average /= 4; panel.map[x][y + halfsidelength] = average; } } offset *= 0.5; sidelength /= 2; if (offset < 1) { offset = 1; } } (int x = 0; x < panel.mapsize - 1; x++) { (int y = 0; y < panel.mapsize - 1; y++) { panel.map[x][y] = (int) ((panel.colors.length - 1) - panel.map[x][y] / 500); if (panel.map[x][y] < 0) { panel.map[x][y] = 0; } else if (panel.map[x][y] > 20) { panel.map[x][y] = 20; } } } panel.drawimage(); }
i see have incorporated of suggestions made original code.
the solution have proposed has 3 loops. have joined first 2 loops, limits of loops different. means don't set values bottom line. (these initialised zero, come out mountains in final map.)
it's fence-post error. illustrate:
+-----+-----+-----+-----+ | | | | | | | | | | +-----+-----+-----+-----+ | | | | | | | | | | +-----+-----+-----+-----+ | | | | | | | | | | +-----+-----+-----+-----+ | | | | | | | | | | +-----+-----+-----+-----+
in 4×4 map,
- the first loop treats centre points 4×4 squares,
- the second loop treats midpoints of 4×5 horizontal lines and
- the third loop treats midpoints of 5×4 vertical lines.
another issue last loop calculates colour indices height values should include map values on right , bottom borders, maybe need them auxiliary values terrain creation.
Comments
Post a Comment