November 28, 2020, 01:41:54 PM

Author Topic: [bb] Midpoint Displacement Algorithm by Pakz [ 1+ years ago ]  (Read 717 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Midpoint Displacement Algorithm
Author : Pakz
Posted : 1+ years ago

Description :

I found this code more then 10 years ago and used it a couple of times after converting it from c++ I think. I converted it to Blitz, java and back to blitz just yet. I hope I did not break it.

The code makes heightmaps.


Code :
Code: BlitzBasic
  1. ; Midpoint Displacement
  2. Graphics 640,480,32,2
  3. SetBuffer BackBuffer()
  4.  
  5. Global mapwidth = 100
  6. Global mapheight = 100
  7. Dim map(mapwidth,mapheight)
  8.  
  9. domidpoint
  10. tw = GraphicsWidth()/mapwidth
  11. th = GraphicsHeight()/mapheight
  12. For y=0 To mapheight
  13. For x=0 To mapwidth
  14.         Color map(x,y),map(x,y),map(x,y)
  15.         Rect x*tw,y*th,tw,th,True
  16. Next
  17. Next
  18.  
  19. Flip
  20. WaitKey
  21. End
  22.  
  23. ; Here the midpoint code begins.
  24. Function domidpoint()
  25.   ; Erase the old map array..
  26.   For y = 0 To mapheight
  27.   For x = 0 To mapwidth
  28.     map(x,y)=0
  29.   Next
  30.   Next
  31.   ; Setup points in the 4 corners of the map.
  32.   map(0,0) = 128
  33.   map(mapwidth,0) = 128
  34.   map(mapwidth,mapheight) = 128
  35.   map(0,mapheight) = 128
  36.   ; Do the midpoint
  37.   midpoint(0,0,mapwidth,mapheight)
  38. End Function
  39.  
  40.  ; This is the actual Mid point displacement code.
  41.  Function midpoint(x1,y1,x2,y2)
  42.         ; If this is pointing at just on pixel, Exit because
  43.         ; it doesn't need doing}
  44.         If x2-x1<2 And y2-y1<2 Then Return False
  45.  
  46.         ; Find distance between points And
  47.         ;use when generating a random number.
  48.     dist=(x2-x1+y2-y1)
  49.     hdist=dist / 2
  50.         ;Find Middle Point
  51.     midx=(x1+x2) / 2
  52.     midy=(y1+y2) / 2
  53.         ;Get pixel colors of corners
  54.     c1=map(x1,y1)
  55.     c2=map(x2,y1)
  56.     c3=map(x2,y2)
  57.     c4=map(x1,y2)
  58.  
  59.         ; If Not already defined, work out the midpoints of the corners of
  60.         ; the rectangle by means of an average plus a random number.
  61.     If(map(midx,y1)=0) Then map(midx,y1)=((c1+c2+Rand(dist)-hdist) / 2)
  62.     If(map(midx,y2)=0) Then map(midx,y2)=((c4+c3+Rand(dist)-hdist) / 2)
  63.     If(map(x1,midy)=0) Then map(x1,midy)=((c1+c4+Rand(dist)-hdist) / 2)
  64.     If(map(x2,midy)=0) Then map(x2,midy)=((c2+c3+Rand(dist)-hdist) / 2)
  65.  
  66.         ; Work out the middle point...
  67.     map(midx,midy) = ((c1+c2+c3+c4+Rand(dist)-hdist)/4)
  68.  
  69.         ; Now divide this rectangle into 4, And call again For Each smaller
  70.         ; rectangle
  71.     midpoint(x1,y1,midx,midy);
  72.     midpoint(midx,y1,x2,midy);
  73.     midpoint(x1,midy,midx,y2);
  74.     midpoint(midx,midy,x2,y2);
  75.  
  76. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal