# ========================================================== # Perlin Noise 2D: COPYRIGHT 2002 KEN PERLIN # # mrl.nyu.edu/~perlin/noise/ # mrl.nyu.edu/~perlin/paper445.pdf # www.google.com/patents/US6867776 # # Understanding Perlin Noise see: # http:# flafla2.github.io/2014/08/09/perlinnoise.html # # Some small adjustments to make it work in CF: # - Smaller permutation table. # (CF has a maximum vector length of 99.) # - Different method to prevent # exceeding bounds of array index. # - Clearer Gradient function. # - Shape Last_Pluto_Dream_test_ is for testing. # - Download this source and use it yourself # import Perlin2d96.cfdg # # ———————————————— # Function : Perlin2d( X,Y Oct,Persist) # Parameter: X : X-coordinate # Y : Y-coordinate # Oct : Octaves # Persist : Persistence # ========================================================== P_=17,85,92,90,89,15,77,13,80,95,53, 3, 7,83,19,37

,72, 5,68,84, 8,52,26,43,21,10,23,60, 6,47,81,44
,30,74, 0,27,35,61,94,63,32,12,39,36,11,33,57,22
,34,87,14,16,56,86,69,20,78,38,42,31,67, 9,73,29
,70, 4, 2,48,28, 1,76,58,49,45,82,65,93,88,59,18
,66,50,62,75,91,41,55,46,71,40,24,79,51,54,64,25
,17

number Grad_(natural hash, X, Y)= select(mod(hash,4),X+Y, -X+Y, X-Y, -X-Y)

number Fade_(t)= t*t*t*(t*(t*6-15)+10) number In(X, A, B)= A + X*(B - A) number Noise_(X, Y)=let(

xf= floor(X); x0= min(xf-96*floor(xf/96),95);
yf= floor(Y); y0= min(yf-96*floor(yf/96),95);
xi= X-xf; U = Fade_(xi);
yi= Y-yf; V = Fade_(yi);

A =mod(P_[x0  ]+y0,96);
B =mod(P_[x0+1]+y0,96);
Interpolate=    #  range -1..1
In(V,In(U,Grad_(P_[A  ],xi  ,yi  )
         ,Grad_(P_[B  ],xi-1,yi  ))
    ,In(U,Grad_(P_[A+1],xi  ,yi-1)
         ,Grad_(P_[B+1],xi-1,yi-1)));
(Interpolate+1)/2  #  bound to 0..1

)

Octave_(X,Y, Oct,Persist, Total,Freq,Ampl,Max)= if( !Oct, Total/Max,

  Octave_(X,Y, Oct-1, Persist,
          Total+Ampl*Noise_(X*Freq,Y*Freq),
          Freq*2, Ampl*Persist, Max+Ampl)
)

Perlin2d(X,Y, Oct,Persist)= let( Total= 0; Freq = 1;

  Ampl = 1; Max  = 0; #  to normalize to 0..1
  Octave_(X,Y,Oct,Persist,Total,Freq,Ampl,Max)
)

# ========================================================== # Test of function Perlin2d # ———————————————— # Render to size # Width: 800 Heigth: 600 # Image border size: variable # ==========================================================

startshape Last_Pluto_Dream_test_ shape Last_Pluto_Dream_test_ { Rand=rand(12)

Octaves=6
Persistence=0.65
loop  Y= 0, 25, 0.02 []
loop  X=-4,  4, 0.02 []
{ p=Perlin2d(X*sqrt(1+Y)/12+Rand,
             Y*sqrt(1+Y)/4 +Rand,
             Octaves, Persistence)
  X1=X+0.1*p*(40-Y)/15
  Y1=sqrt(3*Y+4*p)
  H  =65-0.25*(p-0.5)*360
  Sat=0.2+p
  B  =0.7-Y/40-(abs(p-0.5)^0.35)
  if (Y1>2&&Y1<8)
    CIRCLE[x X1 Y1 -Y s 0.06
           h H sat Sat b B a -0.5]

} }