|
5 | 5 |
|
6 | 6 | class AffineTransform: |
7 | 7 | def __init__(self, a=0., b=0., c=0., d=0., e=0., f=0.): |
8 | | - self._abcd = self._ef = np.asarray([[a, b], [c, d]]) |
| 8 | + self._abcd = np.asarray([[a, b], [c, d]]) |
9 | 9 | self._ef = np.asarray([[e], [f]]) |
10 | 10 |
|
11 | 11 | def __call__(self, x, y): |
12 | 12 | # np.dot maybe introducing overhead? |
| 13 | + # probably not, it's much faster now after optimizing, by parametrizing the scatter plot |
13 | 14 | return np.dot(self._abcd, [[x], [y]]) + self._ef |
14 | 15 |
|
15 | 16 |
|
16 | | -f1 = AffineTransform(0,0,0,0.16,0,0) |
| 17 | +f1 = AffineTransform(0, 0, 0, 0.16, 0, 0) |
17 | 18 | f2 = AffineTransform(0.85, 0.04, -0.04, 0.85, 0, 1.60) |
18 | 19 | f3 = AffineTransform(0.20, -0.26, 0.23, 0.22, 0, 1.6) |
19 | 20 | f4 = AffineTransform(-0.15, 0.28, 0.26, 0.24, 0, 0.44) |
20 | 21 |
|
21 | 22 | functions = [f1, f2, f3, f4] |
22 | 23 | p_cumulative = [0.01, 0.86, 0.92, 1.00] |
| 24 | + |
| 25 | + |
23 | 26 | def select(funcs, probcum): |
24 | 27 | r = np.random.random() |
25 | 28 | for j, p in enumerate(probcum): |
26 | 29 | if r < p: |
27 | 30 | return funcs[j] |
28 | 31 |
|
29 | 32 |
|
| 33 | +#--------plotting and saving pic |
30 | 34 | plt.axis("equal") |
31 | 35 | plt.axis('off') |
32 | | -x = [[0], [0]] |
33 | | -plt.scatter(0, 0) |
34 | | -# TODO: optimize |
35 | | -for i in range(5000): |
36 | | - x = select(functions, p_cumulative)(x[0][0], x[1][0]) |
37 | | - plt.scatter(x[0], x[1]) |
| 36 | +n = 50000 |
| 37 | +fern = np.zeros((n + 1, 2), dtype=float) |
| 38 | + |
| 39 | +for i in range(n): |
| 40 | + fern[i + 1] = (select(functions, p_cumulative)(fern[i][0], fern[i][1])).reshape(2) |
38 | 41 |
|
| 42 | +plt.scatter(fern[:, 0], fern[:, 1], s=0.2, c="g", marker=",") |
39 | 43 | plt.savefig(pathlib.Path(__file__).parent.resolve().__str__() + '\\figures\\' + "barnsley_fern", |
40 | | - dpi=400, s=0.1) |
| 44 | + dpi=400) |
41 | 45 |
|
| 46 | +plt.show() |
0 commit comments