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

var G__33258 = (i__24370__auto___33257 + (1));
i__24370__auto___33257 = G__33258;
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 (seq33255){
return weathergen.math.vector_add.cljs$core$IFn$_invoke$arity$variadic(cljs.core.seq.call(null,seq33255));
});

weathergen.math.scramble = (function weathergen$math$scramble(var_args){
var args33259 = [];
var len__24369__auto___33262 = arguments.length;
var i__24370__auto___33263 = (0);
while(true){
if((i__24370__auto___33263 < len__24369__auto___33262)){
args33259.push((arguments[i__24370__auto___33263]));

var G__33264 = (i__24370__auto___33263 + (1));
i__24370__auto___33263 = G__33264;
continue;
} else {
}
break;
}

var G__33261 = args33259.length;
switch (G__33261) {
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(args33259.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 args33266 = [];
var len__24369__auto___33269 = arguments.length;
var i__24370__auto___33270 = (0);
while(true){
if((i__24370__auto___33270 < len__24369__auto___33269)){
args33266.push((arguments[i__24370__auto___33270]));

var G__33271 = (i__24370__auto___33270 + (1));
i__24370__auto___33270 = G__33271;
continue;
} else {
}
break;
}

var G__33268 = args33266.length;
switch (G__33268) {
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(args33266.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 args33273 = [];
var len__24369__auto___33276 = arguments.length;
var i__24370__auto___33277 = (0);
while(true){
if((i__24370__auto___33277 < len__24369__auto___33276)){
args33273.push((arguments[i__24370__auto___33277]));

var G__33278 = (i__24370__auto___33277 + (1));
i__24370__auto___33277 = G__33278;
continue;
} else {
}
break;
}

var G__33275 = args33273.length;
switch (G__33275) {
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(args33273.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 args33280 = [];
var len__24369__auto___33283 = arguments.length;
var i__24370__auto___33284 = (0);
while(true){
if((i__24370__auto___33284 < len__24369__auto___33283)){
args33280.push((arguments[i__24370__auto___33284]));

var G__33285 = (i__24370__auto___33284 + (1));
i__24370__auto___33284 = G__33285;
continue;
} else {
}
break;
}

var G__33282 = args33280.length;
switch (G__33282) {
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(args33280.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__33287 = (result + (weathergen.math.continuous_noise_field.call(null,(x / z),(y / z),seed) * ((z / zoom) / 2.0)));
var G__33288 = (z / (2));
result = G__33287;
z = G__33288;
continue;
}
break;
}
});

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

weathergen.math.magnitude = (function weathergen$math$magnitude(p__33289){
var vec__33293 = p__33289;
var x = cljs.core.nth.call(null,vec__33293,(0),null);
var y = cljs.core.nth.call(null,vec__33293,(1),null);
return Math.sqrt(((x * x) + (y * y)));
});
weathergen.math.heading = (function weathergen$math$heading(p__33296){
var vec__33300 = p__33296;
var x = cljs.core.nth.call(null,vec__33300,(0),null);
var y = cljs.core.nth.call(null,vec__33300,(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 args33303 = [];
var len__24369__auto___33306 = arguments.length;
var i__24370__auto___33307 = (0);
while(true){
if((i__24370__auto___33307 < len__24369__auto___33306)){
args33303.push((arguments[i__24370__auto___33307]));

var G__33308 = (i__24370__auto___33307 + (1));
i__24370__auto___33307 = G__33308;
continue;
} else {
}
break;
}

var G__33305 = args33303.length;
switch (G__33305) {
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(args33303.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__33310_SHARP_){
return (c * p1__33310_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__33311){
var vec__33315 = p__33311;
var x = cljs.core.nth.call(null,vec__33315,(0),null);
var y = cljs.core.nth.call(null,vec__33315,(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__33318_SHARP_){
return (p1__33318_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);
});
