// Compiled by ClojureScript 1.9.562 {}
goog.provide('weathergen.math');
goog.require('cljs.core');
weathergen.math.low_bits = (function weathergen$math$low_bits(x,n){
return (x & (((1) << n) - (1)));
});
weathergen.math.high_bits = (function weathergen$math$high_bits(x,n){
return (x >> ((32) - n));
});
weathergen.math.bit_rotate_right = (function weathergen$math$bit_rotate_right(x,n){
return (weathergen.math.high_bits.call(null,x,((32) - n)) + (weathergen.math.low_bits.call(null,x,n) << ((32) - n)));
});
weathergen.math.bit_rotate_left = (function weathergen$math$bit_rotate_left(x,n){
return (weathergen.math.high_bits.call(null,x,n) + (weathergen.math.low_bits.call(null,x,((32) - n)) << n));
});
weathergen.math.frac = (function weathergen$math$frac(x){
return (x - cljs.core.long$.call(null,x));
});
weathergen.math.whole = cljs.core.long$;
weathergen.math.mean = (function weathergen$math$mean(x,y){
return ((x + y) / 2.0);
});
weathergen.math.interpolate = (function weathergen$math$interpolate(x,y,f){
return (((1.0 - f) * x) + (f * y));
});
weathergen.math.vector_interpolate = (function weathergen$math$vector_interpolate(v1,v2,x,x1,x2){
var f = ((x - x1) / (x2 - x1));
var interp = ((function (f){
return (function (a,b){
return ((((1) - f) * a) + (f * b));
});})(f))
;
return cljs.core.mapv.call(null,interp,v1,v2);
});
weathergen.math.vector_add = (function weathergen$math$vector_add(var_args){
var args__24566__auto__ = [];
var len__24559__auto___28987 = arguments.length;
var i__24560__auto___28988 = (0);
while(true){
if((i__24560__auto___28988 < len__24559__auto___28987)){
args__24566__auto__.push((arguments[i__24560__auto___28988]));

var G__28989 = (i__24560__auto___28988 + (1));
i__24560__auto___28988 = G__28989;
continue;
} else {
}
break;
}

var argseq__24567__auto__ = ((((0) < args__24566__auto__.length))?(new cljs.core.IndexedSeq(args__24566__auto__.slice((0)),(0),null)):null);
return weathergen.math.vector_add.cljs$core$IFn$_invoke$arity$variadic(argseq__24567__auto__);
});

weathergen.math.vector_add.cljs$core$IFn$_invoke$arity$variadic = (function (vs){
return cljs.core.apply.call(null,cljs.core.mapv,cljs.core._PLUS_,vs);
});

weathergen.math.vector_add.cljs$lang$maxFixedArity = (0);

weathergen.math.vector_add.cljs$lang$applyTo = (function (seq28986){
return weathergen.math.vector_add.cljs$core$IFn$_invoke$arity$variadic(cljs.core.seq.call(null,seq28986));
});

weathergen.math.scramble = (function weathergen$math$scramble(var_args){
var args28990 = [];
var len__24559__auto___28993 = arguments.length;
var i__24560__auto___28994 = (0);
while(true){
if((i__24560__auto___28994 < len__24559__auto___28993)){
args28990.push((arguments[i__24560__auto___28994]));

var G__28995 = (i__24560__auto___28994 + (1));
i__24560__auto___28994 = G__28995;
continue;
} else {
}
break;
}

var G__28992 = args28990.length;
switch (G__28992) {
case 1:
return weathergen.math.scramble.cljs$core$IFn$_invoke$arity$1((arguments[(0)]));

break;
case 2:
return weathergen.math.scramble.cljs$core$IFn$_invoke$arity$2((arguments[(0)]),(arguments[(1)]));

break;
default:
throw (new Error([cljs.core.str.cljs$core$IFn$_invoke$arity$1("Invalid arity: "),cljs.core.str.cljs$core$IFn$_invoke$arity$1(args28990.length)].join('')));

}
});

