// Compiled by ClojureScript 1.10.520 {}
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.clamp = (function weathergen$math$clamp(min,max,val){
if((val < min)){
return min;
} else {
if((max < val)){
return max;
} else {
return val;

}
}
});
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__4736__auto__ = [];
var len__4730__auto___27765 = arguments.length;
var i__4731__auto___27766 = (0);
while(true){
if((i__4731__auto___27766 < len__4730__auto___27765)){
args__4736__auto__.push((arguments[i__4731__auto___27766]));

var G__27767 = (i__4731__auto___27766 + (1));
i__4731__auto___27766 = G__27767;
continue;
} else {
}
break;
}

var argseq__4737__auto__ = ((((0) < args__4736__auto__.length))?(new cljs.core.IndexedSeq(args__4736__auto__.slice((0)),(0),null)):null);
return weathergen.math.vector_add.cljs$core$IFn$_invoke$arity$variadic(argseq__4737__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);

/** @this {Function} */
weathergen.math.vector_add.cljs$lang$applyTo = (function (seq27764){
var self__4718__auto__ = this;
return self__4718__auto__.cljs$core$IFn$_invoke$arity$variadic(cljs.core.seq.call(null,seq27764));
});

weathergen.math.vector_subtract = (function weathergen$math$vector_subtract(var_args){
var args__4736__auto__ = [];
var len__4730__auto___27769 = arguments.length;
var i__4731__auto___27770 = (0);
while(true){
if((i__4731__auto___27770 < len__4730__auto___27769)){
args__4736__auto__.push((arguments[i__4731__auto___27770]));

var G__27771 = (i__4731__auto___27770 + (1));
i__4731__auto___27770 = G__27771;
continue;
} else {
}
break;
}

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

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

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

/** @this {Function} */
weathergen.math.vector_subtract.cljs$lang$applyTo = (function (seq27768){
var self__4718__auto__ = this;
return self__4718__auto__.cljs$core$IFn$_invoke$arity$variadic(cljs.core.seq.call(null,seq27768));
});

weathergen.math.vector_scale = (function weathergen$math$vector_scale(c,v){
return cljs.core.mapv.call(null,(function (p1__27772_SHARP_){
return (c * p1__27772_SHARP_);
}),v);
});
weathergen.math.vector_clamp = (function weathergen$math$vector_clamp(p__27773,p__27774,p__27775){
var vec__27776 = p__27773;
var x = cljs.core.nth.call(null,vec__27776,(0),null);
var y = cljs.core.nth.call(null,vec__27776,(1),null);
var vec__27779 = p__27774;
var minx = cljs.core.nth.call(null,vec__27779,(0),null);
var miny = cljs.core.nth.call(null,vec__27779,(1),null);
var vec__27782 = p__27775;
var maxx = cljs.core.nth.call(null,vec__27782,(0),null);
var maxy = cljs.core.nth.call(null,vec__27782,(1),null);
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [weathergen.math.clamp.call(null,minx,maxx,x),weathergen.math.clamp.call(null,miny,maxy,y)], null);
});
weathergen.math.scramble = (function weathergen$math$scramble(var_args){
var G__27786 = arguments.length;
switch (G__27786) {
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(["Invalid arity: ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(arguments.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 G__27789 = arguments.length;
switch (G__27789) {
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(["Invalid arity: ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(arguments.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 G__27792 = arguments.length;
switch (G__27792) {
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(["Invalid arity: ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(arguments.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 G__27795 = arguments.length;
switch (G__27795) {
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(["Invalid arity: ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(arguments.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__27797 = (result + (weathergen.math.continuous_noise_field.call(null,(x / z),(y / z),seed) * ((z / zoom) / 2.0)));
var G__27798 = (z / (2));
result = G__27797;
z = G__27798;
continue;
}
break;
}
});

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

weathergen.math.magnitude = (function weathergen$math$magnitude(p__27799){
var vec__27800 = p__27799;
var x = cljs.core.nth.call(null,vec__27800,(0),null);
var y = cljs.core.nth.call(null,vec__27800,(1),null);
return Math.sqrt(((x * x) + (y * y)));
});
weathergen.math.heading = (function weathergen$math$heading(p__27803){
var vec__27804 = p__27803;
var x = cljs.core.nth.call(null,vec__27804,(0),null);
var y = cljs.core.nth.call(null,vec__27804,(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 G__27808 = arguments.length;
switch (G__27808) {
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(["Invalid arity: ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(arguments.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;

/**
 * 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__27810_SHARP_){
return (c * p1__27810_SHARP_);
}),v);
});
weathergen.math.deg__GT_rad = (function weathergen$math$deg__GT_rad(deg){
return ((deg * Math.PI) / 180.0);
});
weathergen.math.rad__GT_deg = (function weathergen$math$rad__GT_deg(rad){
return ((rad * 180.0) / Math.PI);
});
weathergen.math.rotate = (function weathergen$math$rotate(deg,p__27811){
var vec__27812 = p__27811;
var x = cljs.core.nth.call(null,vec__27812,(0),null);
var y = cljs.core.nth.call(null,vec__27812,(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__27815_SHARP_){
return (p1__27815_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);
});
