diff options
author | Holden Rohrer <holden.rohrer@gmail.com> | 2019-12-23 23:11:42 -0500 |
---|---|---|
committer | Holden Rohrer <holden.rohrer@gmail.com> | 2019-12-23 23:11:42 -0500 |
commit | a59b6c0cfdeb469cecb00648cf6b3c03627322b8 (patch) | |
tree | c8bed9dceb150d630ef3eddf2ede5d79e06f571e | |
parent | 6c912dc1511c7eb17b2c2ff43c1023e8fb9aff78 (diff) |
added vec.js
-rw-r--r-- | examples/jarvis.js | 17 | ||||
-rw-r--r-- | tests/vec_elem.js | 10 | ||||
-rw-r--r-- | utils/vec.js | 26 |
3 files changed, 46 insertions, 7 deletions
diff --git a/examples/jarvis.js b/examples/jarvis.js index a94121c..9d4bb91 100644 --- a/examples/jarvis.js +++ b/examples/jarvis.js @@ -19,26 +19,29 @@ main.on('open', ()=>{ // Tries to identify itself with a cursor movement function detect(pos, send){ if (equals(pos[0],coords)){ main.off('cursor', detect); - this.emit('identity', send); + identity(send); } } }) -main.on('identity', (sender) => { // Does this need to be an event (or would a callback like identity(sender) work just as well?) +function identity(sender){ + console.log('identity activated'); var search = new Space(); search.adhoc('jarvis'); // The search space is the word jarvis, so whenever that's caught, a relevant function can be called. - var read = new Search(); + var read = new Search(search); var expire = {}; - main.on('tileUpdate', (send, source, tiles) => { + main.on('tileUpdate', (send, source, tiles) => { console.log(tiles); }); + /*main.on('tileUpdate', (send, source, tiles) => { + console.log('detected update', tiles); if (send == sender) return; let locs = tilekeys(tiles); for (let i=0; i<locs.length; i++){ let loc = locs[i]; - if read.has(loc) clearTimeout(expire[loc]); + if (read.has(loc)) clearTimeout(expire[loc]); let locspace = new Space(); locspace.fromfetch(tiles, [loc[0][0], loc[0][1], loc[1][0], loc[1][1]], conform=false); let results = read.add(locspace); @@ -46,8 +49,8 @@ main.on('identity', (sender) => { // Does this need to be an event (or would a c expire[loc] = setTimeout(()=>{read.del(loc)}, 30000); } - }); -}); + });*/ +} function respond(coord){ console.log('called at', coord); diff --git a/tests/vec_elem.js b/tests/vec_elem.js new file mode 100644 index 0000000..f364681 --- /dev/null +++ b/tests/vec_elem.js @@ -0,0 +1,10 @@ +// Tests vec.js, in its main functionality---.elem() + +const vec = require('../utils/vec'); + +coord1 = [-22, 23]; +coord2 = [25, 25]; + +console.log('add', vec.add(coord1, coord2)); +console.log('sub', vec.sub(coord1, coord2)); +console.log('min', vec.elem(coord1, coord2, (a,b) => Math.min(a,b))); diff --git a/utils/vec.js b/utils/vec.js new file mode 100644 index 0000000..9808c8b --- /dev/null +++ b/utils/vec.js @@ -0,0 +1,26 @@ +// A utility library for vector (coordinate) operations. + +exports.mult = function(vec, scal){ + for (let i = 0; i < vec.length; i++) + vec[i] = scal*vec[i]; + return vec; +} + +function elem(orig, vec, op){ + let comb = []; + for (let i = 0; i < orig.length; i++){ + comb.push(op(vec[i],orig[i])); + } + return comb; +} +exports.elem = elem; + +exports.add = function(orig, vec){ + return elem(orig, vec, (a,b) => a+b); +} +exports.sub = function(orig, vec){ + return elem(orig, vec, (a,b) => a-b); +} +exports.dot = function(orig,vec){ + return elem(orig, vec, (a,b) => a*b); +} |