-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsierpiński triangle.jl
72 lines (44 loc) · 1.64 KB
/
sierpiński triangle.jl
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# coding: utf-8
# In[1]:
#fractal is one of the interesting topics in geometry
#it is usually described by a recursive function
#voila,here we are!
using Plots
# In[2]:
function sierpiński_triangle(coordinates,lvl,fig,colour="black")
#stop recursion
if lvl==0
return
end
#unpack coordinates
#coordinates have to follow the order of left,mid,right
left=coordinates[1];mid=coordinates[2];right=coordinates[3]
#compute mid point for each line
left_new=((mid[1]-left[1])/2+left[1],(mid[2]-left[2])/2+left[2])
mid_new=((right[1]-left[1])/2+left[1],(right[2]-left[2])/2+left[2])
right_new=((right[1]-mid[1])/2+mid[1],(mid[2]-right[2])/2+right[2])
#create new coordinates
coordinates_new=[left_new,mid_new,right_new]
#viz coordinates
for i in coordinates
for j in coordinates
if i!=j
#use ! to add to the existing figure
plot!([i[1],j[1]],[i[2],j[2]],
color=colour,axis=false,
legend=false,grid=false)
end
end
end
#recursive plot sub triangles
sierpiński_triangle([left,left_new,mid_new],lvl-1,fig)
sierpiński_triangle([left_new,mid,right_new],lvl-1,fig)
sierpiński_triangle([mid_new,right_new,right],lvl-1,fig)
end
# In[3]:
#annoying feature of julia
#plot wont show up in a loop unless i specify
gr(size=(250,250))
fig=plot(legend=false,grid=false,axis=false,showaxis=false)
sierpiński_triangle([(0,0),(0.5,1),(1,0)],4,fig)
fig