aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/search.js8
-rw-r--r--space.js14
-rw-r--r--tests/space_comb.js17
3 files changed, 28 insertions, 11 deletions
diff --git a/examples/search.js b/examples/search.js
index 9e9d87a..73f29d0 100644
--- a/examples/search.js
+++ b/examples/search.js
@@ -32,9 +32,11 @@ function Search(searchBlock){ // searchBlock should be a Space object.
});
let block = this.block(loc, inds);
let searchspace = new Space();
- 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
+ block.forEach( (tile) => {
+ searchspace.comb( this.tiles[tile], comb.add );
+ });
+ coords = searchspace.search(searchBlock); // According to space docs, [] on failure and a character location on success
+ return vec.add(coords, searchspace.loc);
}
this.block = function(loc,inds,exclude){
diff --git a/space.js b/space.js
index 2b535fd..4a227cf 100644
--- a/space.js
+++ b/space.js
@@ -82,11 +82,21 @@ function Space(){
return row;
});
}
- this.comb = function(other, func, offset){
+
+ this.comb = function(other, func){ // other must have a valid .loc. If this.loc null, treated as offset
+ if (! other.loc) throw "The secondary space must have a valid .loc"
+ if (this.loc.length == 0) {
+ offset = [0, 0];
+ this.loc = other.loc;
+ } else {
+ offset = vec.sub(this.loc, other.loc);
+ if (this.data.length > 0)
+ this.loc = vec.elem(this.loc, other.loc, (a,b) => Math.min(a,b));
+ } // offset is position of other relative to this, so these are mainly definitional.
// Convert negative offsets of either sort into zero offsets with significant previous whitespace (translation)
for (let i = 0; i < -offset[0]; i++) this.data.unshift([]);
if (offset[0] < 0) offset[0] = 0;
- for (let row = 0; row < offset[0]+other.data.length; row++){ // offset[1] < 0 fix
+ for (let row = 0; row < this.data.length; row++){ // offset[1] < 0 fix
for (let i = 0; i < -offset[1]; i++) this.data[row].unshift('');
}
if (offset[1] < 0) offset[1] = 0;
diff --git a/tests/space_comb.js b/tests/space_comb.js
index 5200fd8..819ee5c 100644
--- a/tests/space_comb.js
+++ b/tests/space_comb.js
@@ -21,10 +21,15 @@ function add(char1,char2){
else return char1;
}
-otherspace.comb(newspace, add, [0,0]);
-
-empty.comb(otherspace, add, [0,0]);
-empty.comb(otherspace, add, [8,0]);
-
-//console.log(otherspace.print());
+otherspace.loc = [0,0];
+newspace.loc = [0,0];
+otherspace.comb(newspace, add);
+
+empty.comb(otherspace, add);
+otherspace.loc = [8,0];
+empty.comb(otherspace, add);
+newspace.loc = [0, -32];
+empty.comb(newspace, add);
+console.log(otherspace.print());
+console.log('--------');
console.log(empty.print()); // Should be equal to otherspace slightly displaced