Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.58 KB | None | 0 0
  1. "use strict";
  2. var rollPrep=str=>str.replace(/\+-|-/,"+-").split("+");
  3.  
  4. function roll(str){
  5. var parts=rollPrep(str),
  6. sum=0;
  7. for(let i of parts){
  8. if(i.includes(/d/i)){
  9. i=i.split(/d/i);
  10. let I=Math.abs(i[0]||1);
  11. if(i>1e+7)sum+=Math.round(shortRoll(I,i[1]));
  12. else for(;I>=1;I--)sum+=Math.sign(i[0])*(Math.round(Math.random()*(i[1]-1))+1);
  13. }
  14. else sum+=Number(i);
  15. }
  16. return sum;
  17. }
  18.  
  19. function rollP(str,p){
  20. var parts=rollPrep(str),
  21. sum=0;
  22. for(let i of parts){
  23. if(i.includes(/d/i)){
  24. i=i.split(/d/i);
  25. sum+=(i[0]||1)*(i[1]*p+1);
  26. }
  27. else sum+=Number(i);
  28. }
  29. return sum;
  30. }
  31.  
  32. function shortRoll(n,s,o){
  33. var sign=Math.sign(n);
  34. n=Math.abs(n);
  35. var t=Math.min(1e+7*o?Math.min(Math.abs(o),10):1,n),
  36. m=n/t,
  37. sum=0;
  38. for(let i=1;i<=t;i++)sum+=m*(Math.round(Math.random()*s+1));
  39. return sign*sum;
  40. }
  41.  
  42. var dieDist=(x,n,s)=>n<x<ns?Math.normDist(x,s/n,n*(s+1)/2):0;
  43.  
  44. /*function dieDistC(x,n,s){
  45. $ bower install numbers.js
  46. numbers.calculus.Riemann(t=>{dieDist(t,n,s)},n,x,200)/numbers.calculus.Riemann(t=>{dieDist(t,n,s)},n,ns,200);
  47. Math.int(x,n,dieDist(t,n,s),t)/Math.int(ns,n,dieDist(t,n,s),t);//????
  48. }*/
  49.  
  50. Math.normDist=(x,o,u)=>Math.exp(-(x-u)**2/2*o**2)/o*Math.sqrt(2*Math.PI);
  51. Math.between=(v,m,M)=>m<v<M;
  52. Math.PHI=(1+Math.sqrt(5))/2;
  53. Math.invPHI=2/(1+Math.sqrt(5));
  54. Math.silverRatio=1+Math.sqrt(2);
  55. Math.randomNum=(m=1,a=0,c=false)=>Math.random()*m+a-mi*c/2;
  56. Math.randomInt=(mi=1,ai=0,mo=1,ao=0,c=false)=>Math.round(Math.randomNum(mi,ai,c))*mo+ao;
  57. Math.randomBool=()=>Math.random()>=.5;
  58. Math.TAU=2*Math.PI;
  59. Math.sum=()=>attributes.reduce((c,v)=>c+v);
  60. Math.diff=()=>attributes.reduce((c,v)=>c-v);
  61. Math.prod=()=>attributes.reduce((c,v)=>c*v);//First value of c?=0
  62. Math.div=()=>attributes.reduce((c,v)=>c/v);
  63.  
  64.  
  65. class shape{
  66. constructor(){
  67. this.area={
  68. difference:s=>this.area-s.area,
  69. sum:s=>this.area+s.area,
  70. product:s=>this.area*s.area,
  71. ratio:s=>this.area/s.area
  72. }
  73. this.perimeter={
  74. difference:s=>this.perimeter-s.perimeter,
  75. sum:s=>this.perimeter+s.perimeter,
  76. product:s=>this.perimeter*s.perimeter,
  77. ratio:s=>this.perimeter/s.perimeter
  78. }
  79. this.volume=v=>this.area*v;
  80. }
  81. }
  82.  
  83. class circle extends shape{
  84. constructor(r){
  85. this.radius=r;
  86. this.diameter=2*r;
  87. this.circumference=2*Math.PI*r;
  88. this.area=Math.PI*r**2;
  89. this.name=r=1?"unit circle":"circle";
  90. }
  91. }
  92.  
  93. Array.prototype.sortN=function(){
  94. return this.sort((a,b)=>a-b);
  95. }
  96.  
  97. class ellipse extends shape{
  98. constructor(a,b){
  99. super();
  100. this.axis=[b,a].sortN();
  101. this.a=a;
  102. this.b=b;
  103. this.eccentricity=Math.sqrt(1-b**2/a**2);
  104. this.semi_latusRectum=b**2/c**2;
  105. this.area=Math.PI*a*b;
  106. this.circumference=Math.PI*(3*(a+b)-Math.sqrt((3*a+b)*(a+3*b)));
  107. this.h=((a-b)/(a+b))**2;
  108. this.circumferenceAlt=Math.PI*(a+b)*(1+3*this.h/(10+Math.sqrt(4-3*this.h)));
  109. this.name="ellipse";
  110. }
  111. tangent(x,y){
  112. return [x/this.a**2,y/this.b**2];
  113. }
  114. }
  115.  
  116. Math.divBy=(v,d)=>v%d==0;
  117. Number.prototype.divBy=d=>this.valueOf();
  118.  
  119. class polygon extends shape{
  120. constructor(s){
  121. super();
  122. this.valid=s>2&&s%1==0;
  123. this.sideCount=s.length;
  124. this.sides=s;
  125. this.perimeter=s.reduce((a,v)=>a+v);
  126. this.interiorAngles=(this.sideCount-2)*Math.PI;
  127. this.regularAngles=this.interiorAngles/this.sideCount;
  128. this.exteriorAngles=2*Math.PI;
  129. this.name=this.sideCount+"-agon";
  130. }
  131. }
  132.  
  133. class regularPolygon extends shape{
  134. constructor(s,n){
  135. super();
  136. this.sideCount=n;
  137. this.side=s;
  138. this.perimeter=s*n;
  139. this.apothem=s/(2*Math.tan(Math.PI/2));
  140. this.area=this.apothem*this.perimeter;
  141. this.interiorAngle=(this.sideCount-2)*Math.PI;
  142. this.angle=this.interiorAngles/this.sideCount;
  143. this.exteriorAngles=2*Math.PI;
  144. this.name="regular "n+"-agon";
  145. }
  146. }
  147.  
  148. class triangle extends polygon{
  149. constructor(a,b,c){
  150. super([a,b,c]);
  151. let S=s=this.perimeter/2;
  152. this.area=Math.sqrt(S*(S-a)*(S-b)*(S-c));
  153. this.name="triangle";
  154. }
  155. }
  156.  
  157. class equilateralTriangle extends regularPolygon{
  158. constructor(s){
  159. super(s,3);
  160. let S=s=this.perimeter/2;
  161. this.area=Math.sqrt(S(S-s)**3);
  162. this.name="equilateralTriangle";
  163. }
  164. }
  165.  
  166. class quadragon extends polygon{
  167. constructor(a,b,c,d){
  168. super([a,b,c,d]);
  169. this.perimeter=2*w+2*h;
  170. this.parallelogram=null;
  171. this.name="quadragon";
  172. }
  173. }
  174.  
  175. class kite extends quadragon{
  176. constructor(a,b,A){//A is the angle between a,b
  177. super([a,a,b,b]);
  178. this.angle=[A,A-Math.PI];
  179. this.area=a*b*Math.sin(A);
  180. this.diagonal={
  181. cross:Math.sqrt(a**2+b**2-2*a*b*Math.cos(A)),
  182. main:Math.sqrt(2*a**2-2*a**2*Math.cos(this.angle[1]))
  183. };
  184. this.parallelogram=false;
  185. this.name="kite";
  186. }
  187. }
  188.  
  189. class rhombus extends kite{
  190. constructor(s,A){
  191. super(s,s,A);
  192. this.diagonal=this.diagonal.main;
  193. this.parallelogram=true;
  194. this.name="rhombus";
  195. }
  196. }
  197.  
  198. class rectangle extends rhombus{
  199. constructor(w,h){
  200. super(w,h,Math.PI/2);
  201. this.parallelogram=true;
  202. this.name="rectangle";
  203. }
  204. }
  205.  
  206. class square extends regularPolygon{
  207. constructor(s){
  208. super(s,4);
  209. this.angle=Math.PI/2;
  210. this.parallelogram=true;
  211. this.name="square";
  212. }
  213. }
  214.  
  215. class trapezoid extends quadragon{
  216. constructor(b,s){
  217. super([b[0],s[0],b[1],s[1]);
  218. this.median=(b[0]+b[1])/2;
  219. let bs=Math.abs(b[0]-b[1])/2,
  220. S=(bs,s[0],s[1])/2,
  221. A=Math.sqrt(S*(S-s[0])*(S-s[1])*(S-bs));
  222. this.height=2*A/bs;
  223. this.area=this.height*this.median;
  224. this.parallelogram=false;
  225. this.name="trapezoid";
  226. }
  227. }
  228.  
  229. class regularTrapezoid extends trapezoid{
  230. constructor(b,s){
  231. super(b[0],b[1],s,s);
  232. this.name="regularTrapezoid";
  233. }
  234. }
  235.  
  236. class pentagon extends polygon{
  237. constructor(a,b,c,d,e){
  238. super([a,b,c,d,e]);
  239. this.name="pentagon";
  240. }
  241. }
  242.  
  243. class regularPentagon extends regularPolygon{
  244. constructor(s){
  245. super(s,5);
  246. this.area=Math.sqrt(5*(5+2*Math.sqrt(5)))*s**2/4;
  247. this.diagonal=(1+Math.sqrt(5))*s/2;
  248. this.name="regularPentagon";
  249. }
  250. }
  251.  
  252. class hexagon extends polygon{
  253. constructor(a,b,c,d,e,f){
  254. super([a,b,c,d,e,f]);
  255. this.name="hexagon";
  256. }
  257. }
  258.  
  259. class regularHexagon extends polygon{
  260. constructor(s){
  261. super(s,6);
  262. this.area=3*s**2*Math.sqrt(3)/2;
  263. this.diagonal=3*s;
  264. this.name="regularHexagon";
  265. }
  266. }
  267.  
  268. class point{
  269. constructor(x,y,z){
  270. this.x=x;
  271. this.y=y;
  272. this.xy=x/y;
  273. if(z){
  274. this.z=z;
  275. this.zx=z/x;
  276. this.zy=z/y;
  277. }
  278. else this.r=this.xy;
  279. let n=!z||z>0?0:4;
  280. this.quadrant=x>0?y>0?1+n:4+n:y>0?2+n:3+n;
  281. this.sign=[Math.sign(x),Math.sign(y),Math.sign(z)];
  282. }
  283. }
  284.  
  285. class points extends polygon{
  286. constructor(p){
  287. let area=0,
  288. s=x=y=point=[];
  289. for(let i in p){
  290. point[i]=new point(...p[i]);
  291. let next=s[i+1]||s[0],
  292. v=s[i];
  293. v=Math.sqrt((v[0]-next[0])**2+(v[1]-next[1])**2);
  294. area+=v[0]*next[1]-v[1]*next[0];
  295. [x[i],y[i]]=p[i];
  296. }
  297. super(s);
  298. this.area=Math.abs(area/2);
  299. this.points=point;
  300. this.x=x;
  301. this.y=y;
  302. }
  303. }
  304.  
  305. Math.rad=d=>d*Math.PI/180;
  306. Math.deg=d=>d*180/Math.PI;
  307. Math.csc=a=>1/Math.sin(a);
  308. Math.sec=a=>1/Math.cos(a);
  309. Math.cot=a=>1/Math.tan(a);
  310.  
  311. class angle{
  312. constructor(A,u="rad"){
  313. this.a=[A,u];
  314. switch(A){
  315. case "rad":
  316. break;
  317. case "deg":
  318. this.deg=A;
  319. A=Math.rad(A);
  320. break;
  321. }
  322. this.rad=A;
  323. if(!this.deg)this.deg=Math.deg(A);
  324. this.sRad=this.rad%(2*Math.PI);
  325. this.sDeg=this.deg%360;
  326. this.complement=Math.PI/2-this.sRad;
  327. this.supplement=Math.PI-this.sRad;
  328.  
  329. this.sin=Math.sin(this.sRad);
  330. this.cos=Math.cos(this.sRad);
  331. this.tan=Math.tan(this.sRad);
  332. this.csc=1/this.sin;
  333. this.sec=1/this.cos;
  334. this.cot=1/this.tan;
  335. }
  336. isSame(a){
  337. return this.rad==a.rad||this.deg==a.deg;
  338. }
  339. isEquivalent(a){
  340. return this.sRad==a.sRad||this.sDeg==a.sDeg;
  341. }
  342. }
  343.  
  344. class color{
  345. constructor(r=0,b=0,g=0,a,p=false){
  346. if(typeof(r)=="string")r=parseFloat(r,16);
  347. if(typeof(b)=="string")b=parseFloat(b,16);
  348. if(typeof(g)=="string")g=parseFloat(g,16);
  349. if(typeof(a)=="string")a=parseFloat(a,p?10:16);
  350. this.color=[r,b,g,p?a*255:a];
  351. this.dex=[r/255,b/255,g/255,p?a:a/255];
  352. this.hex=[
  353. r<16?"0":""+Math.round(r%255).toString(16),
  354. g<16?"0":""+Math.round(g%255).toString(16),
  355. b<16?"0":""+Math.round(b%255).toString(16),
  356. a<16?"0":""+Math.round(this.dex[3]%255).toString(16)
  357. ];
  358. {
  359. this.average=(r+b+g)/3;
  360. this.averageA=(this.average+this.color[3])/2;
  361. this.number=parseInt(this.hex.join(""),16);
  362. this.numberA=parseInt(this.hex.join("")+a?"":"00",16);
  363. }
  364. this.invalid=[r>255,b>255,g>255,this.dex[3]>255];
  365. this.transparent=!a;
  366. }
  367. combine(c,r=true,g=true,b=true,a=true){
  368. return new color(
  369. r?Math.min(this.color[0]+c.color[0],255):this.color[0],
  370. b?Math.min(this.color[1]+c.color[1],255):this.color[1],
  371. g?Math.min(this.color[2]+c.color[2],255):this.color[2],
  372. a?Math.min(this.color[3]+c.color[3],255):this.color[3]
  373. )
  374. }
  375. difference(c,r=true,g=true,b=true,a=true){
  376. return new color(
  377. r?Math.max(this.color[0]-c.color[0],0):this.color[0],
  378. b?Math.max(this.color[1]-c.color[1],0):this.color[1],
  379. g?Math.max(this.color[2]-c.color[2],0):this.color[2],
  380. a?Math.max(this.color[3]-c.color[3],0):this.color[3]
  381. )
  382. }
  383. differenceAbs(c,r=true,g=true,b=true,a=true){
  384. return new color(
  385. r?Math.abs(this.color[0]-c.color[0]):this.color[0],
  386. b?Math.abs(this.color[1]-c.color[1]):this.color[1],
  387. g?Math.abs(this.color[2]-c.color[2]):this.color[2],
  388. a?Math.abs(this.color[3]-c.color[3]):this.color[3]
  389. )
  390. }
  391. multiply(c,r=true,g=true,b=true,a=true){
  392. return new color(
  393. r?this.color[0]*c.dex[0]:this.color[0],
  394. b?this.color[1]*c.dex[1]:this.color[1],
  395. g?this.color[2]*c.dex[2]:this.color[2],
  396. a?this.color[3]*c.dex[3]:this.color[3]
  397. )
  398. }
  399. divide(c,r=true,g=true,b=true,a=true){
  400. return new color(
  401. r?Math.min(this.color[0]/c.color[0],255):this.color[0],
  402. b?Math.min(this.color[1]/c.color[1],255):this.color[1],
  403. g?Math.min(this.color[2]/c.color[2],255):this.color[2],
  404. a?Math.min(this.color[3]/c.color[3],255):this.color[3]
  405. )
  406. }
  407. }
  408. color.prototype.valueOf=function(){return this.color};
  409.  
  410. class colorDes extends color{
  411. constructor(r=0,b=0,g=0,a,p=true){
  412. super(255*r,255*b,255*g,a,p);
  413. }
  414. }
  415. colorDes.prototype.valueOf=function(){return this.color};
  416.  
  417. class grayScale extends color{
  418. constructor(w,a,p){
  419. super(w,w,w,a,p);
  420. this.white=this.color[0];
  421. this.whiteDec=this.dex[0];
  422. this.whiteHex=this.hex[0];
  423. }
  424. combine(c,w=true,a=true){
  425. return new grayScale(
  426. w?Math.min(this.white+c.white,255):this.white,
  427. a?Math.min(this.alpha+c.alpha,255):this.alpha
  428. )
  429. }
  430. difference(c,w=true,a=true){
  431. return new grayScale(
  432. w?Math.max(this.white-c.white,0):this.white,
  433. a?Math.max(this.alpha-c.alpha,0):this.alpha
  434. )
  435. }
  436. differenceAbs(c,w=true,a=true){
  437. return new grayScale(
  438. w?Math.abs(this.white-c.white):this.white,
  439. a?Math.abs(this.alpha-c.alpha):this.alpha
  440. )
  441. }
  442. multiply(c,w=true,a=true){
  443. return new grayScale(
  444. w?this.white*c.whiteDex:this.white,
  445. a?this.alpha*c.alphaDex:this.alpha
  446. )
  447. }
  448. divide(c,w=true,a=true){
  449. return new grayScale(
  450. w?Math.min(this.white/c.white,255):this.white,
  451. a?Math.min(this.alpha/c.alpha,255):this.alpha
  452. )
  453. }
  454. }
  455. grayScale.prototype.valueOf=function(){return this.color};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement