Skip to content

Instantly share code, notes, and snippets.

@bmander
Created May 15, 2012 23:14
Show Gist options
  • Save bmander/2705904 to your computer and use it in GitHub Desktop.
Save bmander/2705904 to your computer and use it in GitHub Desktop.
metaball
//make lumpy circles
function makecircle(r){
return function(x,y){
return Math.pow(x,2) + Math.pow(y,2) < Math.pow(r,2);
}
}
function translate(tx, ty, func){
return function(x,y){
var xprime = x-tx;
var yprime = y-ty;
return func(xprime,yprime);
}
}
function nothing(){
return function(x,y){return false};
}
function scale(sx, sy, func){
return function(x,y){
return func(x/sx, y/sy);
}
}
function dist(x,y){
return Math.pow(Math.pow(x,2)+Math.pow(y,2),0.5);
}
function max(a,b){
if(a>b){
return a;
}else{
return b;
}
}
function min(a,b){
if(a<b){
return a;
} else {
return b;
}
}
function bulge(cx,cy,stddev,amp,func){
return function(x,y){
//distance from point to center
var dd = (dist(x-cx,y-cy));
//normal vector from point to center
var ux = (cx-x)/dd;
var uy = (cy-y)/dd;
var indraw=amp*(Math.exp(-Math.pow(dd/stddev,2)));
indraw = min(dd,indraw);
var xp = x+indraw*ux;
var yp = y+indraw*uy;
return func(xp,yp);
}
}
emit( bulge(151,189,100,60,
translate(250,250,
scale(1.6,1.6,
makecircle(75)
)
) ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment