Skip to content

Commit

Permalink
context.at for absolute indexing (fix #78)
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Aug 14, 2022
1 parent 32bebf2 commit e437768
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,17 @@ The `Context` object has the following properties and methods:
but handling the case when `context.key` is `null`).
* `context.i` is the row number of the cell of this tile (starting at 0).
* `context.j` is the column number of the cell of this tile (starting at 0).
* `context.neighbor(dj, di)` returns a new `Context` for row `i + di` and
column `j + dj`. (Note the reversal of coordinates, so that the order
passed to `neighbor` corresponds to *x* then *y* coordinate.)
* `context.neighbor(dj, di)` returns a new `Context` for row `context.i + di`
and column `context.j + dj` (for relative neighbors).
(Note the reversal of coordinates, so that the order passed to `neighbor`
corresponds to *x* then *y* coordinate.)
If there is no tile at that position, you will still get a `Context` object
but its `key` value will be `null` and `includes()` and `match()`
will always return `false`.
* `context.at(j, i)` returns a new `Context` for row `j` and column `i`
(absolute coordinates).
(Note again the reversal of coordinates to correspond to *x* before *y*.)
Negative numbers count backward from the last row or column.
* In particular, it's useful to call e.g.
`context.neighbor(1, 0).includes('-')` to check for adjacent tiles that
change how this tile should be rendered.
Expand Down
1 change: 1 addition & 0 deletions examples/test/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
all:
svgtiler -f at.coffee at.asc
svgtiler -f escape.txt escape.csv
svgtiler -f css-escape.css css-escape.txt css-escape.asc
svgtiler -f share-provider.js share-user.js
3 changes: 3 additions & 0 deletions examples/test/at.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abc
de
f
14 changes: 14 additions & 0 deletions examples/test/at.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
->
console.assert undefined == @at(0, -4).key == @at(0, -5).key, 'row -1'
console.assert 'a' == @at(0, 0).key == @at(-3, 0).key == @at(0, -3).key == @at(-3, -3).key, 'a'
console.assert 'b' == @at(1, 0).key == @at(-2, 0).key == @at(1, -3).key == @at(-2, -3).key, 'b'
console.assert 'c' == @at(2, 0).key == @at(-1, 0).key == @at(2, -3).key == @at(-1, -3).key, 'c'
console.assert undefined == @at(3, 0).key == @at(-4, 0).key == @at(3, -3).key == @at(-4, -3).key, 'row 0'
console.assert 'd' == @at(0, 1).key == @at(-2, 1).key == @at(0, -2).key == @at(-2, -2).key, 'd'
console.assert 'e' == @at(1, 1).key == @at(-1, 1).key == @at(1, -2).key == @at(-1, -2).key, 'e'
console.assert undefined == @at(2, 1).key == @at(-3, 1).key == @at(2, -2).key == @at(-3, -2).key, 'row 1'
console.assert 'f' == @at(0, 2).key == @at(-1, 2).key == @at(0, -1).key == @at(-1, -1).key, 'f'
console.assert undefined == @at(1, 2).key == @at(-2, 2).key == @at(1, -1).key == @at(-2, -1).key, 'row 2'
console.assert undefined == @at(0, 3).key == @at(1, 3).key == @at(-1, 3).key == @at(-2, 3).key, 'row 3'
console.assert undefined == @at(0, 4).key == @at(1, 4).key == @at(-1, 4).key == @at(-2, 4).key, 'row 4'
''
11 changes: 11 additions & 0 deletions examples/test/at.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/svgtiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,12 @@ class Context
@move i, j if i? and j?
move: (@i, @j) ->
@key = @drawing.data[@i]?[@j]
at: (j, i) ->
if i < 0
i += @drawing.data.length
if j < 0
j += @drawing.data[i]?.length ? 0
new Context @drawing, i, j
neighbor: (dj, di) ->
new Context @drawing, @i + di, @j + dj
includes: (...args) ->
Expand Down

0 comments on commit e437768

Please sign in to comment.