forked from bmaslakov/kotlin-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SierpinskiTriangle.kt
67 lines (62 loc) · 2.19 KB
/
SierpinskiTriangle.kt
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
package io.uuddlrlrba.ktalgs.geometry
import io.uuddlrlrba.ktalgs.math.log2
class SierpinskiTriangle {
/**
* @param d base of the triangle (i.e. the smallest dimension)
* @param n fractalization depth (must be less than log2(d))
* @throws IllegalArgumentException if n > log2(d)
*/
fun makeTriangles(base: Int, n: Int): Array<BooleanArray> {
if (n > log2(base)) throw IllegalArgumentException("fractalization depth must be less than log2(base): " +
"$n > ${log2(base).toInt()}")
val arr = Array(base, { BooleanArray(base * 2 - 1) })
drawTriangles(n, arr, 0, 0, base - 1, base * 2 - 2)
return arr
}
fun drawTriangles(n: Int, arr: Array<BooleanArray>, top: Int, left: Int, bottom: Int, right: Int) {
if (n > 0) {
val width = right - left
val height = bottom - top
drawTriangles(n - 1, arr,
top,
left + width / 4 + 1,
top + height / 2,
right - width / 4 - 1
)
drawTriangles(n - 1, arr,
top + 1 + height / 2,
left,
bottom,
left + width / 2 - 1
)
drawTriangles(n - 1, arr,
top + 1 + height / 2,
left + width / 2 + 1,
bottom,
right
)
} else {
drawTriangles(arr, top, left, bottom, right)
}
}
fun drawTriangles(arr: Array<BooleanArray>, top: Int, left: Int, bottom: Int, right: Int) {
val height = bottom - top
val width = right - left
for (i in 0..height) {
for (j in (height - i)..width / 2) {
arr[top + i][left + j] = true
}
for (j in (width / 2..width / 2 + i)) {
arr[top + i][left + j] = true
}
}
}
}
fun main(args : Array<String>) {
SierpinskiTriangle()
.makeTriangles(128, 7)
.map { array ->
array.map { if (it) 'x' else ' ' }.joinToString(separator = "")
}
.forEach { println(it) }
}