The concept of the Sierpinski triangle is very simple:
- Take a triangle
- Create four triangles out of that one by connecting the centers of each side
- Cut out the middle triangle
- Repeat the process with the remaining triangles
This is the examples out with 5 iteration
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static Vector < String > sierpinski(int n) { Vector < String > down = new Vector < String > (Arrays.asList("*")); String space = " "; for (int i = 0; i < n; i++) { Vector < String > newDown = new Vector < String > (); for (String x: down) newDown.add(space + x + space); for (String x: down) newDown.add(x + " " + x); down = newDown; space += space; } return down; } public static void main(String[] args) { for (String x: sierpinski(5)) System.out.println(x); } |
No comments:
Post a Comment