// 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___34783 = arguments.length;
var i__24560__auto___34784 = (0);
while(true){
if((i__24560__auto___34784 < len__24559__auto___34783)){
args__24566__auto__.push((arguments[i__24560__auto___34784]));

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

weathergen.math.scramble = (function weathergen$math$scramble(var_args){
var args34786 = [];
var len__24559__auto___34789 = arguments.length;
var i__24560__auto___34790 = (0);
while(true){
if((i__24560__auto___34790 < len__24559__auto___34789)){
args34786.push((arguments[i__24560__auto___34790]));

var G__34791 = (i__24560__auto___34790 + (1));
i__24560__auto___34790 = G__34791;
continue;
} else {
}
break;
}

var G__34788 = args34786.length;
switch (G__34788) {
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(args34786.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 args34793 = [];
var len__24559__auto___34796 = arguments.length;
var i__24560__auto___34797 = (0);
while(true){
if((i__24560__auto___34797 < len__24559__auto___34796)){
args34793.push((arguments[i__24560__auto___34797]));

var G__34798 = (i__24560__auto___34797 + (1));
i__24560__auto___34797 = G__34798;
continue;
} else {
}
break;
}

var G__34795 = args34793.length;
switch (G__34795) {
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(args34793.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 args34800 = [];
var len__24559__auto___34803 = arguments.length;
var i__24560__auto___34804 = (0);
while(true){
if((i__24560__auto___34804 < len__24559__auto___34803)){
args34800.push((arguments[i__24560__auto___34804]));

var G__34805 = (i__24560__auto___34804 + (1));
i__24560__auto___34804 = G__34805;
continue;
} else {
}
break;
}

var G__34802 = args34800.length;
switch (G__34802) {
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(args34800.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 args34807 = [];
var len__24559__auto___34810 = arguments.length;
var i__24560__auto___34811 = (0);
while(true){
if((i__24560__auto___34811 < len__24559__auto___34810)){
args34807.push((arguments[i__24560__auto___34811]));

var G__34812 = (i__24560__auto___34811 + (1));
i__24560__auto___34811 = G__34812;
continue;
} else {
}
break;
}

var G__34809 = args34807.length;
switch (G__34809) {
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(args34807.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__34814 = (result + (weathergen.math.continuous_noise_field.call(null,(x / z),(y / z),seed) * ((z / zoom) / 2.0)));
var G__34815 = (z / (2));
result = G__34814;
z = G__34815;
continue;
}
break;
}
});

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

weathergen.math.magnitude = (function weathergen$math$magnitude(p__34816){
var vec__34820 = p__34816;
var x = cljs.core.nth.call(null,vec__34820,(0),null);
var y = cljs.core.nth.call(null,vec__34820,(1),null);
return Math.sqrt(((x * x) + (y * y)));
});
weathergen.math.heading = (function weathergen$math$heading(p__34823){
var vec__34827 = p__34823;
var x = cljs.core.nth.call(null,vec__34827,(0),null);
var y = cljs.core.nth.call(null,vec__34827,(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 args34830 = [];
var len__24559__auto___34833 = arguments.length;
var i__24560__auto___34834 = (0);
while(true){
if((i__24560__auto___34834 < len__24559__auto___34833)){
args34830.push((arguments[i__24560__auto___34834]));

var G__34835 = (i__24560__auto___34834 + (1));
i__24560__auto___34834 = G__34835;
continue;
} else {
}
break;
}

var G__34832 = args34830.length;
switch (G__34832) {
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(args34830.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__34837_SHARP_){
return (c * p1__34837_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__34838){
var vec__34842 = p__34838;
var x = cljs.core.nth.call(null,vec__34842,(0),null);
var y = cljs.core.nth.call(null,vec__34842,(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__34845_SHARP_){
return (p1__34845_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);
});
