By looking at this image I think you will understand my problem pretty well:
(image removed - url no longer valid, returns advertising now)
So basic
Here's the closed-form solution that doesn't rely on a loop... I'm not handy with Java, so it's in C#, but it uses basic operations.
static void SpiralCalc(int i) {
i -= 2;
// Origin coordinates
int x = 100, y = 100;
if (i >= 0) {
int v = Convert.ToInt32(Math.Truncate(Math.Sqrt(i + .25) - .5));
int spiralBaseIndex = v * (v + 1);
int flipFlop = ((v & 1) << 1) - 1;
int offset = flipFlop * ((v + 1) >> 1);
x += offset; y += offset;
int cornerIndex = spiralBaseIndex + (v + 1);
if (i < cornerIndex) {
x -= flipFlop * (i - spiralBaseIndex + 1);
} else {
x -= flipFlop * (v + 1);
y -= flipFlop * (i - cornerIndex + 1);
}
}
// x and y are now populated with coordinates
Console.WriteLine(i + 2 + "\t" + x + "\t" + y);
}