-
Notifications
You must be signed in to change notification settings - Fork 29
/
mandelbrot.R
56 lines (47 loc) · 1.07 KB
/
mandelbrot.R
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
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 # black
}
else
{
color=n*floor(length(cl)/max_iter)
}
t=t+1
points[t,]=c(a,b,color)
}
}
df=as.data.frame(points)
# Can change the colors by fiddling with the following.
# last_plot() + scale_colour_manual(values=sort(c("#00000000", rainbow(23)), decreasing=FALSE))
ggplot(data=df, aes(V1, V2, color=cl[V3]))+
geom_point() +
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')
ggsave('mandelbrot_ggplot2.png')
print('Image Saved.')
dev.off()