-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.swift
47 lines (41 loc) · 1.42 KB
/
Model.swift
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
import Foundation
public class Model {
func makeMandelbrotSet(
max: (Double, Double),
min: (Double, Double),
size: Int,
limit: Double = 100.0,
maxCycle: Int = 500
) -> [[Int]] {
let scale = (
(max.0 - min.0) / Double(size - 1),
(max.1 - min.1) / Double(size - 1)
)
var table = [(Double, Double)]()
table.append(min)
for i in 1 ..< size {
table.append((
table[i - 1].0 + scale.0,
table[i - 1].1 + scale.1
))
}
var history = [(Double, Double)](repeating: (0, 0), count: maxCycle)
var result = [[Int]](repeating: [Int](repeating: 0, count: size), count: size)
for row in 0 ..< size {
for col in 0 ..< size {
for cycle in 1 ..< maxCycle {
let previous = history[cycle - 1]
history[cycle] = (
previous.0 * previous.0 - previous.1 * previous.1 + table[row].0,
2 * previous.0 * previous.1 + table[col].1
)
if (limit <= sqrt(history[cycle].0 * history[cycle].0 + history[cycle].1 * history[cycle].1)) {
result[row][col] = (cycle % 255) + 1
break
}
}
}
}
return result
}
}