From a59b6c0cfdeb469cecb00648cf6b3c03627322b8 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Mon, 23 Dec 2019 23:11:42 -0500 Subject: added vec.js --- examples/jarvis.js | 17 ++++++++++------- tests/vec_elem.js | 10 ++++++++++ utils/vec.js | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 tests/vec_elem.js create mode 100644 utils/vec.js 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 { // 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); +} -- cgit