Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.70 KB | None | 0 0
  1.  
  2. "use strict";
  3.  
  4. class SNoise
  5. {
  6. constructor(wavelength, cross_wavelength=-1, angle=0)
  7. {
  8. this.wavelength_t = wavelength;
  9. if (cross_wavelength == -1) this.wavelength_s = wavelength;
  10. else this.wavelength_s = cross_wavelength;
  11. this.offset_s = Math.random()*100;
  12. this.offset_t = Math.random()*100;
  13. this.theta = 2*Math.PI*angle/360;
  14. this.sin_theta = Math.sin(this.theta);
  15. this.cos_theta = Math.cos(this.theta);
  16. this.octaves_count = 1;
  17. this.wavenumber_rolloff = 2;
  18. this.amplitude_rolloff = 0.5;
  19. this.rms = 1;
  20. }
  21.  
  22. octaves(count, amplitude_rolloff = 0.5, wavenumber_rolloff = 2)
  23. {
  24. this.octaves_count = count;
  25. this.wavenumber_rolloff = wavenumber_rolloff;
  26. this.amplitude_rolloff = amplitude_rolloff;
  27. var square = 0;
  28. for (var i=0; i<count; i++) square += Math.pow(amplitude_rolloff, 2*i);
  29. this.rms = Math.sqrt(square);
  30. }
  31.  
  32. get(x, y)
  33. {
  34. var unscaled_s = x * this.cos_theta + y * this.sin_theta;
  35. var unscaled_t = x * this.sin_theta - y * this.cos_theta;
  36. var t = unscaled_t / this.wavelength_t;
  37. var s;
  38. if (this.wavelength_s == 0) s = 0;
  39. else s = unscaled_s / this.wavelength_s;
  40. //return Noise.simplex2(s+this.offset_s, t+this.offset_t)
  41. return this.get_basic_noise(s,t);
  42. }
  43.  
  44. get_basic_noise(s,t)
  45. {
  46. var wavenumber = 1;
  47. var amplitude = 1;
  48. var sum = 0;
  49. for (var i=0; i<this.octaves_count; i++)
  50. {
  51. var signal = Noise.simplex2(s*wavenumber+(i+1)*this.offset_s, t*wavenumber+(i+1)*this.offset_t);
  52. sum += signal * amplitude;
  53. amplitude *= this.amplitude_rolloff;
  54. wavenumber *= this.wavenumber_rolloff;
  55. }
  56. return sum / this.rms;
  57. }
  58. }
  59.  
  60. var output = [0,0,0];
  61. function blend_colours(a, b, s)
  62. {
  63. for (var i=0; i<3; i++) output[i] = a[i]*(1-s) + b[i]*s;
  64. return output;
  65. }
  66.  
  67. class Stack
  68. {
  69. constructor(n)
  70. {
  71. this.indices = new Uint32Array(n);
  72. this.pointer = -1;
  73. }
  74.  
  75. clear()
  76. {
  77. this.pointer = -1;
  78. }
  79.  
  80. is_nonempty()
  81. {
  82. return (this.pointer>-1);
  83. }
  84.  
  85. push(index)
  86. {
  87. this.pointer +=1;
  88. this.indices[this.pointer] = index;
  89. }
  90.  
  91. pop()
  92. {
  93. let output = this.indices[this.pointer];
  94. this.pointer -= 1;
  95. return output;
  96. }
  97. }
  98.  
  99. class PriorityQueue_d_ary
  100. {
  101. constructor(n, d, values)
  102. {
  103. this.indices_heap = new Uint32Array(n);
  104. this.values_heap = new Uint32Array(n);
  105. this.d = d;
  106. this.heap_pointer = -1; //points to last occupied slot
  107. this.values = values;
  108. }
  109.  
  110. better(x,y)
  111. {
  112. return (x<y);
  113. }
  114.  
  115. clear()
  116. {
  117. this.max_heap_size = 0;
  118. this.heap_pointer = -1;
  119. }
  120.  
  121. is_nonempty()
  122. {
  123. return (this.heap_pointer>-1);
  124. }
  125.  
  126. push(index)
  127. {
  128. let d = this.d;
  129. let value = this.values[index];
  130. this.heap_pointer += 1;
  131. let pos = this.heap_pointer;
  132. let parent_pos, parent_value, parent_index;
  133. while (pos>0)
  134. {
  135. parent_pos = Math.floor((pos-1) / d);
  136. parent_index = this.indices_heap[parent_pos];
  137. parent_value = this.values_heap[parent_pos];
  138. if (this.better(value, parent_value))
  139. {
  140. this.indices_heap[pos] = parent_index;
  141. this.values_heap[pos] = parent_value;
  142. pos = parent_pos;
  143. }
  144. else break;
  145. }
  146. this.indices_heap[pos] = index;
  147. this.values_heap[pos] = value;
  148. }
  149.  
  150. pop()
  151. {
  152. let d = this.d;
  153. let output = this.indices_heap[0];
  154. let pos = 0;
  155. while(true) // propagate hole down to leaf
  156. {
  157. let children_offset = pos*d;
  158. let child_count = Math.min(d, this.heap_pointer - children_offset);
  159. if (child_count <= 0) break;
  160. let best_child_pos = 1;
  161. let best_child_value = this.values_heap[children_offset + 1];
  162. for (let i=2; i<=child_count; i++)
  163. {
  164. let child_value = this.values_heap[children_offset + i];
  165. if (this.better(child_value, best_child_value))
  166. {
  167. best_child_pos = i;
  168. best_child_value = child_value;
  169. }
  170. }
  171. // now we know the best child, swap it with hole
  172. let new_pos = children_offset + best_child_pos;
  173. this.indices_heap[pos] = this.indices_heap[new_pos];
  174. this.values_heap[pos] = this.values_heap[new_pos];
  175. pos = new_pos;
  176. }
  177. // now hole is at bottom, maybe we are done
  178. if (pos == this.heap_pointer)
  179. {
  180. this.heap_pointer -= 1;
  181. return;
  182. }
  183. // otherwise move last entry into hole, and propagate up
  184. let index = this.indices_heap[this.heap_pointer];
  185. let value = this.values_heap[this.heap_pointer];
  186. this.heap_pointer -= 1;
  187. let parent_pos, parent_value, parent_index;
  188. while (pos>0)
  189. {
  190. parent_pos = Math.floor((pos-1) / d);
  191. parent_index = this.indices_heap[parent_pos];
  192. parent_value = this.values_heap[parent_pos];
  193. if (this.better(value, parent_value))
  194. {
  195. this.indices_heap[pos] = parent_index;
  196. this.values_heap[pos] = parent_value;
  197. pos = parent_pos;
  198. }
  199. else break;
  200. }
  201. this.indices_heap[pos] = index;
  202. this.values_heap[pos] = value;
  203. }
  204.  
  205.  
  206. }
  207.  
  208. class PriorityQueue
  209. {
  210. constructor(n, values)
  211. {
  212. this.indices_heap = new Uint32Array(n+1);
  213. this.values = values;
  214. this.values_heap = new Float32Array(n+1);
  215. this.heap_pointer = 0;
  216. this.max_heap_size = 0;
  217. }
  218.  
  219. better(x, y) // the best thing goes to the top of the heap
  220. {
  221. return (x<y);
  222. }
  223.  
  224. clear()
  225. {
  226. //console.log(this.max_heap_size);
  227. this.max_heap_size = 0;
  228. this.heap_pointer = 0;
  229. }
  230.  
  231. is_nonempty()
  232. {
  233. return (this.heap_pointer>0);
  234. }
  235.  
  236. push(index)
  237. {
  238. this.heap_pointer += 1;
  239. if (this.heap_pointer > this.max_heap_size) this.max_heap_size = this.heap_pointer;
  240. let value = this.values[index];
  241. let pos = this.heap_pointer;
  242. let parent;
  243. let parent_value;
  244. while (pos>1)
  245. {
  246. parent = this.indices_heap[pos>>1];
  247. parent_value = this.values_heap[pos>>1];
  248. if (this.better(value, parent_value))
  249. {
  250. this.indices_heap[pos] = parent;
  251. this.values_heap[pos] = parent_value;
  252. pos = pos>>1;
  253. }
  254. else break;
  255. }
  256. this.indices_heap[pos] = index;
  257. this.values_heap[pos] = value;
  258. }
  259.  
  260. pop()
  261. {
  262. let output = this.indices_heap[1];
  263. let pos = 1;
  264. let left_child, right_child, left_value, right_value;
  265. while (true) // propagate hole down to leaf
  266. {
  267. let left = pos<<1;
  268. if (this.heap_pointer < left) //pos has no children
  269. {
  270. break;
  271. }
  272. else if (this.heap_pointer == left) //only left child
  273. {
  274. left_child = this.indices_heap[left];
  275. left_value = this.values_heap[left];
  276. this.indices_heap[pos] = left_child; //child is smaller
  277. this.values_heap[pos] = left_value;
  278. pos = left;
  279. continue;
  280. }
  281. else //two children
  282. {
  283. left_child = this.indices_heap[left];
  284. left_value = this.values_heap[left];
  285. right_child = this.indices_heap[left+1];
  286. right_value = this.values_heap[left+1];
  287. if (this.better(left_value, right_value))
  288. {
  289. this.indices_heap[pos] = left_child; //swap with left child
  290. this.values_heap[pos] = left_value;
  291. pos = left;
  292. continue;
  293. }
  294. else
  295. {
  296. this.indices_heap[pos] = right_child; //swap with right child
  297. this.values_heap[pos] = right_value;
  298. pos = left+1;
  299. continue;
  300. }
  301. }
  302. } // hole is now at a leaf, pos
  303.  
  304. // now move last entry into hole, and propagate up
  305. let index = this.indices_heap[this.heap_pointer];
  306. let value = this.values_heap[this.heap_pointer];
  307. this.heap_pointer -= 1;
  308. let parent;
  309. let parent_value;
  310. while (pos>1)
  311. {
  312. parent = this.indices_heap[pos>>1];
  313. parent_value = this.values_heap[pos>>1];
  314. if (this.better(value, parent_value))
  315. {
  316. this.indices_heap[pos] = parent;
  317. this.values_heap[pos] = parent_value;
  318. pos = pos>>1;
  319. }
  320. else break;
  321. }
  322. this.indices_heap[pos] = index;
  323. this.values_heap[pos] = value;
  324.  
  325. return output;
  326. }
  327. }
  328.  
  329. class Old_PriorityQueue // TODO - better to store values in heap, next to index?
  330. {
  331. constructor(n, values)
  332. {
  333. this.indices_heap = new Uint32Array(n+1);
  334. this.values = values;
  335. this.heap_pointer = 0;
  336. this.max_heap_size = 0;
  337. }
  338.  
  339. better(x, y) // the best thing goes to the top of the heap
  340. {
  341. return (x<y);
  342. }
  343.  
  344. clear()
  345. {
  346. console.log(this.max_heap_size);
  347. this.max_heap_size = 0;
  348. this.heap_pointer = 0;
  349. }
  350.  
  351. is_nonempty()
  352. {
  353. return (this.heap_pointer>0);
  354. }
  355.  
  356. push(index)
  357. {
  358. this.heap_pointer += 1;
  359. if (this.heap_pointer > this.max_heap_size) this.max_heap_size = this.heap_pointer;
  360. let value = this.values[index];
  361. let pos = this.heap_pointer;
  362. let parent;
  363. while (pos>1)
  364. {
  365. parent = this.indices_heap[pos>>1];
  366. if (this.better(value, this.values[parent]))
  367. {
  368. this.indices_heap[pos] = parent;
  369. pos = pos>>1;
  370. }
  371. else break;
  372. }
  373. this.indices_heap[pos] = index;
  374. }
  375.  
  376. pop()
  377. {
  378. let output = this.indices_heap[1];
  379. let pos = 1;
  380. let left_child, right_child;
  381. while (true) // propagate hole down to leaf
  382. {
  383. if (this.heap_pointer < pos<<1) //pos has no children
  384. {
  385. break;
  386. }
  387. else if (this.heap_pointer == pos<<1) //only left child
  388. {
  389. left_child = this.indices_heap[pos<<1];
  390. this.indices_heap[pos] = left_child; //child is smaller
  391. pos = pos<<1;
  392. continue;
  393. }
  394. else //two children
  395. {
  396. left_child = this.indices_heap[pos<<1];
  397. right_child = this.indices_heap[(pos<<1)+1];
  398. if (this.better(this.values[left_child], this.values[right_child]))
  399. {
  400. this.indices_heap[pos] = left_child; //swap with left child
  401. pos = pos<<1;
  402. continue;
  403. }
  404. else
  405. {
  406. this.indices_heap[pos] = right_child; //swap with right child
  407. pos = (pos<<1)+1;
  408. continue;
  409. }
  410. }
  411. } // hole is now at a leaf, pos
  412.  
  413. // now move last entry into hole, and propagate up
  414. let index = this.indices_heap[this.heap_pointer];
  415. let value = this.values[index];
  416. this.heap_pointer -= 1;
  417. let parent;
  418. while (pos>1)
  419. {
  420. parent = this.indices_heap[pos>>1];
  421. if (this.better(value, this.values[parent]))
  422. {
  423. this.indices_heap[pos] = parent;
  424. pos = pos>>1;
  425. }
  426. else break;
  427. }
  428. this.indices_heap[pos] = index;
  429.  
  430. return output;
  431. }
  432. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement