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