Last active
August 29, 2015 14:03
-
-
Save dzlab/68195b599523620ff86d to your computer and use it in GitHub Desktop.
Filling a map with a Spiral algorithm (description here http://www.commentcamarche.net/forum/affich-1824691-algorithme-niveau-signore)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Point; | |
import java.util.Scanner; | |
public class Spiral { | |
private final int n; | |
private final int center; | |
private final int[][] matrix; | |
public Spiral(int n) | |
{ | |
this.n = n; | |
this.center = n/2; | |
this.matrix = new int[n][n]; | |
} | |
/** | |
* Move to the left square | |
*/ | |
public Point left(Point p) | |
{ | |
int x = p.x - 1; | |
int y = p.y; | |
return new Point(x, y); | |
} | |
/** | |
* Move to the right square | |
*/ | |
public Point right(Point p) | |
{ | |
int x = p.x + 1; | |
int y = p.y; | |
return new Point(x, y); | |
} | |
/** | |
* Move to the upper square | |
*/ | |
public Point up(Point p) | |
{ | |
int x = p.x; | |
int y = p.y - 1; | |
return new Point(x, y); | |
} | |
/** | |
* Move to the down square | |
*/ | |
public Point down(Point p) | |
{ | |
int x = p.x; | |
int y = p.y + 1; | |
return new Point(x, y); | |
} | |
/** | |
* Move to the next square | |
*/ | |
public Point next(Point p) | |
{ | |
if(p.y == center && p.x == center) | |
{ | |
return right(p); | |
} | |
Point next = null; | |
// find the corner of current inner matrix | |
int diff = Math.max(Math.abs(p.x - center), Math.abs(p.y - center)); | |
int x1 = center - diff; | |
int x2 = center + diff; | |
int y1 = center - diff; | |
int y2 = center + diff; | |
System.out.println("diff: "+diff); | |
System.out.println("(x1, y1): "+x1+","+y1); | |
System.out.println("(x1, y2): "+x1+","+y2); | |
System.out.println("(x2, y1): "+x2+","+y1); | |
System.out.println("(x2, y2): "+x2+","+y2); | |
// decide the next move | |
if(p.x == x1) | |
{ | |
if(p.y == y1) | |
{ | |
next = down(p); | |
} | |
else if(p.y == y2) | |
{ | |
next = right(p); | |
} | |
else | |
{ | |
next = down(p); | |
} | |
} | |
else if(p.x == x2) | |
{ | |
if(p.y == y1) | |
{ | |
next = left(p); | |
} | |
else if(p.y == y2) | |
{ | |
next = right(p); | |
} | |
else | |
{ | |
next = up(p); | |
} | |
} | |
else if(p.y == y1) | |
{ | |
next = left(p); | |
} | |
else | |
{ | |
next = right(p); | |
} | |
System.out.println("next for "+p+" is "+next.x+","+next.y+" and center is "+center); | |
return next; | |
} | |
/** | |
* Set the value of a given square | |
*/ | |
public void set(int row, int column, int v) | |
{ | |
matrix[row][column] = v; | |
} | |
/** | |
* Print current state of the matrix | |
*/ | |
public void print() | |
{ | |
StringBuffer buf = null; | |
for(int i=0; i<n; i++) | |
{ | |
buf = new StringBuffer(); | |
for(int j=0; j<n; j++) | |
{ | |
buf.append(matrix[i][j]).append(" "); | |
} | |
System.out.println(buf.toString()); | |
} | |
} | |
/** | |
* Run the program | |
*/ | |
public static void main(String[] args) | |
{ | |
System.out.print("Type an odd number representing the matrix size: "); | |
Scanner in = new Scanner(System.in); | |
int n = in.nextInt(), center = n/2, val = 1; | |
in.close(); | |
Spiral spiral = new Spiral(n); | |
Point p = new Point(center, center); | |
while(p.x < n && p.y < n) | |
{ | |
spiral.set(p.y, p.x, val++); | |
p = colimaçon.next(p); | |
} | |
colimaçon.print(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment