-3

I want to create endless spiral in java like this:

Endless spiral

I simply want to pass a number from 1-∞ and get it's spiral map.

Example:

getPoints(0); Would result in answer (0;0)

getPoints(5); Would result in answer (-1;0)

TerZer
  • 83
  • 8
  • 2
    You forgot to paste the code wherein you made a serious and concerted effort to solve this problem yourself. – Kon Jul 26 '17 at 00:00
  • Added code you wanted – TerZer Jul 26 '17 at 00:02
  • Map class is nothing special, just saving x and z. – TerZer Jul 26 '17 at 00:04
  • 1
    Using sin(_θ_) and cos(_θ_) (along with radius _r_) works fine if you're trying to draw an _actual_ spiral, but it looks like you're trying to draw a grid "in spiral order". If you try to round your polar co-ordinates to Cartesian integers, you'll end up with all kinds of awful edge cases. You're much better off thinking in terms of integer-valued (_x_, _y_) ordered pairs. – Kevin J. Chase Jul 26 '17 at 00:48
  • Possible duplicate of [Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin](https://stackoverflow.com/questions/3706219/algorithm-for-iterating-over-an-outward-spiral-on-a-discrete-2d-grid-from-the-or) – Kevin J. Chase Jul 28 '17 at 20:12

3 Answers3

2

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).
  • In round 1, you walk the 8 cells around it.
  • In round 2, you walk the 16 cells around that.
  • In round 3, you walk the 24 cells around that.

Now see the pattern:

  • In each round, the walk on each side is 2 more steps, totally 8 more steps per round.
  • Each round starts 1 cell to the right of the upper-left corner.
  • Walking on each side starts 1 cell from the corner.

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.
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 2
    Don't show code in a comment. If you already have code: 1) Why didn't you show it in the question. 2) *What* is your question, i.e. what answer(s) are you looking for? – Andreas Jul 26 '17 at 00:50
  • 1
    @TerZer Cool. What did _you_ come up with? Post _that_ in your question, then you will get answers you'll like. The more effort you put into writing your question, the more we put into answers. –  Jul 26 '17 at 00:51
  • @Andreas this code, does not work how I want. I simply want to send integer from 1-∞ number and get it's spiral map. Example: getXZForMap(0); ----> (0;0) getXZForMap(5); ----> (-1;0) Will add this to question – TerZer Jul 26 '17 at 11:33
  • 1
    *"I simply want"* Then simply write the code to do that, though I wouldn't use the word "simply" for your requirement. With this answer, I gave you the outline of one way to solve the problem. I'm not going to give you the code, because this is an assignment/challenge for *you* to solve, not us. Your friend came up with another way to do it. It's up to you to decide how to proceed from here. – Andreas Jul 26 '17 at 13:54
0
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

Demo:

$ java spiral 0
(0;0)
$ java spiral 5
(-1;0)
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;       
}
TerZer
  • 83
  • 8