weathergen.math.scramble.cljs$core$IFn$_invoke$arity$1 = (function (x){
return weathergen.math.scramble.call(null,x,(1));
});

weathergen.math.scramble.cljs$core$IFn$_invoke$arity$2 = (function (x,seed){
var a = Math.abs(Math.sin((x * seed)));
var b = (a * 100000.0);
return weathergen.math.frac.call(null,b);
});

weathergen.math.scramble.cljs$lang$maxFixedArity = 2;

weathergen.math.discrete_noise_field = (function weathergen$math$discrete_noise_field(var_args){
var args28997 = [];
var len__24559__auto___29000 = arguments.length;
var i__24560__auto___29001 = (0);
while(true){
if((i__24560__auto___29001 < len__24559__auto___29000)){
args28997.push((arguments[i__24560__auto___29001]));

var G__29002 = (i__24560__auto___29001 + (1));
i__24560__auto___29001 = G__29002;
continue;
} else {
}
break;
}

var G__28999 = args28997.length;
switch (G__28999) {
case 2:
return weathergen.math.discrete_noise_field.cljs$core$IFn$_invoke$arity$2((arguments[(0)]),(arguments[(1)]));

break;
case 3:
return weathergen.math.discrete_noise_field.cljs$core$IFn$_invoke$arity$3((arguments[(0)]),(arguments[(1)]),(arguments[(2)]));

break;
default:
throw (new Error([cljs.core.str.cljs$core$IFn$_invoke$arity$1("Invalid arity: "),cljs.core.str.cljs$core$IFn$_invoke$arity$1(args28997.length)].join('')));

}
});

weathergen.math.discrete_noise_field.cljs$core$IFn$_invoke$arity$2 = (function (x,y){
return weathergen.math.discrete_noise_field.call(null,x,y,(1));
});

weathergen.math.discrete_noise_field.cljs$core$IFn$_invoke$arity$3 = (function (x,y,seed){
return weathergen.math.scramble.call(null,((x * (65521)) + y),seed);
});

weathergen.math.discrete_noise_field.cljs$lang$maxFixedArity = 3;

weathergen.math.continuous_noise_field = (function weathergen$math$continuous_noise_field(var_args){
var args29004 = [];
var len__24559__auto___29007 = arguments.length;
var i__24560__auto___29008 = (0);
while(true){
if((i__24560__auto___29008 < len__24559__auto___29007)){
args29004.push((arguments[i__24560__auto___29008]));

var G__29009 = (i__24560__auto___29008 + (1));
i__24560__auto___29008 = G__29009;
continue;
} else {
}
break;
}

var G__29006 = args29004.length;
switch (G__29006) {
case 2:
return weathergen.math.continuous_noise_field.cljs$core$IFn$_invoke$arity$2((arguments[(0)]),(arguments[(1)]));

break;
case 3:
return weathergen.math.continuous_noise_field.cljs$core$IFn$_invoke$arity$3((arguments[(0)]),(arguments[(1)]),(arguments[(2)]));

break;
default:
throw (new Error([cljs.core.str.cljs$core$IFn$_invoke$arity$1("Invalid arity: "),cljs.core.str.cljs$core$IFn$_invoke$arity$1(args29004.length)].join('')));

}
});

weathergen.math.continuous_noise_field.cljs$core$IFn$_invoke$arity$2 = (function (x,y){
return weathergen.math.continuous_noise_field.call(null,x,y,1.0);
});

