aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHolden Rohrer <holden.rohrer@gmail.com>2019-12-27 14:48:48 -0500
committerHolden Rohrer <holden.rohrer@gmail.com>2019-12-27 14:48:48 -0500
commit310165f74d17631cfaaadc71e55000303ba0262d (patch)
tree789e3d9576d9bcfcb8b34fb3dc75e62cbd1796f5 /examples
parent8921cc0d825981ab79f9f0d3b9d19d08d5d1677e (diff)
search.js: integrated vec
Diffstat (limited to 'examples')
-rw-r--r--examples/search.js20
1 files changed, 4 insertions, 16 deletions
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){