Tuesday, November 1, 2011

Triangles!

public class Pattern {
public static void main(String[] args) {
int width = 150;
int height = 70;
int[][] grid = new int[width][height];
grid[width/2][0] = 1;
for (int f=1; f<height; f++) {
for (int i=1; i<width-1; i++) {
if (grid[i-1][f-1] == 1 && grid[i+1][f-1] == 0) {
grid[i][f] = 1;
}
if (grid[i-1][f-1] == 0 && grid[i+1][f-1] == 1) {
grid[i][f] = 1;
}
}
}
// Print it out
for (int i=0; i<height; i++) {
for (int j=0; j<width; j++) {
if (grid[j][i] == 1) {
System.out.print("X");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
view raw Pattern.java hosted with ❤ by GitHub
Adjust your font size accordingly. (This also happens to be Wolfram's rule #18. Rule #18 of the internet is "Anything that can be labeled can be hated"). See you next week!

No comments:

Post a Comment