// 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__24376__auto__ = [];
var len__24369__auto___30149 = arguments.length;
var i__24370__auto___30150 = (0);
while(true){
if((i__24370__auto___30150 < len__24369__auto___30149)){
args__24376__auto__.push((arguments[i__24370__auto___30150]));

var G__30151 = (i__24370__auto___30150 + (1));
i__24370__auto___30150 = G__30151;
continue;
} else {
}
break;
}

var argseq__24377__auto__ = ((((0) < args__24376__auto__.length))?(new cljs.core.IndexedSeq(args__24376__auto__.slice((0)),(0),null)):null);
return weathergen.math.vector_add.cljs$core$IFn$_invoke$arity$variadic(argseq__24377__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 (seq30148){
return weathergen.math.vector_add.cljs$core$IFn$_invoke$arity$variadic(cljs.core.seq.call(null,seq30148));
});

weathergen.math.scramble = (function weathergen$math$scramble(var_args){
var args30152 = [];
var len__24369__auto___30155 = arguments.length;
var i__24370__auto___30156 = (0);
while(true){
if((i__24370__auto___30156 < len__24369__auto___30155)){
args30152.push((arguments[i__24370__auto___30156]));

var G__30157 = (i__24370__auto___30156 + (1));
i__24370__auto___30156 = G__30157;
continue;
} else {
}
break;
}

var G__30154 = args30152.length;
switch (G__30154) {
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(args30152.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 args30159 = [];
var len__24369__auto___30162 = arguments.length;
var i__24370__auto___30163 = (0);
while(true){
if((i__24370__auto___30163 < len__24369__auto___30162)){
args30159.push((arguments[i__24370__auto___30163]));

var G__30164 = (i__24370__auto___30163 + (1));
i__24370__auto___30163 = G__30164;
continue;
} else {
}
break;
}

var G__30161 = args30159.length;
switch (G__30161) {
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(args30159.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 args30166 = [];
var len__24369__auto___30169 = arguments.length;
var i__24370__auto___30170 = (0);
while(true){
if((i__24370__auto___30170 < len__24369__auto___30169)){
args30166.push((arguments[i__24370__auto___30170]));

var G__30171 = (i__24370__auto___30170 + (1));
i__24370__auto___30170 = G__30171;
continue;
} else {
}
break;
}

var G__30168 = args30166.length;
switch (G__30168) {
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(args30166.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 args30173 = [];
var len__24369__auto___30176 = arguments.length;
var i__24370__auto___30177 = (0);
while(true){
if((i__24370__auto___30177 < len__24369__auto___30176)){
args30173.push((arguments[i__24370__auto___30177]));

var G__30178 = (i__24370__auto___30177 + (1));
i__24370__auto___30177 = G__30178;
continue;
} else {
}
break;
}

var G__30175 = args30173.length;
switch (G__30175) {
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(args30173.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__30180 = (result + (weathergen.math.continuous_noise_field.call(null,(x / z),(y / z),seed) * ((z / zoom) / 2.0)));
var G__30181 = (z / (2));
result = G__30180;
z = G__30181;
continue;
}
break;
}
});

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

weathergen.math.magnitude = (function weathergen$math$magnitude(p__30182){
var vec__30186 = p__30182;
var x = cljs.core.nth.call(null,vec__30186,(0),null);
var y = cljs.core.nth.call(null,vec__30186,(1),null);
return Math.sqrt(((x * x) + (y * y)));
});
weathergen.math.heading = (function weathergen$math$heading(p__30189){
var vec__30193 = p__30189;
var x = cljs.core.nth.call(null,vec__30193,(0),null);
var y = cljs.core.nth.call(null,vec__30193,(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 args30196 = [];
var len__24369__auto___30199 = arguments.length;
var i__24370__auto___30200 = (0);
while(true){
if((i__24370__auto___30200 < len__24369__auto___30199)){
args30196.push((arguments[i__24370__auto___30200]));

var G__30201 = (i__24370__auto___30200 + (1));
i__24370__auto___30200 = G__30201;
continue;
} else {
}
break;
}

var G__30198 = args30196.length;
switch (G__30198) {
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(args30196.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__30203_SHARP_){
return (c * p1__30203_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__30204){
var vec__30208 = p__30204;
var x = cljs.core.nth.call(null,vec__30208,(0),null);
var y = cljs.core.nth.call(null,vec__30208,(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__30211_SHARP_){
return (p1__30211_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);
});
