Author Topic: Midpoint Displacement Clouds  (Read 2314 times)

Mopz

  • Guest
Midpoint Displacement Clouds
« on: June 20, 2013, 05:36:53 PM »
Here's a program in naalaa that generates clouds with the Midpoint Displacement algorithm:


Code: [Select]
rem Images should be of the size 2^n + 1, that is:
rem
rem   33x33
rem   65x65
rem   129x129
rem   257x257
rem   513x513
rem   ...

set redraw off

rem Generate and display an image.
set caret 320, 0
center "Midpoint Displacement Cloud"
center
center "Press any key to continue ..."
proc MD_GenerateImage 1, 257, 1.0, 0.3, 1
draw image 1, 192, 100

redraw
wait keydown

rem Show how the contrast parameter works.
for i = 0 to 100
proc MD_GenerateImage 1, 257, float(i)*0.01, 0.3, 3

set color 0, 0, 0
cls
set color 255, 255, 255
set caret 320, 0
center "Midpoint Displacement Cloud"
center "Contrast = ", float(i)*0.01
draw image 1, 192, 100

wait 0
redraw
next

center
center "Press any key to continue ..."
redraw
wait keydown

rem Show how the smooth parameter works.
for i = 0 to 100
proc MD_GenerateImage 1, 257, 1.0, float(i)*0.01, 56

set color 0, 0, 0
cls
set color 255, 255, 255
set caret 320, 0
center "Midpoint Displacement Cloud"
center "Smooth = ", float(i)*0.01
draw image 1, 192, 100

wait 0
redraw
next

center
center "Press any key to exit ..."
redraw
wait keydown


rem ==================================================================
rem Generate image. contrast and smooth should both be in the
rem range [0..1].
rem ==================================================================
procedure MD_GenerateImage(img, size, contrast#, smooth#, seed)
a#[][] = MD_Generate(size, contrast, smooth, seed)
create image img, size, size
set image img
for y = 0 to size - 1
for x = 0 to size - 1
i = int(a[x][y]*255.0)
set color i, i, i
set pixel x, y
next
next
set image primary
endproc

rem ==================================================================
rem Generate array with elements in the range [0..1].
rem ==================================================================
function MD_Generate#[][](size, contrast#, smooth#, seed)
randomize seed
contrast = min#(contrast, 1.0)
contrast = max#(contrast, 0.0)
smooth = min#(smooth, 1.0)
smooth = max#(smooth, 0.0)
smooth = 1.0 - smooth
md#[size][size]
md[0][0] = 0.5 + contrast*float(rnd(100))*0.01 - contrast*0.5
md[size - 1][0] = 0.5 + contrast*float(rnd(100))*0.01 - contrast*0.5
md[size - 1][size - 1] = 0.5 + contrast*float(rnd(100))*0.01 - contrast*0.5
md[0][size - 1] = 0.5 + contrast*float(rnd(100))*0.01 - contrast*0.5
proc MD_Rec md, 0, 0, size - 1, size - 1, contrast, smooth
return md
endfunc

rem ==================================================================
rem Recursive step.
rem ==================================================================
procedure MD_Rec(&md#[][], xmin, ymin, xmax, ymax, contrast#, smooth#)
if xmax - xmin <= 1 then return
if ymax - ymin <= 1 then return
hw = (xmin + xmax)/2
hh = (ymin + ymax)/2
md[hw][hh] = (md[xmin][ymin] + md[xmax][ymin] + md[xmax][ymax] + md[xmin][ymax])*0.25 + contrast*float(rnd(100))*0.01 - contrast*0.5
md[hw][hh] = max#(md[hw][hh], 0.0)
md[hw][hh] = min#(md[hw][hh], 1.0)
md[xmin][hh] = (md[xmin][ymin] + md[xmin][ymax])*0.5
md[xmax][hh] = (md[xmax][ymin] + md[xmax][ymax])*0.5
md[hw][ymin] = (md[xmin][ymin] + md[xmax][ymin])*0.5
md[hw][ymax] = (md[xmin][ymax] + md[xmax][ymax])*0.5
proc MD_Rec md, xmin, ymin, hw, hh, contrast*smooth, smooth
proc MD_Rec md, hw, ymin, xmax, hh, contrast*smooth, smooth
proc MD_Rec md, xmin, hh, hw, ymax, contrast*smooth, smooth
proc MD_Rec md, hw, hh, xmax, ymax, contrast*smooth, smooth
endproc

See the attached image generated by the program.

EDIT: This is just a test implementation of the algorithm described here, http://en.wikipedia.org/wiki/Midpoint_displacement_algorithm, it's not optimized in any way and I sort of made up the contrast and smooth parameters myself.

EDIT 2: Modified the program for showing what the contrast and smooth parameters do.
« Last Edit: June 21, 2013, 08:17:40 AM by Mopz »

cvirus

  • Guest
Re: Midpoint Displacement Clouds
« Reply #1 on: June 20, 2013, 07:02:27 PM »
Bad weather comming this way :).

Cool  ;D

Mopz

  • Guest
Re: Midpoint Displacement Clouds
« Reply #2 on: June 20, 2013, 07:11:34 PM »
Bad weather comming this way :).

Cool  ;D

"Cloudy with a chance of meatballs" is actually my fav movie alltimes :)