Created
January 30, 2014 14:59
-
-
Save aNNiMON/8710268 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JOptionPane; | |
/** | |
* @author aNNiMON | |
*/ | |
public class SpiralAlgorithm { | |
private static enum Directions { | |
UP { | |
@Override | |
public Directions nextDirection() { | |
return RIGHT; | |
} | |
}, | |
RIGHT { | |
@Override | |
public Directions nextDirection() { | |
return DOWN; | |
} | |
}, | |
DOWN { | |
@Override | |
public Directions nextDirection() { | |
return LEFT; | |
} | |
}, | |
LEFT { | |
@Override | |
public Directions nextDirection() { | |
return UP; | |
} | |
}; | |
public abstract Directions nextDirection(); | |
}; | |
private final int[][] array; | |
public SpiralAlgorithm(int width, int height) { | |
array = new int[height][width]; | |
} | |
/** | |
* Заполнение по спирали. | |
* Мой метод с направлениями. | |
*/ | |
public void setSpiral_1() { | |
// Стартовое значение | |
int currentValue = 1; | |
// Индекс операции всегда с нуля | |
int index = 0; | |
int x = 0; | |
int y = 0; | |
// Сдвиг | |
int shift = 1; | |
final int allValues = array.length * array[0].length; | |
Directions direction = Directions.RIGHT; | |
while( index < allValues ) { | |
array[y][x] = currentValue; | |
switch (direction) { | |
case RIGHT: | |
if(x >= (array[0].length - shift)) | |
direction = direction.nextDirection(); | |
else { | |
x++; | |
break; | |
} | |
case DOWN: | |
if(y >= (array.length - shift)) | |
direction = direction.nextDirection(); | |
else { | |
y++; | |
break; | |
} | |
case LEFT: | |
if(x < shift) | |
direction = direction.nextDirection(); | |
else { | |
x--; | |
break; | |
} | |
case UP: | |
if(y <= shift) { | |
direction = direction.nextDirection(); | |
// Сделали круг, увеличили отступ | |
shift++; | |
// Так как мы не можем непосредственно выполнить RIGHT | |
x++; | |
} else y--; | |
break; | |
} | |
index++; | |
currentValue++; | |
} | |
} | |
/** | |
* Заполнение по спирали. | |
* Мой метод с направлениями. Оптимизация. | |
*/ | |
public void setSpiral_2() { | |
// Стартовое значение | |
int currentValue = 1; | |
// Индекс операции всегда с нуля | |
int index = 0; | |
int x = 0; | |
int y = 0; | |
// Сдвиг | |
int shift = 1; | |
final int allValues = array.length * array[0].length; | |
int direction = 0; | |
while( index < allValues ) { | |
array[y][x] = currentValue; | |
switch (direction) { | |
case 0: // RIGHT | |
if(x >= (array[0].length - shift)) | |
direction++; | |
else { | |
x++; | |
break; | |
} | |
case 1: // DOWN | |
if(y >= (array.length - shift)) | |
direction++; | |
else { | |
y++; | |
break; | |
} | |
case 2: // LEFT | |
if(x < shift) | |
direction++; | |
else { | |
x--; | |
break; | |
} | |
case 3: // UP | |
if(y <= shift) { | |
direction = 0; | |
// Сделали круг, увеличили отступ | |
shift++; | |
// Так как мы не можем непосредственно выполнить RIGHT | |
x++; | |
} else y--; | |
break; | |
} | |
index++; | |
currentValue++; | |
} | |
} | |
@Override | |
public String toString() { | |
StringBuilder out = new StringBuilder(); | |
out.append("Size: "); | |
out.append(array[0].length).append('x').append(array.length); | |
out.append('\n'); | |
for (int i = 0; i < array.length; i++) { | |
for (int j = 0; j < array[i].length; j++) { | |
out.append(array[i][j]).append(' '); | |
} | |
out.append('\n'); | |
} | |
return out.toString(); | |
} | |
public static void main(String[] args) { | |
final int width = Integer.parseInt(JOptionPane.showInputDialog("Enter width of array.")); | |
final int height = Integer.parseInt(JOptionPane.showInputDialog("Enter height of array.")); | |
SpiralAlgorithm spiral = new SpiralAlgorithm(width, height); | |
spiral.setSpiral_2(); | |
JOptionPane.showMessageDialog(null, spiral.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment