人懒的时候容易被一些事情所烦,需要找些事情放松放松。看看图形是个不错的选择,比如经典的Mandelbrot集。虽然R画的没有绚烂,但是还能看。下面是搜集的稍作修改的一段代码和图形。
1.ggplot2的图形,循环迭代较多,运行确实很慢。
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 | library(ggplot2) max_iter = 25 cl = colours() step = seq(-2, 0.8, by = 0.005) points = array(0, dim = c(length(step)^2, 3)) t = 0 for (a in step) { for (b in step + 0.6) { x = 0 y = 0 n = 0 dist = 0 while (n < max_iter & dist < 4) { n = n + 1 newx = a + x^2 - y^2 newy = b + 2 * x * y dist = newx^2 + newy^2 x = newx y = newy } if (dist < 4) { color = 24 } else { color = n * floor(length(cl)/max_iter) } t = t + 1 points[t, ] = c(a, b, color) } } df = as.data.frame(points) ggplot(data = df, aes(V1, V2, color = cl[V3])) + geom_point(size = 0.5) + opts(panel.background = theme_blank(), panel.grid.major = theme_blank(), panel.grid.minor = theme_blank(), axis.ticks = theme_blank(), axis.text.x = theme_blank(), axis.text.y = theme_blank(), axis.title.x = theme_blank(), axis.title.y = theme_blank(), legend.position = "none") |
渲染的图形还是非常漂亮的,如下(点击图形看大图,否则看不出颜色渲染的效果)
2. 动态看图形变化,此转于此处,代码简洁易理解。不过代码做出来的图与原网页展示的图形不同,颜色怎么调都没有他们的漂亮,主要是迭代次数还不够,也不够精细,直接看原链接的吧,搭配颜色是个细致活,伤脑细胞。
ggplot2的性能的确让人有些抓狂,关于曼德布洛特集,我以前写过一篇博客,http://www.bjt.name/2009/12/mandelbrot-set/
确实让人着迷~~配色和思想~~如果速度能提升就好了,应该很快就会有大大的改进!
思喆大哥的那个mandelbrot set早已拜读过呀~~呵呵,分形还有更多的应用,如果都能发掘出来就好了