Create endless grid spiral in java -
i want create endless spiral in java this:
i want send integer 1-∞ number , it's spiral map. example:
getxzformap(0); ----> (0;0)
getxzformap(5); ----> (-1;0)
came code now, not know how go further:
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; }
import java.util.scanner; class spiral { private static string getxzformap(int np) { // (dx, dy) vector - direction in move right int dx = 0; int dy = 1; // length of current segment int segment_length = 1; // current position (x, y) , how of current segment passed int x = 0; int y = 0; int segment_passed = 0; if (np == 0){ return ("(" + y + ";" + x + ")"); } (int n = 0; n < np; ++n) { // make step, add 'direction' vector (dx, dy) current position (x, y) x += dx; y += dy; ++segment_passed; if (segment_passed == segment_length) { // done 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 line string spiral_map = getxzformap(number_of_points); // , put int here system.out.println(spiral_map); } }
adapted answer: algorithm iterating on outward spiral on discrete 2d grid origin
demo:
$ java spiral 0 (0;0) $ java spiral 5 (-1;0)
Comments
Post a Comment