Skip to content

Instantly share code, notes, and snippets.

@triplog
Created May 10, 2014 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save triplog/4b18606603402e18efe9 to your computer and use it in GitHub Desktop.
Save triplog/4b18606603402e18efe9 to your computer and use it in GitHub Desktop.
// Fill the empty space with the minimum number of rectangles.
// (Rectangles should not overlap each other or walls.)
// The grid size is 1 meter, but the smallest wall/floor tile is 4 meters.
// Check the blue guide button at the top for more info.
// Make sure to sign up on the home page to save your code.
var grid = this.getNavGrid().grid;
var tileSize = 4;
var width = tileSize;
for(var y = 0; y + tileSize < grid.length; y += tileSize) {
for(var x = 0; x + tileSize < grid[0].length; x += width) {
var occupied = grid[y][x].length > 0;
width = tileSize;
if(!occupied) {
while(grid[y][x+width].length<=0)
width+=tileSize;
this.addRect(x + width / 2, y + tileSize/ 2, width,tileSize);
this.wait(); // Hover over the timeline to help debug!
}
}
}
var flag = true;
while(flag){
flag = false;
for(var i=0;i<this.spawnedRectangles.length;i++){
var rect1 = this.spawnedRectangles[i];
for(var j=i+1;j<this.spawnedRectangles.length;j++){
var rect2 = this.spawnedRectangles[j];
if(rect1.pos.x==rect2.pos.x && (((rect2.pos.y-rect2.height/2)-(rect1.pos.y+rect1.height/2)<4&&(rect2.pos.y-rect2.height/2)-(rect1.pos.y+rect1.height/2)>-4)||((rect1.pos.y-rect1.height/2)-(rect2.pos.y+rect2.height/2)<4&&(rect1.pos.y-rect1.height/2)-(rect2.pos.y+rect2.height/2)>-4)) && rect1.width==rect2.width){
width = rect1.width;
height = rect1.height+rect2.height;
x = rect1.pos.x;
y = rect1.pos.y * rect1.height/height + rect2.pos.y * rect2.height/height;
this.removeRectAt(rect1.pos.x,rect1.pos.y);
this.removeRectAt(rect2.pos.x,rect2.pos.y);
this.addRect(x,y,width,height);
flag = true;
this.wait(); // Hover over the timeline to help debug!
break;
}
}
}
}
num = 5; //1回じゃ全更新できない
while(num>0){
for(i=0;i<this.spawnedRectangles.length;i++){
x = this.spawnedRectangles[i].pos.x;
y = this.spawnedRectangles[i].pos.y;
width = this.spawnedRectangles[i].width;
height = this.spawnedRectangles[i].height;
this.removeRectAt(x,y);
this.addRect(x,y,width,height);
this.wait(); // Hover over the timeline to help debug!
}
num--;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment