Представляю первый вариант решения, пока что работает только с подразделение на 4 части, что означает что он НЕ работает, однако выладываю как пример (работает с размерами step/size: 2/5,4/9 и тд.)
var arr = [], sectors = [], step = 2, size = 5; // заполняем исходный массив значениями 0-24 var str = ''; for(i = 0; i < size*size; i++) { // fill array arr[i] = i; str+=addZero(''+arr[i], 2); if( (i+1) % size === 0 && i !== 0) { console.log(str); // draw array str = ''; } else { str+=', '; } } // считаем колличество сектаров for(i = 0; i < Math.pow(Math.floor(size/step),2); i++) { sectors.push([]); } // собственно сам алгоритм нарезки var to_arr = 0, x, y, mod_x, mod_y, line_up = Math.floor(size / step); for( i = 0 ; i < size*size; i++) { x = Math.floor( i % size ); y = Math.floor( i / size ); mod_x = x%(step+1); mod_y = y%(step+1); to_arr = Math.floor(y/(step+1)) * line_up + Math.floor(x/(step+1)); if(mod_x == step && mod_y == step ) { sectors[to_arr].push(i); sectors[to_arr+1].push(i); sectors[to_arr+line_up].push(i); sectors[to_arr+line_up+1].push(i); } else if(mod_x == step) { sectors[to_arr].push(i); sectors[to_arr+1].push(i); } else if(mod_y == step) { sectors[to_arr].push(i); sectors[to_arr+line_up].push(i); } else { sectors[to_arr].push(i); } } console.log(sectors); function addZero(str,length) { // add 0 to fill str length while(str.length < length) { str = '0'+str; } return str; } Жду Ваших решений ) З.Ы. - скорость еще не тестировал |
Все решил, жалко что никто не поучаствовал.
Вот решение (работает со всеми правильно введенными срезами(step) для размера 9 их три step: 2,4,8) var arr = [], sectors = [], step = 2, size = 9; var str = ''; for(i = 0; i < size*size; i++) { // fill array arr[i] = i+1; str+=addZero(''+arr[i], 2); if( (i+1) % size === 0 && i !== 0) { console.log(str); // draw array str = ''; } else { str+=', '; } } for(i = 0; i < Math.pow(Math.floor(size/step),2); i++) { sectors.push([]); } var to_arr = 0, x, y, line_up = Math.floor(size / step), is_repeat, j_step; for( i = 1, j = 0, k = 0; i < 82; i++, j++) { x = Math.floor( i % size ); y = Math.floor( (i-1) / size ); j_step =j%step; is_repeat = k%step==0 && k != 0 && y != 0 && y!=(size-1); if(j_step != 0 || j == 0) { sectors[to_arr].push(i); if(is_repeat) { sectors[to_arr+line_up].push(i); } } if(j_step == 0 && j != 0) { sectors[to_arr].push(i); if(is_repeat) { sectors[to_arr+line_up].push(i); } if(x!=0) { sectors[to_arr+1].push(i); if(is_repeat) { sectors[to_arr+line_up+1].push(i); } } to_arr++; j = 0; } if(x==0) { to_arr = (Math.floor(k/step)-(is_repeat?1:0)) * line_up+(k%step==0 && k !=0?line_up:0); k++; j=-1; } } console.log(sectors); function addZero(str,length) { // add 0 to fill str length while(str.length < length) { str = '0'+str; } return str; } |
Та же функция, но заполняем по индексам:
(тестировал с size: 257, 513, 1025, 2049, чем выше size тем медленее работает даынный вариант по сравнению с предыдущим на js, на с++ впринципе это вариан всегда быстрее в 5 раз с маленькими размерами и в 25 раз с большими ) var arr = [], sectors = [], step = 2, size = 9; var str = ''; for(i = 0; i < size*size; i++) { // fill array arr[i] = i+1; str+=addZero(''+arr[i], 2); if( (i+1) % size === 0 && i !== 0) { console.log(str); // draw array str = ''; } else { str+=', '; } } for(i = 0; i < Math.pow(Math.floor(size/step),2); i++) { sectors.push(new Array(Math.pow(step+1,2))); } var to_arr = 0, x, y, line_up = Math.floor(size / step), is_repeat, k_step; for( i = 1, j = 0, k = 0; i <= arr.length; i++, j++) { x = Math.floor( i % size ); y = Math.floor( (i-1) / size ); k_step = k == step && k != 0; is_repeat = k_step && y != 0 && y!=(size-1); if(j != step || j == 0) { sectors[to_arr][j+k*(step+1)] = i; if(is_repeat) { sectors[to_arr+line_up][j] = i; } } if(j == step && j != 0) { sectors[to_arr][j+k*(step+1)] = i; if(is_repeat) { sectors[to_arr+line_up][j] = i; } if(x!=0) { sectors[to_arr+1][j+k*(step+1)-step] = i; if(is_repeat) { sectors[to_arr+line_up+1][j-step] = i; } } to_arr++; j = 0; } if(x==0) { to_arr = (Math.floor(y/step)-(is_repeat?1:0)) * line_up+(k_step?line_up:0); k = k_step?1:k + 1; j=-1; } } console.log(sectors); function addZero(str,length) { // add 0 to fill str length while(str.length < length) { str = '0'+str; } return str; } |
Добавляю функцию для вывода получившихся секций:
console.log('--------------------------------'); var str = ""; for(i = 0; i < sectors.length; i+=Math.floor(size/step) ) { for( k = 0; k <= step; k++) { for(s = 0; s < Math.floor(size/step); s++) { for(j=0;j<=step;j++) str+=addZero(''+sectors[i+s][k*(step+1)+j],TO_SIZE)+" "; str+=" "; } str+="\n"; } if(i % step == 0) { str += new Array((size+1)*3+1).join(' '); str += "\n"; } } console.log(str); И все вместе что бы посмотреть: var arr = [], sectors = [], step = 4, size = 17; var TO_SIZE = size.toString().length+1; var str = ''; for(i = 0; i < size*size; i++) { // fill array arr[i] = i+1; str+=addZero(''+arr[i], TO_SIZE); if( (i+1) % size === 0 && i !== 0) { console.log(str); // draw array str = ''; } else { str+=', '; } } for(i = 0; i < Math.pow(Math.floor(size/step),2); i++) { sectors.push(new Array(Math.pow(step+1,2))); } var to_arr = 0, x, y, line_up = Math.floor(size / step), is_repeat, k_step; for( i = 1, j = 0, k = 0; i <= arr.length; i++, j++) { x = Math.floor( i % size ); y = Math.floor( (i-1) / size ); k_step = k == step && k != 0; is_repeat = k_step && y != 0 && y!=(size-1); if(j != step || j == 0) { sectors[to_arr][j+k*(step+1)] = i; if(is_repeat) { sectors[to_arr+line_up][j] = i; } } if(j == step && j != 0) { sectors[to_arr][j+k*(step+1)] = i; if(is_repeat) { sectors[to_arr+line_up][j] = i; } if(x!=0) { sectors[to_arr+1][j+k*(step+1)-step] = i; if(is_repeat) { sectors[to_arr+line_up+1][j-step] = i; } } to_arr++; j = 0; } if(x==0) { to_arr = (Math.floor(y/step)-(is_repeat?1:0)) * line_up+(k_step?line_up:0); k = k_step?1:k + 1; j=-1; } } console.log('--------------------------------'); var str = ""; for(i = 0; i < sectors.length; i+=Math.floor(size/step) ) { for( k = 0; k <= step; k++) { for(s = 0; s < Math.floor(size/step); s++) { for(j=0;j<=step;j++) str+=addZero(''+sectors[i+s][k*(step+1)+j],TO_SIZE)+" "; str+=" "; } str+="\n"; } if(i % step == 0) { str += new Array((size+1)*3+1).join(' '); str += "\n"; } } console.log(str); function addZero(str,length) { // add 0 to fill str length while(str.length < length) { str = '0'+str; } return str; } Всем удачи! |
Часовой пояс GMT +3, время: 01:54. |