# Terrain spikes using diamond square

This topic is 2849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, I've implemented a terrain generation program using the diamond square algorithm and I've noticed that it forms spikes at randomly distributed points around the heightmap.

I'm pretty sure why this is happening at the edges: because when I average 3 points instead of 4 (in the Square routine) I find the centre of that triangle rather than the centre destination point that lies on the edge. I've tried changing this so that it just averages the edge points when on an edge but it still generates these spikes. I have a feeling its either something to do with floating point precision or my Random() function:

(rand()/(float)RAND_MAX) * 2.0f - 1.0f;

I got the algorithm from here: http://www.javaworld.com/javaworld/jw-08-1998/jw-08-step.html?page=2

and I've noticed some of the images of the terrain on that site have also the same artifacts. Is this just a general problem with the algorithm or is it an implementation fault?

##### Share on other sites
I havent tried the algorithm (so it may be a problem with it), but ...

(rand()/(float)RAND_MAX) * 2.0f - 1.0f;

rand() procedure returns int, so to have it correctly written
((float)rand() / (float)RAND_MAX) * 2.0f - 1.0f;

But that is a detail, that shouldn't be needed (as compiler will do that on his own).

I'd test application if you're not dividing by zero ... or get rid of them after I generate fractal (some kind of filtering).

Maybe clever thing how to avoid this could be to generate 2 units wider and longer (e.g. have one more unit on each edge of generated "height map") and not to use edges, but just the inside.
E.g. you generate 514x514 fractal and use <1 - 513> x <1 - 513> intervals from it.

1. 1
2. 2
3. 3
4. 4
Rutin
11
5. 5

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632665
• Total Posts
3007711
• ### Who's Online (See full list)

There are no registered users currently online

×