diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/set_utils.js | 81 | ||||
| -rw-r--r-- | src/utils/stars.js | 53 |
2 files changed, 134 insertions, 0 deletions
diff --git a/src/utils/set_utils.js b/src/utils/set_utils.js new file mode 100644 index 0000000..88e8fea --- /dev/null +++ b/src/utils/set_utils.js @@ -0,0 +1,81 @@ +/** + * Operations on sets. + * @module app/utils/set_utils + */ + +/** + * Determine if `set` contains `subset` + * @param {Set} set the superset + * @param {Set} subset the subset + * @return {Boolean} true if set contains subset + */ +export function isSuperset(set, subset) { + for (let elem of subset) { + if (!set.has(elem)) { + return false; + } + } + return true; +} + +/** + * Return the union (A or B) of two sets + * @param {Set} setA a set + * @param {Set} setB a set + * @return {Boolean} the union of the sets + */ +export function union(setA, setB) { + let _union = new Set(setA); + for (let elem of setB) { + _union.add(elem); + } + return _union; +} + +/** + * Return the intersection (A and B) of two sets + * @param {Set} setA a set + * @param {Set} setB a set + * @return {Boolean} the intersection of the sets + */ +export function intersection(setA, setB) { + let _intersection = new Set(); + for (let elem of setB) { + if (setA.has(elem)) { + _intersection.add(elem); + } + } + return _intersection; +} + +/** + * Return the symmetric difference (A xor B) of two sets + * @param {Set} setA a set + * @param {Set} setB a set + * @return {Boolean} the symmetric difference of the sets + */ +export function symmetricDifference(setA, setB) { + let _difference = new Set(setA); + for (let elem of setB) { + if (_difference.has(elem)) { + _difference.delete(elem); + } else { + _difference.add(elem); + } + } + return _difference; +} + +/** + * Return the difference (A not B) of two sets + * @param {Set} setA a set + * @param {Set} setB a set + * @return {Boolean} the difference of the sets + */ +export function difference(setA, setB) { + let _difference = new Set(setA); + for (let elem of setB) { + _difference.delete(elem); + } + return _difference; +} diff --git a/src/utils/stars.js b/src/utils/stars.js new file mode 100644 index 0000000..516a359 --- /dev/null +++ b/src/utils/stars.js @@ -0,0 +1,53 @@ +export default function stars() { + var canvas = document.createElement("canvas"), + ctx = canvas.getContext("2d"); + document.body.appendChild(canvas); + canvas.style.width = "100%"; + canvas.style.height = "100%"; + canvas.style.position = "absolute"; + canvas.style.top = "0px"; + canvas.style.left = "0px"; + canvas.style.zIndex = 1; + canvas.style.opacity = 0.6; + canvas.style.pointerEvents = "none"; + document.body.addEventListener("resize", go); + document.body.parentNode.style.backgroundColor = "black"; + ctx.strokeStyle = "white"; + var s = Math.sin, + c = Math.cos; + go(); + function ri(n) { + return Math.random() * n; + } + function rr(a, b) { + return (b - a) * Math.random() + a; + } + function go() { + var w = (canvas.width = window.innerWidth); + var h = (canvas.height = window.innerHeight); + ctx.clearRect(0, 0, w, h); + var n = Math.sqrt(w * h) | 0; + while (n--) { + var x = ri(w); + var y = ri(h); + var r0 = rr(0, 1); + var r1 = rr(0, 1); + var r2 = rr(0, 1); + var t0 = ri(2 * Math.PI); + var t1 = ri(2 * Math.PI); + var t2 = ri(2 * Math.PI); + var x0 = x + c(t0) * r0; + var y0 = y + s(t0) * r0; + var x1 = x + c(t1) * r1; + var y1 = y + s(t1) * r1; + var x2 = x + c(t2) * r2; + var y2 = y + s(t2) * r2; + ctx.beginPath(); + ctx.moveTo(x, y); + ctx.bezierCurveTo(x0, y0, x1, y1, x2, y2); + var color = rr(0, 255) | 0; + ctx.strokeStyle = "rgb(" + color + "," + color + "," + color + ")"; + ctx.stroke(); + } + } +} |
