Created
September 8, 2019 01:01
-
-
Save zekrom-vale/938e4ce136f399885d1320f8609d614b to your computer and use it in GitHub Desktop.
Roll the dice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var rollPrep=str=>str.replace(/\+-|-/,"+-").split("+"); | |
function roll(str){ | |
var parts=rollPrep(str), | |
sum=0; | |
for(let i of parts){ | |
if(i.includes(/d/i)){ | |
i=i.split(/d/i); | |
let I=Math.abs(i[0]||1); | |
if(i>1e+7)sum+=Math.round(shortRoll(I,i[1])); | |
else for(;I>=1;I--)sum+=Math.sign(i[0])*(Math.round(Math.random()*(i[1]-1))+1); | |
} | |
else sum+=Number(i); | |
} | |
return sum; | |
} | |
function rollP(str,p){ | |
var parts=rollPrep(str), | |
sum=0; | |
for(let i of parts){ | |
if(i.includes(/d/i)){ | |
i=i.split(/d/i); | |
sum+=(i[0]||1)*(i[1]*p+1); | |
} | |
else sum+=Number(i); | |
} | |
return sum; | |
} | |
function shortRoll(n,s,o){ | |
var sign=Math.sign(n); | |
n=Math.abs(n); | |
var t=Math.min(1e+7*o?Math.min(Math.abs(o),10):1,n), | |
m=n/t, | |
sum=0; | |
for(let i=1;i<=t;i++)sum+=m*(Math.round(Math.random()*s+1)); | |
return sign*sum; | |
} | |
var dieDist=(x,n,s)=>n<x<ns?Math.normDist(x,s/n,n*(s+1)/2):0; | |
/*function dieDistC(x,n,s){ | |
$ bower install numbers.js | |
numbers.calculus.Riemann(t=>{dieDist(t,n,s)},n,x,200)/numbers.calculus.Riemann(t=>{dieDist(t,n,s)},n,ns,200); | |
Math.int(x,n,dieDist(t,n,s),t)/Math.int(ns,n,dieDist(t,n,s),t);//???? | |
}*/ | |
Math.normDist=(x,o,u)=>Math.exp(-(x-u)**2/2*o**2)/o*Math.sqrt(2*Math.PI); | |
Math.between=(v,m,M)=>m<v<M; | |
Math.PHI=(1+Math.sqrt(5))/2; | |
Math.invPHI=2/(1+Math.sqrt(5)); | |
Math.silverRatio=1+Math.sqrt(2); | |
Math.randomNum=(m=1,a=0,c=false)=>Math.random()*m+a-mi*c/2; | |
Math.randomInt=(mi=1,ai=0,mo=1,ao=0,c=false)=>Math.round(Math.randomNum(mi,ai,c))*mo+ao; | |
Math.randomBool=()=>Math.random()>=.5; | |
Math.TAU=2*Math.PI; | |
Math.sum=()=>attributes.reduce((c,v)=>c+v); | |
Math.diff=()=>attributes.reduce((c,v)=>c-v); | |
Math.prod=()=>attributes.reduce((c,v)=>c*v);//First value of c?=0 | |
Math.div=()=>attributes.reduce((c,v)=>c/v); | |
class shape{ | |
constructor(){ | |
this.area={ | |
difference:s=>this.area-s.area, | |
sum:s=>this.area+s.area, | |
product:s=>this.area*s.area, | |
ratio:s=>this.area/s.area | |
} | |
this.perimeter={ | |
difference:s=>this.perimeter-s.perimeter, | |
sum:s=>this.perimeter+s.perimeter, | |
product:s=>this.perimeter*s.perimeter, | |
ratio:s=>this.perimeter/s.perimeter | |
} | |
this.volume=v=>this.area*v; | |
} | |
} | |
class circle extends shape{ | |
constructor(r){ | |
this.radius=r; | |
this.diameter=2*r; | |
this.circumference=2*Math.PI*r; | |
this.area=Math.PI*r**2; | |
this.name=r=1?"unit circle":"circle"; | |
} | |
} | |
Array.prototype.sortN=function(){ | |
return this.sort((a,b)=>a-b); | |
} | |
class ellipse extends shape{ | |
constructor(a,b){ | |
super(); | |
this.axis=[b,a].sortN(); | |
this.a=a; | |
this.b=b; | |
this.eccentricity=Math.sqrt(1-b**2/a**2); | |
this.semi_latusRectum=b**2/c**2; | |
this.area=Math.PI*a*b; | |
this.circumference=Math.PI*(3*(a+b)-Math.sqrt((3*a+b)*(a+3*b))); | |
this.h=((a-b)/(a+b))**2; | |
this.circumferenceAlt=Math.PI*(a+b)*(1+3*this.h/(10+Math.sqrt(4-3*this.h))); | |
this.name="ellipse"; | |
} | |
tangent(x,y){ | |
return [x/this.a**2,y/this.b**2]; | |
} | |
} | |
Math.divBy=(v,d)=>v%d==0; | |
Number.prototype.divBy=d=>this.valueOf(); | |
class polygon extends shape{ | |
constructor(s){ | |
super(); | |
this.valid=s>2&&s%1==0; | |
this.sideCount=s.length; | |
this.sides=s; | |
this.perimeter=s.reduce((a,v)=>a+v); | |
this.interiorAngles=(this.sideCount-2)*Math.PI; | |
this.regularAngles=this.interiorAngles/this.sideCount; | |
this.exteriorAngles=2*Math.PI; | |
this.name=this.sideCount+"-agon"; | |
} | |
} | |
class regularPolygon extends shape{ | |
constructor(s,n){ | |
super(); | |
this.sideCount=n; | |
this.side=s; | |
this.perimeter=s*n; | |
this.apothem=s/(2*Math.tan(Math.PI/2)); | |
this.area=this.apothem*this.perimeter; | |
this.interiorAngle=(this.sideCount-2)*Math.PI; | |
this.angle=this.interiorAngles/this.sideCount; | |
this.exteriorAngles=2*Math.PI; | |
this.name="regular "n+"-agon"; | |
} | |
} | |
class triangle extends polygon{ | |
constructor(a,b,c){ | |
super([a,b,c]); | |
let S=s=this.perimeter/2; | |
this.area=Math.sqrt(S*(S-a)*(S-b)*(S-c)); | |
this.name="triangle"; | |
} | |
} | |
class equilateralTriangle extends regularPolygon{ | |
constructor(s){ | |
super(s,3); | |
let S=s=this.perimeter/2; | |
this.area=Math.sqrt(S(S-s)**3); | |
this.name="equilateralTriangle"; | |
} | |
} | |
class quadragon extends polygon{ | |
constructor(a,b,c,d){ | |
super([a,b,c,d]); | |
this.perimeter=2*w+2*h; | |
this.parallelogram=null; | |
this.name="quadragon"; | |
} | |
} | |
class kite extends quadragon{ | |
constructor(a,b,A){//A is the angle between a,b | |
super([a,a,b,b]); | |
this.angle=[A,A-Math.PI]; | |
this.area=a*b*Math.sin(A); | |
this.diagonal={ | |
cross:Math.sqrt(a**2+b**2-2*a*b*Math.cos(A)), | |
main:Math.sqrt(2*a**2-2*a**2*Math.cos(this.angle[1])) | |
}; | |
this.parallelogram=false; | |
this.name="kite"; | |
} | |
} | |
class rhombus extends kite{ | |
constructor(s,A){ | |
super(s,s,A); | |
this.diagonal=this.diagonal.main; | |
this.parallelogram=true; | |
this.name="rhombus"; | |
} | |
} | |
class rectangle extends rhombus{ | |
constructor(w,h){ | |
super(w,h,Math.PI/2); | |
this.parallelogram=true; | |
this.name="rectangle"; | |
} | |
} | |
class square extends regularPolygon{ | |
constructor(s){ | |
super(s,4); | |
this.angle=Math.PI/2; | |
this.parallelogram=true; | |
this.name="square"; | |
} | |
} | |
class trapezoid extends quadragon{ | |
constructor(b,s){ | |
super([b[0],s[0],b[1],s[1]); | |
this.median=(b[0]+b[1])/2; | |
let bs=Math.abs(b[0]-b[1])/2, | |
S=(bs,s[0],s[1])/2, | |
A=Math.sqrt(S*(S-s[0])*(S-s[1])*(S-bs)); | |
this.height=2*A/bs; | |
this.area=this.height*this.median; | |
this.parallelogram=false; | |
this.name="trapezoid"; | |
} | |
} | |
class regularTrapezoid extends trapezoid{ | |
constructor(b,s){ | |
super(b[0],b[1],s,s); | |
this.name="regularTrapezoid"; | |
} | |
} | |
class pentagon extends polygon{ | |
constructor(a,b,c,d,e){ | |
super([a,b,c,d,e]); | |
this.name="pentagon"; | |
} | |
} | |
class regularPentagon extends regularPolygon{ | |
constructor(s){ | |
super(s,5); | |
this.area=Math.sqrt(5*(5+2*Math.sqrt(5)))*s**2/4; | |
this.diagonal=(1+Math.sqrt(5))*s/2; | |
this.name="regularPentagon"; | |
} | |
} | |
class hexagon extends polygon{ | |
constructor(a,b,c,d,e,f){ | |
super([a,b,c,d,e,f]); | |
this.name="hexagon"; | |
} | |
} | |
class regularHexagon extends polygon{ | |
constructor(s){ | |
super(s,6); | |
this.area=3*s**2*Math.sqrt(3)/2; | |
this.diagonal=3*s; | |
this.name="regularHexagon"; | |
} | |
} | |
class point{ | |
constructor(x,y,z){ | |
this.x=x; | |
this.y=y; | |
this.xy=x/y; | |
if(z){ | |
this.z=z; | |
this.zx=z/x; | |
this.zy=z/y; | |
} | |
else this.r=this.xy; | |
let n=!z||z>0?0:4; | |
this.quadrant=x>0?y>0?1+n:4+n:y>0?2+n:3+n; | |
this.sign=[Math.sign(x),Math.sign(y),Math.sign(z)]; | |
} | |
} | |
class points extends polygon{ | |
constructor(p){ | |
let area=0, | |
s=x=y=point=[]; | |
for(let i in p){ | |
point[i]=new point(...p[i]); | |
let next=s[i+1]||s[0], | |
v=s[i]; | |
v=Math.sqrt((v[0]-next[0])**2+(v[1]-next[1])**2); | |
area+=v[0]*next[1]-v[1]*next[0]; | |
[x[i],y[i]]=p[i]; | |
} | |
super(s); | |
this.area=Math.abs(area/2); | |
this.points=point; | |
this.x=x; | |
this.y=y; | |
} | |
} | |
Math.rad=d=>d*Math.PI/180; | |
Math.deg=d=>d*180/Math.PI; | |
Math.csc=a=>1/Math.sin(a); | |
Math.sec=a=>1/Math.cos(a); | |
Math.cot=a=>1/Math.tan(a); | |
class angle{ | |
constructor(A,u="rad"){ | |
this.a=[A,u]; | |
switch(A){ | |
case "rad": | |
break; | |
case "deg": | |
this.deg=A; | |
A=Math.rad(A); | |
break; | |
} | |
this.rad=A; | |
if(!this.deg)this.deg=Math.deg(A); | |
this.sRad=this.rad%(2*Math.PI); | |
this.sDeg=this.deg%360; | |
this.complement=Math.PI/2-this.sRad; | |
this.supplement=Math.PI-this.sRad; | |
this.sin=Math.sin(this.sRad); | |
this.cos=Math.cos(this.sRad); | |
this.tan=Math.tan(this.sRad); | |
this.csc=1/this.sin; | |
this.sec=1/this.cos; | |
this.cot=1/this.tan; | |
} | |
isSame(a){ | |
return this.rad==a.rad||this.deg==a.deg; | |
} | |
isEquivalent(a){ | |
return this.sRad==a.sRad||this.sDeg==a.sDeg; | |
} | |
} | |
class color{ | |
constructor(r=0,b=0,g=0,a,p=false){ | |
if(typeof(r)=="string")r=parseFloat(r,16); | |
if(typeof(b)=="string")b=parseFloat(b,16); | |
if(typeof(g)=="string")g=parseFloat(g,16); | |
if(typeof(a)=="string")a=parseFloat(a,p?10:16); | |
this.color=[r,b,g,p?a*255:a]; | |
this.dex=[r/255,b/255,g/255,p?a:a/255]; | |
this.hex=[ | |
r<16?"0":""+Math.round(r%255).toString(16), | |
g<16?"0":""+Math.round(g%255).toString(16), | |
b<16?"0":""+Math.round(b%255).toString(16), | |
a<16?"0":""+Math.round(this.dex[3]%255).toString(16) | |
]; | |
{ | |
this.average=(r+b+g)/3; | |
this.averageA=(this.average+this.color[3])/2; | |
this.number=parseInt(this.hex.join(""),16); | |
this.numberA=parseInt(this.hex.join("")+a?"":"00",16); | |
} | |
this.invalid=[r>255,b>255,g>255,this.dex[3]>255]; | |
this.transparent=!a; | |
} | |
combine(c,r=true,g=true,b=true,a=true){ | |
return new color( | |
r?Math.min(this.color[0]+c.color[0],255):this.color[0], | |
b?Math.min(this.color[1]+c.color[1],255):this.color[1], | |
g?Math.min(this.color[2]+c.color[2],255):this.color[2], | |
a?Math.min(this.color[3]+c.color[3],255):this.color[3] | |
) | |
} | |
difference(c,r=true,g=true,b=true,a=true){ | |
return new color( | |
r?Math.max(this.color[0]-c.color[0],0):this.color[0], | |
b?Math.max(this.color[1]-c.color[1],0):this.color[1], | |
g?Math.max(this.color[2]-c.color[2],0):this.color[2], | |
a?Math.max(this.color[3]-c.color[3],0):this.color[3] | |
) | |
} | |
differenceAbs(c,r=true,g=true,b=true,a=true){ | |
return new color( | |
r?Math.abs(this.color[0]-c.color[0]):this.color[0], | |
b?Math.abs(this.color[1]-c.color[1]):this.color[1], | |
g?Math.abs(this.color[2]-c.color[2]):this.color[2], | |
a?Math.abs(this.color[3]-c.color[3]):this.color[3] | |
) | |
} | |
multiply(c,r=true,g=true,b=true,a=true){ | |
return new color( | |
r?this.color[0]*c.dex[0]:this.color[0], | |
b?this.color[1]*c.dex[1]:this.color[1], | |
g?this.color[2]*c.dex[2]:this.color[2], | |
a?this.color[3]*c.dex[3]:this.color[3] | |
) | |
} | |
divide(c,r=true,g=true,b=true,a=true){ | |
return new color( | |
r?Math.min(this.color[0]/c.color[0],255):this.color[0], | |
b?Math.min(this.color[1]/c.color[1],255):this.color[1], | |
g?Math.min(this.color[2]/c.color[2],255):this.color[2], | |
a?Math.min(this.color[3]/c.color[3],255):this.color[3] | |
) | |
} | |
} | |
color.prototype.valueOf=function(){return this.color}; | |
class colorDes extends color{ | |
constructor(r=0,b=0,g=0,a,p=true){ | |
super(255*r,255*b,255*g,a,p); | |
} | |
} | |
colorDes.prototype.valueOf=function(){return this.color}; | |
class grayScale extends color{ | |
constructor(w,a,p){ | |
super(w,w,w,a,p); | |
this.white=this.color[0]; | |
this.whiteDec=this.dex[0]; | |
this.whiteHex=this.hex[0]; | |
} | |
combine(c,w=true,a=true){ | |
return new grayScale( | |
w?Math.min(this.white+c.white,255):this.white, | |
a?Math.min(this.alpha+c.alpha,255):this.alpha | |
) | |
} | |
difference(c,w=true,a=true){ | |
return new grayScale( | |
w?Math.max(this.white-c.white,0):this.white, | |
a?Math.max(this.alpha-c.alpha,0):this.alpha | |
) | |
} | |
differenceAbs(c,w=true,a=true){ | |
return new grayScale( | |
w?Math.abs(this.white-c.white):this.white, | |
a?Math.abs(this.alpha-c.alpha):this.alpha | |
) | |
} | |
multiply(c,w=true,a=true){ | |
return new grayScale( | |
w?this.white*c.whiteDex:this.white, | |
a?this.alpha*c.alphaDex:this.alpha | |
) | |
} | |
divide(c,w=true,a=true){ | |
return new grayScale( | |
w?Math.min(this.white/c.white,255):this.white, | |
a?Math.min(this.alpha/c.alpha,255):this.alpha | |
) | |
} | |
} | |
grayScale.prototype.valueOf=function(){return this.color}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment