Showing posts with label lerp. Show all posts
Showing posts with label lerp. Show all posts

Sunday, February 11, 2018

Morphing between two 2D shapes with the same number of points.

Morph from one set (of points) to another.
Softcurve()makes transition ease in and out.


for(i=[-0:0.1:1])
{
morphed=lerp(rabbit(),elephant(),softcurve(i)) ;
translate([i*200,0,0])
polygon( morphed);
}

function lerp(start,end,bias)=(end*bias+start*(1-bias));
function softcurve(a)=let(b=clamp(a))(b*b*(3-2*b));
function clamp(a,b=0,c=10)=min(max(a,b),c);
  

function rabbit()=[[-4.76,-3.76],[-5.76,-5.76],[-4.76,-9.76],[0.24,-9.76],[4.24,-9.76],[6.24,-5.76],[6.24,-3.76],[4.24,-2.76],[2.24,-1.76],[4.24,-0.76],[5.24,1.24],[3.24,3.24],[2.24,3.24],[3.24,5.24],[3.24,7.24],[1.24,6.24],[0.24,3.24],[-0.76,3.24],[-1.76,3.24],[-2.76,6.24],[-7.76,6.24],[-4.76,4.24],[-3.76,2.24],[-5.76,0.24],[-3.76,-1.76]]
+[for(i=[0: (24) ])rands(-1,1,2)]*0.5;
function elephant()=[[-11,-5.84],[-8,-5.84],[-8,-0.84],[-4,-1.84],[0,-1.84],[1,-0.84],[1,-5.84],[4,-5.84],[4,-0.84],[7,-4.84],[10,-4.84],[10,-2.84],[8,-2.84],[6,0.16],[8,4.16],[6,7.16],[3,6.16],[1,9.16],[-2,8.16],[-2,5.16],[1,4.16],[-3,4.16],[-10,3.16],[-11,-0.84],[-11,-5.84]]
+[for(i=[0: (24) ])rands(-1,1,2)]*0.5;


Wednesday, October 11, 2017

0 - 1 Remapping Functions: Compressor / Expander / Biramp

/* remapping functions */

v1=rnd();v2=rnd(); 

for(i=[-0:1/160:1])translate([i,0])square([1/160,  ((  comp(i,v1,v2)))]);
    function comp(i,c=0.5,s=0.75)=i<c? ramp(i,0,c,0,s) :ramp(i,c,1,s,1) ;
    function ramp(v,bi1=1/3,bi2=2/3,start=1/3, end=2/3) = 
        let(b1=min(bi1,bi2),b2=max(bi1,bi2))
        v<=b1?start:v<=b2?lerp(start,end,(v-b1)/(b2-b1))    :end;
     function lerp(start, end, bias) = (end * bias + start * (1 - bias));

function rnd(a = 1, b = 0, s = []) = 
  s == [] ? 
   (rands(min(a, b), max(   a, b), 1)[0]) 
  : 
   (rands(min(a, b), max(a, b), 1, s)[0])
  ; 



Wednesday, September 13, 2017

0 - 1 Remapping Functions: Ramp

/* remapping functions */


for(i=[-0:1/160:1])translate([i,0])
        square([1/160, (ramp( (i),1/3,2/3,0.1)) ]);
function ramp(v,bi1=1/3,bi2=2/3,start=1/3, end=2/3) = 
        let(b1=min(bi1,bi2),b2=max(bi1,bi2))
        v<=b1?start:v<=b2?lerp(start,end,(v-b1)/(b2-b1))    :end;
function lerp(start, end, bias) = (end * bias + start * (1 - bias));
 

Friday, July 14, 2017

List Lerp

Linear interpolation over the items in a list

list=[[1,6],[3,5],[5,3],[1,2],[0,6]];

for(x=[0.01:0.05:4]){
echo(listlerp (list,x));
translate(listlerp (list,x))sphere(0.1);}

function listlerp (l,I)=let(
f=len(l)-1,start=max(0,min(f,floor(I))),
end=max(0,min(f,ceil(I))),bias=I%1) 
(l[end]* bias + l[start] * (1 - bias));

 

Thursday, June 1, 2017

1 dimensional noise

Singel dimension noise layered in three octaves.
Return value is 0-1.

Usage:



seed=rnd(12);
for (a=[-15:0.1:15])  translate([a,0,0])  cube([0.1,0.1, noise_1d(a+seed)]);

function  noise_1d(i,
low_range=1/4,mid_range=1,hi_range=8,
low_magnitude=1/2,mid_magnitude=0.25,hi_magnitude=0.5)=
 (  
single_noise_1d(i/low_range)*low_magnitude+
single_noise_1d(i/mid_range)*mid_magnitude+
single_noise_1d(i/hi_range)*hi_magnitude

)/(low_magnitude+mid_magnitude+hi_magnitude);


function single_noise_1d(i)=
let(
a=floor(i),
b=ceil(i),
c=smooth_curve(i-a),
arnd=rnd(1,0,a),
brnd=rnd(1,0,b),
out=lerp(arnd,brnd,c)
)
out
;

function smooth_curve(a) =let (b = clamp(a))(b * b * (3 - 2 * b));
function clamp(a, b = 0, c = 1) = min(max(a, b), c);
function lerp(start, end, bias) = (end * bias + start * (1 - bias));
function rnd(a = 1, b = 0, s = []) = 
  s == [] ? 
   (rands(min(a, b), max(   a, b), 1)[0]) 
  : 
   (rands(min(a, b), max(a, b), 1, s)[0])
  ; 


Friday, May 12, 2017

Linear interpolation - Lerp

Lerp is very powerful.
Linear interpolation works on values , point, any pair of list as long as they have same length and substructure. Bias usually goes between 0-1. Bias outside that will extrapolate to corresponding values  along the Linear interpolation line. For example you can lerp between two sets of polyhedra points. Combined with Smooth Curve very interesting effects can be achieved. Sometimes Lerp is called mix.

Usage:


my_point=lerp( [x,y] , [5,1] , 0.30 );

function lerp(start, end, bias) = (end * bias + start * (1 - bias));