Coordinate Algorithm - Rotate around the center

前端 未结 1 521
迷失自我
迷失自我 2021-01-16 08:53

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

相关标签:
1条回答
  • 2021-01-16 09:20

    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);
    }
    
    0 讨论(0)
提交回复
热议问题