-
Notifications
You must be signed in to change notification settings - Fork 0
/
libnoise.bas
85 lines (85 loc) · 1.81 KB
/
libnoise.bas
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
77
78
79
80
81
82
83
84
85
16000 '
16010 ' libnoise
16020 ' Perlin noise generator
16030 ' Gilbert Francois Duivesteijn
16040 '
16050 ' init: 16000
16060 ' run: 16470
16070 '
16080 ' Init Perlin noise
16090 ' in: NX,NY: nr of cells
16100 ' OX,OY: offset in pxls
16110 ' out: GX,GY: grad in nodes
16120 ' PX,PY: pxls per cell
16130 '
16140 PRINT "[i] Generating random gradient."
16150 T0=TIME
16160 PX=(256-2*OX)/NX
16170 PY=(192-2*OY)/NY
16180 PRINT "[i] px: " + STR$(PX)
16190 PRINT "[i] py: " + STR$(PY)
16200 DIM GX(NX,NY): ' grad dv/dx
16210 DIM GY(NX,NY): ' grad dv/dy
16220 FOR I=0 TO NX
16230 FOR J=0 TO NY
16240 GX(I,J) = 2*RND(1)-1
16250 GY(I,J) = 2*RND(1)-1
16260 NEXT J
16270 NEXT I
16280 T1=TIME
16290 PRINT " Done, " + STR$((T1-T0)/50) + " sec."
16300 RETURN
16310 '
16320 ' Dot gradient
16330 ' in: x,ix,y,iy,gx,gy
16340 ' out: dg
16350 '
16360 DX=XP-IX
16370 DY=YP-IY
16380 DG=GX(IX,IY)*DX + GY(IX,IY)*DY
16390 RETURN
16400 '
16410 ' Interpolate
16420 ' in: A0,A1,w
16430 ' out: A2
16440 '
16450 A2=(A1-A0)*W+A0
16460 RETURN
16470 '
16480 ' Perlin noise
16490 ' in: X, Y, PX, PY, OX, OY
16500 ' out: Z
16510 '
16520 XP=(X-OX)/PX
16530 YP=(Y-OY)/PY
16535 ' point(ix0,iy0)
16540 IX=INT(XP)
16550 IY=INT(YP)
16560 GOSUB 16310
16570 Z0=DG: DG=0
16575 ' point(ix1,iy0)
16580 IX=INT(XP)+1
16590 IY=INT(YP)
16600 GOSUB 16310
16610 Z1=DG: DG=0
16620 ' point(ix0,iy1)
16630 IX=INT(XP)
16640 IY=INT(YP)+1
16650 GOSUB 16310
16660 Z2=DG: DG=0
16670 ' point(ix1,iy1)
16680 IX=INT(XP)+1
16690 IY=INT(YP)+1
16700 GOSUB 16310
16710 Z3=DG: DG=0
16720 ' interpolate
16730 A0=Z0: A1=Z1: W=XP-INT(XP)
16740 GOSUB 16400
16750 Z4=A2: A2=0
16760 A0=Z2: A1=Z3: W=XP-INT(XP)
16770 GOSUB 16400
16780 Z5=A2: A2=0
16790 A0=Z4: A1=Z5: W=YP-INT(YP)
16800 GOSUB 16400
16810 Z=A2: A2=0
16820 RETURN