aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <holden.rohrer@gmail.com>2020-01-08 17:44:51 -0500
committerHolden Rohrer <holden.rohrer@gmail.com>2020-01-08 17:44:51 -0500
commit590edcd7419963cfe870eb639538cd6a918dd3cf (patch)
tree6c049d1b7792e885345a0980b79437c71f36563e
parentfe47eaa5885c41f9cf0be58028c1ec6bd330badc (diff)
added rasterization for space -> tile transform
-rw-r--r--space.js7
-rw-r--r--utils/raster.js15
2 files changed, 19 insertions, 3 deletions
diff --git a/space.js b/space.js
index d4e4567..d7f68e8 100644
--- a/space.js
+++ b/space.js
@@ -134,10 +134,11 @@ function Space(){
this.subsection = function(range){ // range is a coordinate pair
// Similarly excludes this.loc
newspace = new Space();
- for (let line=range[0][0]; line<=range[1][0]; line++){
+ for (let line=range[0][0]; line<range[1][0]; line++){
newspace.data.push([]);
- for (let chr=range[0][1]; chr<=range[1][1]; chr++){
- newspace.data[line-range[0][0]].push(this.data[line][chr] || '');
+ let thisline = this.data[line] || [];
+ for (let chr=range[0][1]; chr<range[1][1]; chr++){
+ newspace.data[line-range[0][0]].push(thisline[chr] || '');
}
}
return newspace;
diff --git a/utils/raster.js b/utils/raster.js
new file mode 100644
index 0000000..44f4678
--- /dev/null
+++ b/utils/raster.js
@@ -0,0 +1,15 @@
+// Takes a space object and 'rasterizes' it into an object mapping of 8x16 tiles which may not be filled with empty space
+
+const ms = require('./measurespace');
+const vec = require('./vec');
+
+module.exports = function(space){
+ // just use .subsection
+ let tiles = {};
+ dim = ms(space).map(coord => vec.dot(coord,[1/8, 1/16])).map(tile => vec.elem(tile, [], a => Math.floor(a)));
+ for (let y=dim[0][0]; y<=dim[0][1]; y++) for (let x=dim[1][0]; x<=dim[1][1]; x++){
+ let tile = tileToChar([y,x]);
+ tiles[tile] = space.subsection(tile, vec.add(tile, [8,16]));
+ }
+ return tiles
+}