Skip to content

Commit

Permalink
working on grid
Browse files Browse the repository at this point in the history
  • Loading branch information
yi committed Jan 1, 2013
1 parent 1838f37 commit e1b6216
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 8 deletions.
27 changes: 22 additions & 5 deletions lib/grid.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions lib/tests/grid_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
, "main" : "./index.js"
, "dependencies" : {
"heap" : "0.2.1"
, "node-simple-logger" : ">= 0.1.0"
, "dev-logger" : ">= 0.1.0"
}
, "devDependencies" : {
"uglify-js" : ">= 1.2.5"
Expand Down
19 changes: 17 additions & 2 deletions src/grid.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
logger = require "logger"
Buffer = require "Buffer"


class Grid


@bytesFrom2DArray : (width, height, array2d) ->
buf = new Buffer(Math.ceil(width * height / 8))
buf.fill(0) # fill all bits as false
for row, y in array2d
for col, x in row
if Boolean(col) # set bit only when true
index = y * width + x
byteIndex = Math.ceil(index / 8)
offset = index % 8
byte = buf[byteIndex]
byte = byte ^ 1 << offset
buf[byteIndex] = byte

return buf


constructor: (@width, @height, @bytes) ->
unless width > 0 and height > 0 and Buffer.isBuffer(bytes)
throw new Error "bad arguments, width:#{width}, height:#{height}, bytes:#{bytes}"
Expand Down
37 changes: 37 additions & 0 deletions src/tests/grid_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
grid = require "../grid"


describe "Static method in Grid", ->

it "should work" , ->

array2d = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[1, 1, 0],
[1, 0, 0]
]

width = 3

height = 5

buf = grid.bytesFrom2DArray(width, height, array2d)

console.log(buf.toString('hex'))

for i in [0...buf.length]
console.log "[grid_test] buf[i]:#{buf[i].toString(2)}"



#dump = ''

#for y in [0...height] by 1
#for x in [0...width] by 1





1 change: 1 addition & 0 deletions tests

0 comments on commit e1b6216

Please sign in to comment.