# =========================================================================== # # Taken from: # # www.contextfreeart.org/gallery/view.php?id=4248 # # Added: 05.09.2021 # =========================================================================== #

CF::Impure = 1 CF::MinimumSize = 0.0001 CF::Size = [ s 9 9 ]

//startshape PLANK [ s 1 3] startshape FLOOR [ x - 3.5 y 3.5 ]

plankdots = 512, 512 plank_size = 1/plankdots,1/plankdots max_dist = sqrt( plankdots^2 + plankdots^2 )

floor_width = 16 floor_height = 9 board_height = 4

//Return 1 if pixel is at edge of plank edge_(i,n,t) = i<t || (i+t) > n edge(i,j)= edge_(i,plankdots,3) || edge_(j,plankdots,2)

//create knot coord vector2 knot()=rand(0,plankdots), rand(0,plankdots)

//2d distance calc d2(i,j)=(i-j)^2 dist(vector2 v1, vector2 v2)=sqrt(d2(v1,v2)+d2(v1,v2))

mindist_(vector2 pos, vector6 K, n, m)=

if ( n < 0, m,
  let( q=(K[n*2], K[n*2+1]); d=dist(pos,q); o=min(m,d);
    mindist_(pos, K, n-1, o )))

mindist(vector2 pos, vector6 K)=mindist_(pos,K,2,∞)

//wave functions saw_(t,period) = 2*(t/period-floor(0.5+t/period)) saw(t,freq,phase) = saw_(t+phase,1/freq)

sine(t,freq,phase) = sin( (freq*360*t) + (360*phase))

shape FLOOR {

O = 0,2,1,3
loop i=floor_width [ x 1 ]
{
  y_off = -1 * (board_height - 1) + 
    O[mod(i,4)]*(0.25*board_height) + 0.2
  len = floor_height + board_height
  STRIP (len) [ y y_off x -4.5]
}

}

shape STRIP ( n ) {

if ( n > 0 )
{
  L = board_height
  PLANK [ s 1 L  ]
  STRIP ( n-L ) [  y -L  ]
}

}

shape PLANK {

knots = knot(), knot(), knot() 

//freqa and phases for distance offset
F = rand(1,7),rand(0.5,1),rand(1),rand(13,21)
P = rand(), rand(), rand()

loop i=plankdots[0] [ x plank_size[0] ]
loop j=plankdots[1] [ y plank_size[1] ]
transform [ s plank_size[0] plank_size[1] ]
{
  D = mindist( (i,j), knots )
  D_norm = D/max_dist

  D_offset = D_norm + 
    sine(i/plankdots[1],F[0],P[0]) * 0.01 +
    sine(i/plankdots[1],F[1],P[1]) * 0.2 +
    sine(j/plankdots[1],F[2],P[2]) * 0.1

  B_saw = if(saw(D_offset,F[3],0)>0.3..1.7, 0..0.5, 0.7..0.95 )

  B = if ( edge(i,j), 0, B_saw )

  SQUARE [ b B sat 0.3..0.9 h 20..55  ]
}

}