# =========================================================================== # # === random_tile_shapes_jens_revisited.cfdg # # From: # # www.contextfreeart.org/gallery/view.php?id=4110 # # HEIGHT: 980 # WIDTH: 980 # # =========================================================================== #

// —————————————————— // Based on design 3214 by Jens Persson, May 28, 2013 // ——————————————————

Width = 1600 Height= 900 CF::Size = [s Width Height] CF::Tile = [s Width Height] CF::ColorDepth = 16 CF::Impure = 1 CF::Background = [b -0.2]

// —————————————————— // Functions // —————————————————— intBeg(v1,v2,int) = v1 + (v2-v1)* sqrt(1-(1-int)^2) intCos(v1,v2,int) = v1 + (v2-v1)*(1-cos(int*180))/2 intEnd(v1,v2,int) = v1 + (v2-v1)*(1-sqrt(1- int^2))

fscl(numSeg,seg, sclBeg,sclEnd,int)=let ( c =if(seg==0, 0, if(seg<numSeg-1, 1, 2)) ; select(c, max(0.001,intBeg(sclBeg, sclEnd, int))

, max(0.001,intCos(sclBeg, sclEnd, int))
, max(0.001,intEnd(sclBeg, sclEnd, int))

) )

fnumStpN(RND,sclEnd)=let ( Min = if(RND==0, 50, randint( 50, 200)) ; minSteps = max(floor(sclEnd), Min) ; Max = if(RND==0, 200, randint( Min, 300)) ; randint(minSteps, randint(minSteps, Max)) )

fsclEndN(seg,sclEnd)= if (mod(seg, 2) == 1

, max(10, sclEnd/4) + (rand(1)^2)*min(sclEnd*4,40)
, max( 1, sclEnd/8) + (rand(1)^2)*min(sclEnd  ,40)

)

fang(RND,numStpN)=let ( strMid = if(RND==0, .4 , rand(.001, 2.0 )) ; strVar = if(RND==0, .999, rand(.0 , 0.999)) ; str = strMid*rand(1-strVar,1+strVar) ; angStp = if(RND==0, 72, rand(10, 60)) ; floor(numStpN/(1+str)/angStp)*angStp )

fangEndN(RND,angEnd,ang)=let ( prob = if(RND==0, 1, rand(0.5, 1)) ; angEnd + if( rand(1) < prob

, if ( randint(2) , ang, -ang)
, 0
)

) //—————————————————— // Shapes //—————————————————— startshape scene(1) shape scene(RND) {

transform[x -(Width/2) y -(Height/2) z -1] 
loop   Y=Height [y 4]
loop X=Width  [x 4]
  if (mod(X+Y,2)) CIRCLE[s 2 4.8 b 1]
  else      CIRCLE[s 4.8 2 b 1]

N = floor(Width*Height/if(RND==0, 250, 800))
loop N [z 1] {
H  = select(randint(7),0,10,40,45,55,170,210)
SAT= (360-H)/360*rand(0.1,0.95)^4
B  = rand(0,1)*rand(0,1)^0.0001   
X  = rand(-Width/2 ,Width/2 )
Y  = rand(-Height/2,Height/2)        
S  = 0.4+(1-0.8*B)*rand(0.1,0.5)^2
Z  = N*SAT*B^2*(2-S)
sling(RND)[x X y Y s S h H sat SAT b B z Z]

} }

shape sling(RND) {

MinS= if(RND==0,  2, randint(   2, 10))
MaxS= if(RND==0,  4, randint(MinS, 20))
Prob= if(RND==0,  7, randint(   0,100))
Mult= if(RND==0, 10, randint(   2,  4))
MaxP= if(randint(100)<Prob, MaxS*Mult, MaxS)
numSeg= randint(MinS,MaxP)

Steps = if(RND==0, 45, rand(45, 60))
Offset= if(RND==0, 36, rand( 0,360))
Angle = randint(floor(360/Steps))*Steps + Offset

Min   = if(RND==0,  1, rand(  1,20))
Max   = if(RND==0, 30, rand(Min,50))
numStp= rand(Min, Max)

transform[x (cos(Angle)*numStp) y (sin(Angle)*numStp)]
  seg( RND, numSeg, 0   , numStp, 0
      , 0   , numStp, Angle , Angle) []

}

shape seg( RND, numSeg, seg , numStp, stp

      , sclBeg, sclEnd, angBeg, angEnd) {

int = stp/numStp
scl = fscl(numSeg,seg, sclBeg,sclEnd,int)
CIRCLE[x  (scl*0.1)  y -(scl*0.2) 
   s  (scl*1.50) b -0.85 z -0.2 a -0.98]
CIRCLE[s  (scl*1.25) b -0.25 z -0.1]
CIRCLE[s   scl]
CIRCLE[x -(scl*0.1)  y  (scl*0.2) 
   s  (scl*0.25) b  0.25 z  0.1 sat -0.25]

stpSize = 0.5
ang = intCos(angBeg, angEnd, int)
transform[x (cos(ang)*stpSize) y (sin(ang)*stpSize)]
if (stp < numStp) {
  seg( RND, numSeg, seg  , numStp , stp+1
      , sclBeg, sclEnd , angBeg , angEnd) []
}
else if (seg < numSeg-2) {
  numStpN = fnumStpN(RND,sclEnd)
  sclEndN = fsclEndN(seg,sclEnd)
  ang   = fang(RND,numStpN)
  angEndN = fangEndN(RND,angEnd,ang)
  seg( RND, numSeg, seg+1  , numStpN, 0
      , sclEnd, sclEndN, angEnd , angEndN)[]
}
else if (seg == numSeg-2) {
  seg( RND, numSeg, seg+1  , sclEnd , 0
      , sclEnd, 0    , angEnd , angEnd) []

} }

# runcfdg random_shapes_jens_revisited