【问题标题】:Create endless grid spiral in java在java中创建无尽的网格螺旋
【发布时间】:2018-01-01 02:07:13
【问题描述】:

我想像这样在java中创建无尽的螺旋:

我只是想发送 1-∞ 数的整数并得到它的螺旋图。示例:

getXZForMap(0); ----> (0;0)

getXZForMap(5); ----> (-1;0)

现在想出了这段代码,但不知道如何进一步:

public int[] getXZForMap(int i){

    int[] k = new int[2];

    if(i > 0){

        double s = i/8;
        int stage = (int) Math.ceil(s);
        int corner_size = ((stage*8)+4)/4;



    }
    else{
        k[0] = 0;
        k[1] = 0;
    }

    return k;
}

【问题讨论】:

  • 您忘记粘贴代码,您在其中认真和一致地努力自己解决了这个问题。
  • 添加了你想要的代码
  • Map类没什么特别的,只是保存了x和z。
  • 如果你使用 sin(θ) 和 cos(θ)(连同半径 r)可以正常工作重新尝试绘制 实际 螺旋,但看起来您正在尝试“按螺旋顺序”绘制网格。如果您尝试将极坐标四舍五入为笛卡尔整数,您最终会遇到各种可怕的边缘情况。您最好考虑整数值 (x, y) 有序对。

标签: java


【解决方案1】:

从算法上思考。识别模式。

模式是你绕着圆周走,一次一层。

  • 在第 0 轮中,您位于中心点 (0,0)
  • 在第 1 轮中,您绕着它走 8 个牢房。
  • 在第 2 轮中,您绕着 16 个牢房走。
  • 在第 3 轮中,您绕着 24 个牢房走。

现在看看模式:

  • 在每一轮中,每边多走 2 步,每轮总共多走 8 步。
  • 每一轮从左上角右侧的 1 个单元格开始。
  • 从拐角处开始,每边走 1 个单元格。

所以,既然你看到了模式,你可以这样做:

  • 计算你在哪一轮。
  • 对于这一轮,计算你站在哪一边。
  • 然后计算你在那一侧走了多少步。
  • 最后,据此计算坐标。

【讨论】:

  • 我的朋友想出了这个代码:public int[] getXZForMap(int index){ int x=0, y=0, dist=0, direction=1; for(int i = 0;true; i++){ if((distance+1)*direction > index){ x+=(((++dist-index)*direction)); break; }else{ x +=(++dist * direction); if((distance+1)*direction > index){ y+=(((++dist-index)*direction)); break; }else{ y +=(++dist * direction); direction *=-1; } } } int[] k = new int[2]; k[0]=x; k[1]=y; return k; }
  • 不要在评论中显示代码。如果您已经有代码:1)您为什么不在问题中显示它。 2) 什么是您的问题,即您在寻找什么答案?
  • @TerZer 酷。 想出了什么?在你的问题中发布那个,然后你就会得到你喜欢的答案。您在编写问题时付出的努力越多,我们在答案中投入的精力就越多。
  • @Andreas 此代码无法按我的意愿工作。我只是想从 1-∞ 数发送整数并得到它的螺旋图。示例:getXZForMap(0); ----> (0;0) getXZForMap(5); ----> (-1;0) 将这个添加到问题中
  • “我只是想要” 然后只需编写代码即可,尽管我不会使用“简单”一词来满足您的要求。有了这个答案,我给了你解决问题的一种方法的大纲。我不会给你代码,因为这是要解决的任务/挑战,而不是我们。你的朋友想出了另一种方法来做到这一点。由您决定如何从这里开始。
【解决方案2】:
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);
    } 
}

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

演示:

$ java spiral 0
(0;0)
$ java spiral 5
(-1;0)

【讨论】:

    猜你喜欢
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2022-07-21
    • 2018-12-02
    • 1970-01-01
    • 2016-12-17
    • 2015-08-09
    相关资源
    最近更新 更多