Skip to content

Commit d82f79a

Browse files
authored
Create cantor set.jl
1 parent 0a6586c commit d82f79a

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

cantor set.jl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
# coding: utf-8
3+
4+
# In[1]:
5+
6+
7+
#fractal is one of the interesting topics in geometry
8+
#it is usually described by a recursive function
9+
#voila,here we are!
10+
using Plots
11+
12+
13+
# In[2]:
14+
15+
16+
#initialize
17+
x1=0
18+
x2=3
19+
y=0
20+
bar_height=5
21+
between_interval=10
22+
n=6;
23+
24+
25+
# In[3]:
26+
27+
28+
#create rectangle shape
29+
rectangle(w,h,x,y)=Shape(x.+[0,w,w,0],y.-[0,0,h,h])
30+
31+
32+
# In[4]:
33+
34+
35+
#cantor set is one of the simplest fractal shape
36+
#at each iteration,we divide each bar into three equal parts
37+
#we remove the mid part from each bar and keep the rest
38+
#this effectively creates a binary tree
39+
#check the link below for more details on math
40+
# https//www.math.stonybrook.edu/~scott/Book331/Cantor_sets.html
41+
function cantor_set(x1,x2,y,n,
42+
bar_height=5,between_interval=10)
43+
44+
#base case
45+
if n==0
46+
return
47+
48+
else
49+
50+
#viz the first 1/3 and the last 1/3
51+
plot!(rectangle((x2-x1)/3,bar_height,x1,y-between_interval))
52+
plot!(rectangle((x2-x1)/3,bar_height,x2-(x2-x1)/3,y-between_interval))
53+
54+
#recursion
55+
cantor_set(x1,x1+(x2-x1)/3,
56+
y-between_interval,
57+
n-1)
58+
cantor_set(x2-(x2-x1)/3,x2,
59+
y-between_interval,
60+
n-1)
61+
62+
end
63+
64+
end
65+
66+
67+
# In[5]:
68+
69+
70+
#viz
71+
#as n increases
72+
#the bar gets too slim to be visible
73+
fig=plot(legend=false,grid=false,axis=false,ticks=false)
74+
plot!(rectangle((x2-x1),bar_height,x1,y))
75+
cantor_set(x1,x2,y,n)
76+
fig
77+
78+

0 commit comments

Comments
 (0)