Skip to content

Instantly share code, notes, and snippets.

@litvil
Last active August 29, 2015 14:20
Show Gist options
  • Save litvil/64ceb88557e0f4e7e615 to your computer and use it in GitHub Desktop.
Save litvil/64ceb88557e0f4e7e615 to your computer and use it in GitHub Desktop.
Create spiral path from center to borders inside rectangle
private function createSpiralSearchPath(max_x:int, max_y:int, start_point:Point = null):Vector.<Point> {
var current_cell:Point = new Point();
if (start_point){
current_cell.x = start_point.x;
current_cell.y = start_point.y;
}else{
current_cell.x = int(max_x * 0.5);
current_cell.y = int(max_y * 0.5);
}
var next_corner_cell:Point;
var top_left:Point = new Point(0,0);
var top_right:Point = new Point(0, max_y);
var bottom_left:Point = new Point(max_x,0);
var bottom_right:Point = new Point(max_x, max_y);
var top_left_riched:Boolean = false;
var top_right_riched:Boolean = false;
var bottom_left_riched:Boolean = false;
var bottom_right_riched:Boolean = false;
var operate_cells:Vector.<Point> = new <Point>[current_cell];
var steps:uint = 2;
var current_step:uint = 1;
var current_length:uint = 1;
var direction: uint = 0; // 0 - влево, 1-вверх, 2 - вправо, 3 - вниз
while( !allCornersInPath() ){
next_corner_cell = new Point();
switch (direction){
case 0:
next_corner_cell.x = current_cell.x;
next_corner_cell.y = current_cell.y - current_length;
for (var i:int = current_cell.y-1; i >= next_corner_cell.y; i--) {
insertInPath( new Point(current_cell.x, i) );
}
break;
case 1:
next_corner_cell.x = current_cell.x - current_length;
next_corner_cell.y = current_cell.y;
for (var i:int = current_cell.x-1; i >= next_corner_cell.x; i--) {
insertInPath( new Point(i, current_cell.y) );
}
break;
case 2:
next_corner_cell.x = current_cell.x;
next_corner_cell.y = current_cell.y + current_length;
for (var i:int = current_cell.y+1; i <= next_corner_cell.y; i++) {
insertInPath( new Point(current_cell.x, i) );
}
break;
case 3:
next_corner_cell.x = current_cell.x + current_length;
next_corner_cell.y = current_cell.y;
for (var i:int = current_cell.x+1; i <= next_corner_cell.x; i++) {
insertInPath( new Point(i, current_cell.y) );
}
break;
}
current_step = ( (current_step + 1) > 2 ) ? 1 : (current_step + 1);
if (current_step == 1){
current_length++;
}
direction = ( (direction + 1) > 3 ) ? 0 : (direction + 1);
current_cell.x = next_corner_cell.x;
current_cell.y = next_corner_cell.y;
}
return operate_cells;
// получили спиральный путь operate_cells
function allCornersInPath():Boolean{
return (top_left_riched && top_right_riched && bottom_left_riched && bottom_right_riched)
}
function insertInPath(point:Point):void{
if (!isOutOfMapArray(point.x, point.y)){
operate_cells.push(point);
if (point.x == top_left.x && point.y == top_left.y) top_left_riched = true;
if (point.x == top_right.x && point.y == top_right.y) top_right_riched = true;
if (point.x == bottom_left.x && point.y == bottom_left.y) bottom_left_riched = true;
if (point.x == bottom_right.x && point.y == bottom_right.y) bottom_right_riched = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment