From 310165f74d17631cfaaadc71e55000303ba0262d Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Fri, 27 Dec 2019 14:48:48 -0500 Subject: search.js: integrated vec --- examples/search.js | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) (limited to 'examples') diff --git a/examples/search.js b/examples/search.js index 7a64042..9e9d87a 100644 --- a/examples/search.js +++ b/examples/search.js @@ -17,20 +17,13 @@ function tileToChar(coord){ return [coord[0]*cofactor[0], coord[1]*cofactor[1]]; } -function coordmult(coord,factor){ - return [coord[0]*factor, coord[1]*factor] -} - -function coordSum(coord, coord){ // This needs to be generalized and standardized (vector arithmetic) in a utils or a library - return [coord[0]+coord[0], coord[1]+coord[1]]; -} - function Search(searchBlock){ // searchBlock should be a Space object. this.tiles = {}; // Object which stores Spaces. this.sort = [[],[]]; // Vertically/horizontally sorted list of tiles for fast addition, deletion, and searching this.add = function(loc, space){ // loc should be [tileY,tileX] and space Space. this.tiles[loc] = space; + this.tiles[loc].loc = vec.tileToChar(loc); let inds = Array(2); // Records horizontal and vertical indices for insertion. Then actually inserts the item. [0,1].forEach( ind => { // ind chooses y-or-x inds[ind] = -bs(this.sort[ind], loc, getComp(ind))-1; @@ -39,14 +32,9 @@ function Search(searchBlock){ // searchBlock should be a Space object. }); let block = this.block(loc, inds); let searchspace = new Space(); - let root = tileToChar(getdims(Array.from(block))[0]); - //let root = [0,0]; //PLACEHOLDER: get upperleft-most tile in searchspace using getdims. - // searchspace.comb(this.tiles['2,2'], comb.add, [16,32]); - block.forEach( (tile) => {searchspace.comb(this.tiles[tile],comb.add,coordSum(tileToChar(tile),coordmult(root,-1)))}); - coords = searchspace.search(searchBlock); - if (! coords) return coords; // If coords are empty (i.e. no match), return; - return [ coords[0] , coords[1] ]; - //return searchspace.search(searchBlock); // According to space docs, [] on failure and a character location on success + let root = vec.tileToChar(getdims(Array.from(block))[0]); + block.forEach( (tile) => {searchspace.comb(this.tiles[tile],comb.add,vec.add(vec.tileToChar(tile), vec.mult(root,-1)))}); + return searchspace.search(searchBlock); // According to space docs, [] on failure and a character location on success } this.block = function(loc,inds,exclude){ -- cgit