I'm trying to spiral around in a rectangle shape (clockwise) but can't seem to get it to work properly with any sized rectangle.
Here is what I have so far...
public class Test {
private static final int RIGHT = 0;
private static final int DOWN = 1;
private static final int LEFT = 2;
private static final int UP = 3;
public static void main (String[] args) {
int x = 100;
int y = 100;
int xTol = 3;
int yTol = 3;
boolean searching = true;
int minX = x - xTol;
int maxX = x + xTol;
int curX = x;
int minY = y - yTol;
int maxY = y + yTol;
int curY = y;
int direction = 0; // 0 = right, 1 = down, 2 = left, 3 = up
int checks = 1;
// Initial Compare.
System.out.println("ini Compare:\tcurX: " + curX + " curY: " + curY);
int rightFinish = 0;
int downFinish = 0;
int leftFinish = 0;
int upFinish = 0;
int compCount = 0;
while (searching) {
switch (direction) {
case RIGHT:
for (int i = 0; i != checks; ++i) {
++curX;
System.out.println("RIGHT Compare:\tcurX: " + curX + " curY: " + curY);
}
direction++;
rightFinish = curX;
break;
case DOWN:
for (int i = 0; i != checks; ++i) {
++curY;
System.out.println("DOWN Compare:\tcurX: " + curX + " curY: " + curY);
}
direction++;
downFinish = curY;
break;
case LEFT:
for (int i = 0; i != checks; ++i) {
--curX;
System.out.println("LEFT Compare:\tcurX: " + curX + " curY: " + curY);
}
direction++;
leftFinish = curX;
break;
case UP:
for (int i = 0; i != checks; ++i) {
--curY;
System.out.println("UP Compare:\tcurX: " + curX + " curY: " + curY);
}
direction = 0;
upFinish = curY;
break;
}
if (++compCount >= 2) {
compCount = 0;
++checks;
}
if (rightFinish == maxX && downFinish == maxY && leftFinish == minX && upFinish == minY) {
System.out.println(direction);
break;
}
}
}
}
The above code almost accomplishes what I want but not quite.
Ultimately i want to create a "rectangle" out of dots by specifying a maxX, maxY, minX and minY in clockwise form.
IE the output would be something like:
ini Compare: curX: 100 curY: 100
RIGHT Compare: curX: 101 curY: 100
DOWN Compare: curX: 101 curY: 101
LEFT Compare: curX: 100 curY: 101
LEFT Compare: curX: 99 curY: 101
UP Compare: curX: 99 curY: 100
UP Compare: curX: 99 curY: 99
RIGHT Compare: curX: 100 curY: 99
RIGHT Compare: curX: 101 curY: 99
RIGHT Compare: curX: 102 curY: 99
DOWN Compare: curX: 102 curY: 100
DOWN Compare: curX: 102 curY: 101
DOWN Compare: curX: 102 curY: 102
LEFT Compare: curX: 101 curY: 102
LEFT Compare: curX: 100 curY: 102
... and so on until complete. The above code almost makes a square but would not work for rectangle.