-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectortobitmap.js
40 lines (40 loc) · 1.11 KB
/
vectortobitmap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Cells with non-positive or non-integer coordinates may break this module, use with caution.
var toBitmap=function(vector){
if(vector.length===0){
return [[]];
}
var xMax=vector[0][0];
var yMax=vector[0][1];
for(var i=0;i<vector.length;i++){
if(vector[i][0]>xMax){
xMax=vector[i][0];
}
if(vector[i][1]>yMax){
yMax=vector[i][1];
}
}
xMax+=3;
yMax+=3;
var column=[];
for(var i=0;i<yMax;i++){
column.push(0);
}
var bitmap=[];
for(var i=0;i<xMax;i++){
bitmap.push(column.slice());
}
for(var i=0;i<vector.length;i++){
bitmap[vector[i][0]][vector[i][1]]=1;
}
return bitmap.slice();
};
var toBitmapCheck=function(){
console.log("Result of calling toBitmap on the vector array [[2,1],[3,2],[1,3],[2,3],[3,3]]:");
console.log(toBitmap([[2,1],[3,2],[1,3],[2,3],[3,3]]));
console.log("Expected result:");
console.log("[ [ 0, 0, 0, 0, 0, 0 ],\n [ 0, 0, 0, 1, 0, 0 ],\n [ 0, 1, 0, 1, 0, 0 ],\n [ 0, 0, 1, 1, 0, 0 ],\n [ 0, 0, 0, 0, 0, 0 ],\n [ 0, 0, 0, 0, 0, 0 ] ]");
};
module.exports={
toBitmap:toBitmap,
toBitmapCheck:toBitmapCheck
};