weathergen.math.continuous_noise_field.cljs$core$IFn$_invoke$arity$3 = (function (x,y,seed){
var x_frac = cljs.core.mod.call(null,weathergen.math.frac.call(null,x),1.0);
var y_frac = cljs.core.mod.call(null,weathergen.math.frac.call(null,y),1.0);
var x_whole = Math.floor(x);
var y_whole = Math.floor(y);
return weathergen.math.interpolate.call(null,weathergen.math.interpolate.call(null,weathergen.math.discrete_noise_field.call(null,x_whole,y_whole,seed),weathergen.math.discrete_noise_field.call(null,(x_whole + (1)),y_whole,seed),x_frac),weathergen.math.interpolate.call(null,weathergen.math.discrete_noise_field.call(null,x_whole,(y_whole + (1)),seed),weathergen.math.discrete_noise_field.call(null,(x_whole + (1)),(y_whole + (1)),seed),x_frac),y_frac);
});

weathergen.math.continuous_noise_field.cljs$lang$maxFixedArity = 3;

weathergen.math.fractal_field = (function weathergen$math$fractal_field(var_args){
var args29011 = [];
var len__24559__auto___29014 = arguments.length;
var i__24560__auto___29015 = (0);
while(true){
if((i__24560__auto___29015 < len__24559__auto___29014)){
args29011.push((arguments[i__24560__auto___29015]));

var G__29016 = (i__24560__auto___29015 + (1));
i__24560__auto___29015 = G__29016;
continue;
} else {
}
break;
}

var G__29013 = args29011.length;
switch (G__29013) {
case 3:
return weathergen.math.fractal_field.cljs$core$IFn$_invoke$arity$3((arguments[(0)]),(arguments[(1)]),(arguments[(2)]));

break;
case 5:
return weathergen.math.fractal_field.cljs$core$IFn$_invoke$arity$5((arguments[(0)]),(arguments[(1)]),(arguments[(2)]),(arguments[(3)]),(arguments[(4)]));

break;
default:
throw (new Error([cljs.core.str.cljs$core$IFn$_invoke$arity$1("Invalid arity: "),cljs.core.str.cljs$core$IFn$_invoke$arity$1(args29011.length)].join('')));

}
});

weathergen.math.fractal_field.cljs$core$IFn$_invoke$arity$3 = (function (x,y,zoom){
return weathergen.math.fractal_field.call(null,x,y,zoom,(1),1.0);
});

weathergen.math.fractal_field.cljs$core$IFn$_invoke$arity$5 = (function (x,y,zoom,seed,floor){
var result = (0);
var z = zoom;
while(true){
if((z < floor)){
return result;
} else {
var G__29018 = (result + (weathergen.math.continuous_noise_field.call(null,(x / z),(y / z),seed) * ((z / zoom) / 2.0)));
var G__29019 = (z / (2));
result = G__29018;
z = G__29019;
continue;
}
break;
}
});

weathergen.math.fractal_field.cljs$lang$maxFixedArity = 5;

weathergen.math.magnitude = (function weathergen$math$magnitude(p__29020){
var vec__29024 = p__29020;
var x = cljs.core.nth.call(null,vec__29024,(0),null);
var y = cljs.core.nth.call(null,vec__29024,(1),null);
return Math.sqrt(((x * x) + (y * y)));
});
weathergen.math.heading = (function weathergen$math$heading(p__29027){
var vec__29031 = p__29027;
var x = cljs.core.nth.call(null,vec__29031,(0),null);
var y = cljs.core.nth.call(null,vec__29031,(1),null);
return cljs.core.mod.call(null,((Math.atan2(x,y) * 180.0) / Math.PI),(360));
});
weathergen.math.gradient = (function weathergen$math$gradient(var_args){
var args29034 = [];
var len__24559__auto___29037 = arguments.length;
var i__24560__auto___29038 = (0);
while(true){
if((i__24560__auto___29038 < len__24559__auto___29037)){
args29034.push((arguments[i__24560__auto___29038]));

var G__29039 = (i__24560__auto___29038 + (1));
i__24560__auto___29038 = G__29039;
continue;
} else {
}
break;
}

var G__29036 = args29034.length;
switch (G__29036) {
case 4:
return weathergen.math.gradient.cljs$core$IFn$_invoke$arity$4((arguments[(0)]),(arguments[(1)]),(arguments[(2)]),(arguments[(3)]));

break;
case 5:
return weathergen.math.gradient.cljs$core$IFn$_invoke$arity$5((arguments[(0)]),(arguments[(1)]),(arguments[(2)]),(arguments[(3)]),(arguments[(4)]));

break;
default:
throw (new Error([cljs.core.str.cljs$core$IFn$_invoke$arity$1("Invalid arity: "),cljs.core.str.cljs$core$IFn$_invoke$arity$1(args29034.length)].join('')));

}
});

