forked from jarekPierchala/SierpinskiChallenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSierpinskiCarpet.java
67 lines (61 loc) · 2.45 KB
/
SierpinskiCarpet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.ArrayList;
import java.util.List;
public class SierpinskiCarpet {
public static void main(String[] args) {
drawSierpinskiCarpet(3, 27);
}
public static void drawSierpinskiCarpet(int order, int size) {
if (size % Math.pow(3, order) != 0) {
throw new IllegalArgumentException("size must be a multiple of 3^order");
}
innerDrawSierpinskiCarpet(order, size);
}
/**
* Draws a Sierpinski carpet of the order and size, using *
*
* @param order order of the triangle
* @param size size, multiple of 3^order
*/
private static void innerDrawSierpinskiCarpet(int order, int size) {
getLinesSierpinskiCarpet(order, size).forEach(System.out::println);
}
/**
* See Sierpinski Triangle for why we use String.
* Base case order = 0 just gets size lines filled with stars.
* Each Carpet is a 3x3 compound, with center empty (spaces) and each of the other 8 a carpet of 1 order less.
* We build the carpet by calculating the lesser order, then composing the lines to go at the top and bottom
* (which are the same), and the lines that go in the middle (with the middle space)
* Having top/bottom and middle, final result is concatenating those
*
* @param order order of the carpet
* @param size size of side
* @return list of (side = size) strings to draw
*/
private static List<String> getLinesSierpinskiCarpet(int order, int size) {
if (order == 0) {
List<String> lines = new ArrayList<>();
String filled = StringHelp.getCharLine(size, '*');
for (int i = 0; i < size; i++) {
lines.add(filled);
}
return lines;
} else {
int third = size / 3;
List<String> lesser = getLinesSierpinskiCarpet(order - 1, third);
List<String> upDown = new ArrayList<>();
List<String> middle = new ArrayList<>();
String emptyThird = StringHelp.getEmptyLine(third);
for (int i = 0; i < third; i++) {
String lineTopBottom = lesser.get(i);
upDown.add(lineTopBottom + lineTopBottom + lineTopBottom);
middle.add(lineTopBottom + emptyThird + lineTopBottom);
}
middle.addAll(upDown);
middle.addAll(0, upDown);
return middle;
}
}
private SierpinskiCarpet() {
// utility class
}
}