Hey guys,
I am having troubles with the Diamond-Square algorithm, which is why i started messing about with this simple program, and it has helped my understanding of how it works alot, so I decided to post it here for others to maybe learn a little from.
It's basically the first step on the road to learning the Diamond-Square algorithm to make a heightmap for 3D landscapes, or clouds.
The program generates an array of height-values that looks like a landscape or mountains if drawn as series of dots.
The next step is ofcourse to generate a 2D array to use as a heightmap for 3D terrains.
I have commented the code as best I could, although I'm not always great at making good comments.

If you have any questions about this snippet, go ahead and ask.
EDIT: Forgot to put the code in!
sync on : sync rate 0
randomize timer()
H as FLOAT
H = 1.0 REM - H decides the initial jaggedness of the landscape.
wid = 512 REM - Width of the landscape.
dim a(wid) REM - Create an array to hold the height values.
q = rnd(255) REM - Randomize the height of the edges.
a(0) = q
a(wid) = q
m=1 REM - Keeps track of how many points we need to fill in.
thetime = timer() REM - Just for seeing how slow/fast it generates.
while m<=wid/2 REM - Keep running until we have filled in all the dots.
iter = m*2 REM - Keeps track of which points we need to avg.
for i=1 to iter-1 step 2
sSize = (wid/m)/2 REM - Size of each segment.
cSpot = sSize * i REM - The current point. (I called them spots :P)
pSpot = sSize * (i-1) REM - The previous point to use in averaging.
nSpot = sSize * (i+1) REM - The next point to use in averaging.
a(cSpot) = ((a(pSpot) + a(nSpot)) / 2) REM - Average current point using previous and next point. (p+n)/2
u#=rnd(16384)/8192.0-1.0 REM - Make a random nr. from -1 to 1. Thanks GG. :P
a(cSpot) = a(cSpot) + ((rnd(512)*H)*u#) REM - Add some randomness to the landscape.
if a(cSpot) > 255 then a(cSpot) = 255 REM - Make sure the values are withing 0-255. (byte range)
if a(cSpot) < 0 then a(cSpot) = 0
dot cSpot,a(cSpot) REM - Not really necessary during creation... just to show how it runs...
sync
next i
m = m*2 REM - Subdivide the range. Meaning we start with 1 segment. then 2,4,8,16 and so on...
H = H / 2 REM - Make the randomness smaller and smaller to smooth out the landscape.
endwhile
text 0,300,"Time elapsed(ms): "+str$(timer() - thetime)
line 0,256,wid-1,256
sync
wait key
end
- enderleit
[href]www.eleit.dk[/href]