weathergen.math.gradient.cljs$core$IFn$_invoke$arity$4 = (function (x,y,field,delta){
return weathergen.math.gradient.call(null,x,y,field,delta,field.call(null,x,y));
});

weathergen.math.gradient.cljs$core$IFn$_invoke$arity$5 = (function (x,y,field,delta,f0){
var fx = field.call(null,(x + delta),y);
var fy = field.call(null,x,(y + delta));
var dfdx = ((fx - f0) / delta);
var dfdy = ((fy - f0) / delta);
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [dfdx,dfdy], null);
});

weathergen.math.gradient.cljs$lang$maxFixedArity = 5;

weathergen.math.clamp = (function weathergen$math$clamp(min,max,val){
if((val < min)){
return min;
} else {
if((max < val)){
return max;
} else {
return val;

}
}
});
/**
 * Round x to the nearest n. E.g. (nearest 10 15) => 20
 */
weathergen.math.nearest = (function weathergen$math$nearest(x,n){
return (Math.round((x / n)) * n);
});
weathergen.math.scale = (function weathergen$math$scale(c,v){
return cljs.core.mapv.call(null,(function (p1__29041_SHARP_){
return (c * p1__29041_SHARP_);
}),v);
});
weathergen.math.deg__GT_rad = (function weathergen$math$deg__GT_rad(deg){
return ((deg * Math.PI) / 180.0);
});
weathergen.math.rotate = (function weathergen$math$rotate(deg,p__29042){
var vec__29046 = p__29042;
var x = cljs.core.nth.call(null,vec__29046,(0),null);
var y = cljs.core.nth.call(null,vec__29046,(1),null);
var rad = weathergen.math.deg__GT_rad.call(null,(- deg));
var cs = Math.cos(rad);
var sn = Math.sin(rad);
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [((x * cs) - (y * sn)),((x * sn) + (y * cs))], null);
});
weathergen.math.normalize = (function weathergen$math$normalize(v){
var m = weathergen.math.magnitude.call(null,v);
if((m === (0))){
return v;
} else {
return cljs.core.mapv.call(null,((function (m){
return (function (p1__29049_SHARP_){
return (p1__29049_SHARP_ / m);
});})(m))
,v);
}
});
/**
 * Map x via a sort of gamma-shaped distribution. Shape determines how
 *   close to the mean the values will cluster - lower values spread them
 *   out more. Values below one will actually make a U-shaped
 *   distribution, with values towards the min and max more likely.
 */
weathergen.math.distribute = (function weathergen$math$distribute(x,min,mean,max,shape){
var x1 = ((x * (2)) - (1));
var x2 = Math.pow(Math.abs(x1),shape);
if((x1 < (0))){
return ((((1) - x2) * (mean - min)) + min);
} else {
return ((x2 * (max - mean)) + mean);
}
});
/**
 * Take a number in the range [0,1], presumably normally distributed
 *   with a mean of 0.5, and 'spread' it so that the tails are thrown
 *   away and the remainder of the distribution stretched into it.
 *   `spread` determins the amount - zero does not change the
 *   distribution and 1 throws away all but the mean.
 */
weathergen.math.reject_tails = (function weathergen$math$reject_tails(spread,x){
var x1 = (((x - 0.5) * (2)) * ((1) / ((1) - spread)));
return ((weathergen.math.clamp.call(null,(-1),(1),x1) / (2)) + 0.5);
});
