简体   繁体   English

在 java 中创建无尽的网格螺旋

[英]Create endless grid spiral in java

I want to create endless spiral in java like this:我想像这样在 java 中创建无尽的螺旋:

无尽的螺旋

I simply want to pass a number from 1-∞ and get it's spiral map.我只是想传递一个 1-∞ 的数字并得到它的螺旋形 map。

Example:例子:

getPoints(0); Would result in answer (0;0)将导致答案 (0;0)

getPoints(5); Would result in answer (-1;0)将导致答案 (-1;0)

Think algorithmically.用算法思考。 Identify the pattern.识别模式。

The pattern is that you walk the circumference, one layer at a time.模式是你走圆周,一次一层。

  • In round 0, you are at the center point (0,0) .在第 0 轮中,您位于中心点(0,0)
  • In round 1, you walk the 8 cells around it.在第 1 轮中,您绕着它走 8 个牢房。
  • In round 2, you walk the 16 cells around that.在第 2 轮中,您绕着 16 个牢房走。
  • In round 3, you walk the 24 cells around that.在第 3 轮中,你绕着它走 24 个牢房。

Now see the pattern:现在看模式:

  • In each round, the walk on each side is 2 more steps, totally 8 more steps per round.每轮每边多走2步,每轮总共多走8步。
  • Each round starts 1 cell to the right of the upper-left corner.每轮从左上角右侧的 1 个单元格开始。
  • Walking on each side starts 1 cell from the corner.每边行走从拐角处开始 1 个单元格。

So, now that you see the pattern, you can do it like this:所以,现在你看到了模式,你可以这样做:

  • Calculate which round you are in.计算你在哪一轮。
  • For that round, calculate which side you are on.对于这一轮,计算你在哪一边。
  • Then calculate how many steps you've taken on that side.然后计算你在那一边走了多少步。
  • Finally, calculate the coordinate from that.最后,从中计算坐标。
import java.util.Scanner;

class spiral 
{
    private static String getXZForMap(int np)
    {         
        // (dx, dy) is a vector - direction in which we move right now
        int dx = 0;
        int dy = 1;
        // length of current segment
        int segment_length = 1;

        // current position (x, y) and how much of current segment we passed
        int x = 0;
        int y = 0;
        int segment_passed = 0;
        if (np == 0){
            return ("(" + y + ";" + x + ")");
        }
        for (int n = 0; n < np; ++n) {
            // make a step, add 'direction' vector (dx, dy) to current position (x, y)
            x += dx;
            y += dy;
            ++segment_passed;

            if (segment_passed == segment_length) {
                // done with current segment
                segment_passed = 0;

                // 'rotate' directions
                int buffer = dy;
                dy = -dx;
                dx = buffer;

                // increase segment length if necessary
                if (dx == 0) {
                    ++segment_length;
                }
            }
        }
        return("(" + y + ";" + x + ")");
    }

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        int NUMBER_OF_POINTS = Integer.valueOf(args[0]);   // or delete this line  
        String spiral_map = getXZForMap(NUMBER_OF_POINTS); // and put your int here
        System.out.println(spiral_map);
    } 
}

Adapted from this answer: Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin改编自这个答案: Algorithm for iterate over an outpiration on a discret 2D grid from the origin

Demo:演示:

$ java spiral 0
(0;0)
$ java spiral 5
(-1;0)
public int[] getPoints(int n){ int[] k = new int[2]; if(n == 0){ k[0] = 0; k[1] = 0; return k; } n--; int r = (int) (Math.floor((Math.sqrt(n + 1) -1) / 2) + 1); int p = (8 * r * (r - 1)) / 2; int a = (1 + n - p) % (r * 8); switch ((int) Math.floor(a / (r * 2))) { case 0: k[0] = a - r; k[1] = -r; return k; case 1: k[0] = r; k[1] = (a % (r * 2)) - r; return k; case 2: k[0] = r - (a % (r * 2)); k[1] = r; return k; case 3: k[0] = -r; k[1] = r - (a % (r * 2)); return k; } return null; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM