Last active
July 3, 2019 06:59
-
-
Save cristhianleonli/f4cd61e31decd6853784a5a4dff0ce46 to your computer and use it in GitHub Desktop.
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 Foundation | |
typealias Matrix = [[Bool]] | |
typealias Vector = [Bool] | |
func fractal(n: Int) { | |
let width = 63 | |
let height = width / 2 + 1 | |
var matrix = createMatrix(w: width, h: height) | |
if n == 0 { | |
printing(matrix) | |
return | |
} | |
matrix = drawPartition(matrix: matrix, slide: Node(x: 0, y: 0, w: width, h: height)) | |
if n == 1 { | |
printing(matrix) | |
return | |
} | |
let slides = split(matrix: matrix, level: n - 1) | |
for i in slides { | |
for slide in i { | |
matrix = drawPartition(matrix: matrix, slide: slide) | |
} | |
} | |
printing(matrix) | |
} | |
// ready | |
func createMatrix(w: Int, h: Int) -> Matrix { | |
var mat: Matrix = [] | |
var i = 1 | |
for _ in 0..<h { | |
var row = Vector.init(repeating: false, count: w) | |
row = switching(list: row, value: i) | |
i += 2 | |
mat.append(row) | |
} | |
return mat | |
} | |
// | |
func drawPartition(matrix: Matrix, slide: Node) -> Matrix { | |
var m = matrix | |
let w = slide.w | |
let h = slide.h | |
let x = slide.x + (w / 2 / 2) + 1 | |
let y = slide.y + h / 2 | |
var n = 1 | |
for i in y..<y+h/2 { | |
m[i] = switchingInSlice(row: m[i], a: x, b: x + w / 2 - 1, value: w / 2 + 1 - n) | |
n += 2 | |
} | |
return m | |
} | |
// ready | |
func switchingInSlice(row: Vector, a: Int, b: Int, value: Int) -> Vector { | |
let slice = Array(row[a...b]) | |
let new = switching(list: slice, value: value) | |
var result = row | |
var j = 0 | |
(a...b).forEach { i in | |
result[i] = new[j] | |
j += 1 | |
} | |
return result | |
} | |
// ready | |
func switching(list: Vector, value: Int) -> Vector { | |
var l = list | |
let center = list.count / 2 | |
l[center] = !l[center] | |
let times = (value - 1) / 2 | |
if times == 0 { | |
return l | |
} | |
(1...times).forEach { i in | |
l[center + i] = !l[center + i] | |
l[center - i] = !l[center - i] | |
} | |
return l | |
} | |
// ready | |
func split(matrix: Matrix, level: Int) -> [[Node]] { | |
return split(matrix: matrix, acc: [], level: level) | |
} | |
// ready | |
struct Node { | |
var x: Int | |
var y: Int | |
var w: Int | |
var h: Int | |
} | |
// ready | |
func split(matrix: Matrix, acc: [[Node]], level: Int) -> [[Node]] { | |
if level == acc.count { | |
return acc | |
} | |
if acc.isEmpty { | |
let w = matrix[0].count / 2 | |
let h = matrix.count / 2 | |
let top = Node(x: matrix[0].count / 2 / 2 + 1, y: 0, w: w, h: h) | |
let left = Node(x: 0, y: matrix.count / 2, w: w, h: h) | |
let right = Node(x: matrix[0].count / 2 + 1, y: matrix.count / 2, w: w, h: h) | |
return split(matrix: matrix, acc: [[top, left, right]], level: level) | |
} | |
let lastLevel = acc[acc.count - 1] | |
var levelNodes: [Node] = [] | |
for i in lastLevel { | |
let w = i.w / 2 | |
let h = i.h / 2 | |
let top = Node(x: i.x + i.w / 2 / 2 + 1, y: i.y, w: w, h: h) | |
let left = Node(x: i.x, y: i.y + i.h / 2, w: w, h: h) | |
let right = Node(x: i.x + i.w / 2 + 1, y: i.y + i.h / 2, w: w, h: h) | |
levelNodes.append(top) | |
levelNodes.append(left) | |
levelNodes.append(right) | |
} | |
var totalAcc = acc | |
totalAcc.append(levelNodes) | |
return split(matrix: matrix, acc: totalAcc, level: level) | |
} | |
// ready | |
func printing(_ mat: Matrix) { | |
for i in mat { | |
let j = i.map({ $0 ? "1" : "_" }).joined() | |
print(j) | |
} | |
} | |
fractal(n: 5) | |
""" | |
_______________________________1_______________________________ | |
______________________________1_1______________________________ | |
_____________________________1___1_____________________________ | |
____________________________1_1_1_1____________________________ | |
___________________________1_______1___________________________ | |
__________________________1_1_____1_1__________________________ | |
_________________________1___1___1___1_________________________ | |
________________________1_1_1_1_1_1_1_1________________________ | |
_______________________1_______________1_______________________ | |
______________________1_1_____________1_1______________________ | |
_____________________1___1___________1___1_____________________ | |
____________________1_1_1_1_________1_1_1_1____________________ | |
___________________1_______1_______1_______1___________________ | |
__________________1_1_____1_1_____1_1_____1_1__________________ | |
_________________1___1___1___1___1___1___1___1_________________ | |
________________1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1________________ | |
_______________1_______________________________1_______________ | |
______________1_1_____________________________1_1______________ | |
_____________1___1___________________________1___1_____________ | |
____________1_1_1_1_________________________1_1_1_1____________ | |
___________1_______1_______________________1_______1___________ | |
__________1_1_____1_1_____________________1_1_____1_1__________ | |
_________1___1___1___1___________________1___1___1___1_________ | |
________1_1_1_1_1_1_1_1_________________1_1_1_1_1_1_1_1________ | |
_______1_______________1_______________1_______________1_______ | |
______1_1_____________1_1_____________1_1_____________1_1______ | |
_____1___1___________1___1___________1___1___________1___1_____ | |
____1_1_1_1_________1_1_1_1_________1_1_1_1_________1_1_1_1____ | |
___1_______1_______1_______1_______1_______1_______1_______1___ | |
__1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____1_1__ | |
_1___1___1___1___1___1___1___1___1___1___1___1___1___1___1___1_ | |
1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment