Skip to content

Commit 334f4b3

Browse files
authored
Add files via upload
1 parent cc1b96d commit 334f4b3

File tree

2 files changed

+304
-0
lines changed

2 files changed

+304
-0
lines changed

jerusalem cross.jl

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
#create rectangle shape
17+
rectangle(w,h,x,y)=Shape(x.+[0,w,w,0],y.-[0,0,h,h])
18+
19+
#compute euclidean distance
20+
function euclidean_distance(point1,point2)
21+
return ((point1[1]-point2[1])^2+(point1[2]-point2[2])^2)
22+
end
23+
24+
25+
# In[3]:
26+
27+
28+
#recursively plot jerusalem cross
29+
#it kinda looks like flag of georgia (n=2)
30+
#i mean the eurasian country not a yankee state
31+
#i call it jerusalem cross but it is aka cross menger square,jerusalem square
32+
#it is a 2d version of jerusalem cube
33+
#a good reference to jerusalem cube
34+
# https://robertdickau.com/jerusalemcube.html
35+
#a good understanding of sierpiński carpet is helpful as well
36+
# https://github.com/je-suis-tm/recursion-and-dynamic-programming/blob/master/sierpi%C5%84ski%20carpet.py
37+
#do not confuse it with quadratic cross,which creates new crosses from the tips
38+
# https://onlinemathtools.com/generate-quadratic-cross-fractal
39+
#or fibonacci snowflakes,which is more like koch snowflake
40+
# http://www.slabbe.org/Publications/2011-fibo-snowflakes.pdf
41+
#or vicsek fractal,which is more similar to crosslet cross
42+
# https://en.wikipedia.org/wiki/Vicsek_fractal
43+
function jerusalem_cross(top_left,top_right,bottom_left,bottom_right,n)
44+
45+
if n<=0
46+
return
47+
else
48+
49+
#compute the width
50+
width=euclidean_distance(top_left,top_right)
51+
52+
#create the cross
53+
plot!(rectangle(width*((2)-1)^2,
54+
width*(1-2*(((2)-1)^2)),
55+
top_left[1]+width*((2)-1),
56+
top_left[2]-width*((2)-1)^2),
57+
color="black")
58+
plot!(rectangle(width*(1-2*(((2)-1)^2)),
59+
width*((2)-1)^2,
60+
top_left[1]+width*((2)-1)^2,
61+
top_left[2]-width*((2)-1)),
62+
color="black")
63+
64+
#top left corner recursion
65+
jerusalem_cross(top_left,(top_left[1]+width*((2)-1),top_left[2]),
66+
(top_left[1],top_left[2]-width*((2)-1)),
67+
(top_left[1]+width*((2)-1),
68+
top_left[2]-width*((2)-1)),n-1)
69+
70+
#top right corner recursion
71+
jerusalem_cross((top_right[1]-width*((2)-1),top_left[2]),top_right,
72+
(top_right[1]-width*((2)-1),
73+
top_left[2]-width*((2)-1)),
74+
(top_right[1],top_left[2]-width*((2)-1)),n-1)
75+
76+
#bottom left corner recursion
77+
jerusalem_cross((bottom_left[1],bottom_left[2]+width*((2)-1)),
78+
(bottom_left[1]+width*((2)-1),
79+
bottom_left[2]+width*((2)-1)),
80+
bottom_left,
81+
(bottom_left[1]+width*((2)-1),bottom_left[2]),n-1)
82+
83+
#bottom right corner recursion
84+
jerusalem_cross((bottom_right[1]-width*((2)-1),
85+
bottom_right[2]+width*((2)-1)),
86+
(bottom_right[1],
87+
bottom_right[2]+width*((2)-1)),
88+
(bottom_right[1]-width*((2)-1),
89+
bottom_right[2]),
90+
bottom_right,n-1)
91+
92+
#top mid corner recursion
93+
jerusalem_cross((top_left[1]+width*((2)-1),top_left[2]),
94+
(top_right[1]-width*((2)-1),top_left[2]),
95+
(top_left[1]+width*((2)-1),
96+
top_left[2]-width*((2)-1)^2),
97+
(top_right[1]-width*((2)-1),
98+
top_left[2]-width*((2)-1)^2),n-2)
99+
100+
#bottom mid corner recursion
101+
jerusalem_cross((bottom_left[1]+width*((2)-1),
102+
bottom_left[2]+width*((2)-1)^2),
103+
(bottom_right[1]-width*((2)-1),
104+
bottom_left[2]+width*((2)-1)^2),
105+
(bottom_left[1]+width*((2)-1),
106+
bottom_left[2]),
107+
(bottom_right[1]-width*((2)-1),
108+
bottom_left[2]),n-2)
109+
110+
#left mid corner recursion
111+
jerusalem_cross((bottom_left[1],
112+
top_left[2]-width*((2)-1)),
113+
(bottom_left[1]+width*((2)-1)^2,
114+
top_left[2]-width*((2)-1)),
115+
(bottom_left[1],bottom_left[2]+width*((2)-1)),
116+
(bottom_left[1]+width*((2)-1)^2,
117+
bottom_left[2]+width*((2)-1)),n-2)
118+
119+
#right mid corner recursion
120+
jerusalem_cross((bottom_right[1]-width*((2)-1)^2,
121+
top_right[2]-width*((2)-1)),
122+
(bottom_right[1],
123+
top_right[2]-width*((2)-1)),
124+
(bottom_right[1]-width*((2)-1)^2,
125+
bottom_right[2]+width*((2)-1)),
126+
(bottom_right[1],bottom_right[2]+width*((2)-1)),
127+
n-2)
128+
129+
end
130+
131+
end
132+
133+
134+
# In[4]:
135+
136+
137+
#initialize
138+
top_left=(0,0)
139+
top_right=(1,0)
140+
bottom_left=(0,-1)
141+
bottom_right=(1,-1)
142+
n=5;
143+
144+
145+
# In[5]:
146+
147+
148+
#viz
149+
gr(size=(500,500))
150+
fig=plot(legend=false,grid=false,axis=false,ticks=false,
151+
152+
xlim=(top_left[1],top_right[1]),
153+
ylim=(bottom_right[2],top_right[2]))
154+
jerusalem_cross(top_left,top_right,bottom_left,bottom_right,n)
155+
fig
156+
157+

jerusalem cross.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
import matplotlib.pyplot as plt
11+
12+
13+
# In[2]:
14+
15+
16+
#compute euclidean distance
17+
def euclidean_distance(point1,point2):
18+
return ((point1[0]-point2[0])**2+(point1[1]-point2[1])**2)**0.5
19+
20+
21+
# In[3]:
22+
23+
24+
#recursively plot jerusalem cross
25+
#it kinda looks like flag of georgia (n=2)
26+
#i mean the eurasian country not a yankee state
27+
#i call it jerusalem cross but it is aka cross menger square,jerusalem square
28+
#it is a 2d version of jerusalem cube
29+
#a good reference to jerusalem cube
30+
# https://robertdickau.com/jerusalemcube.html
31+
#a good understanding of sierpiński carpet is helpful as well
32+
# https://github.com/je-suis-tm/recursion-and-dynamic-programming/blob/master/sierpi%C5%84ski%20carpet.py
33+
#do not confuse it with quadratic cross,which creates new crosses from the tips
34+
# https://onlinemathtools.com/generate-quadratic-cross-fractal
35+
#or fibonacci snowflakes,which is more like koch snowflake
36+
# http://www.slabbe.org/Publications/2011-fibo-snowflakes.pdf
37+
#or vicsek fractal,which is more similar to crosslet cross
38+
# https://en.wikipedia.org/wiki/Vicsek_fractal
39+
def jerusalem_cross(top_left,top_right,bottom_left,bottom_right,n):
40+
41+
if n<=0:
42+
return
43+
else:
44+
45+
#compute the length
46+
length=euclidean_distance(top_left,top_right)
47+
48+
#create the cross
49+
plt.fill_between(
50+
[top_left[0]+length*(2**0.5-1),top_right[0]-length*(2**0.5-1)],
51+
[bottom_left[1]+length*(2**0.5-1)**2,bottom_left[1]+length*(2**0.5-1)**2],
52+
[top_left[1]-length*(2**0.5-1)**2,top_left[1]-length*(2**0.5-1)**2],
53+
color='k')
54+
plt.fill_between(
55+
[top_left[0]+length*(2**0.5-1)**2,top_right[0]-length*(2**0.5-1)**2],
56+
[bottom_left[1]+length*(2**0.5-1),bottom_left[1]+length*(2**0.5-1)],
57+
[top_left[1]-length*(2**0.5-1),top_left[1]-length*(2**0.5-1)],
58+
color='k')
59+
60+
#top left corner recursion
61+
jerusalem_cross(top_left,(top_left[0]+length*(2**0.5-1),top_left[1]),
62+
(top_left[0],top_left[1]-length*(2**0.5-1)),
63+
(top_left[0]+length*(2**0.5-1),
64+
top_left[1]-length*(2**0.5-1)),n-1)
65+
66+
#top right corner recursion
67+
jerusalem_cross((top_right[0]-length*(2**0.5-1),top_left[1]),top_right,
68+
(top_right[0]-length*(2**0.5-1),
69+
top_left[1]-length*(2**0.5-1)),
70+
(top_right[0],top_left[1]-length*(2**0.5-1)),n-1)
71+
72+
#bottom left corner recursion
73+
jerusalem_cross((bottom_left[0],bottom_left[1]+length*(2**0.5-1)),
74+
(bottom_left[0]+length*(2**0.5-1),
75+
bottom_left[1]+length*(2**0.5-1)),
76+
bottom_left,
77+
(bottom_left[0]+length*(2**0.5-1),bottom_left[1]),n-1)
78+
79+
#bottom right corner recursion
80+
jerusalem_cross((bottom_right[0]-length*(2**0.5-1),
81+
bottom_right[1]+length*(2**0.5-1)),
82+
(bottom_right[0],
83+
bottom_right[1]+length*(2**0.5-1)),
84+
(bottom_right[0]-length*(2**0.5-1),
85+
bottom_right[1]),
86+
bottom_right,n-1)
87+
88+
#top mid corner recursion
89+
jerusalem_cross((top_left[0]+length*(2**0.5-1),top_left[1]),
90+
(top_right[0]-length*(2**0.5-1),top_left[1]),
91+
(top_left[0]+length*(2**0.5-1),
92+
top_left[1]-length*(2**0.5-1)**2),
93+
(top_right[0]-length*(2**0.5-1),
94+
top_left[1]-length*(2**0.5-1)**2),n-2)
95+
96+
#bottom mid corner recursion
97+
jerusalem_cross((bottom_left[0]+length*(2**0.5-1),
98+
bottom_left[1]+length*(2**0.5-1)**2),
99+
(bottom_right[0]-length*(2**0.5-1),
100+
bottom_left[1]+length*(2**0.5-1)**2),
101+
(bottom_left[0]+length*(2**0.5-1),
102+
bottom_left[1]),
103+
(bottom_right[0]-length*(2**0.5-1),
104+
bottom_left[1]),n-2)
105+
106+
#left mid corner recursion
107+
jerusalem_cross((bottom_left[0],
108+
top_left[1]-length*(2**0.5-1)),
109+
(bottom_left[0]+length*(2**0.5-1)**2,
110+
top_left[1]-length*(2**0.5-1)),
111+
(bottom_left[0],bottom_left[1]+length*(2**0.5-1)),
112+
(bottom_left[0]+length*(2**0.5-1)**2,
113+
bottom_left[1]+length*(2**0.5-1)),n-2)
114+
115+
#right mid corner recursion
116+
jerusalem_cross((bottom_right[0]-length*(2**0.5-1)**2,
117+
top_right[1]-length*(2**0.5-1)),
118+
(bottom_right[0],
119+
top_right[1]-length*(2**0.5-1)),
120+
(bottom_right[0]-length*(2**0.5-1)**2,
121+
bottom_right[1]+length*(2**0.5-1)),
122+
(bottom_right[0],bottom_right[1]+length*(2**0.5-1)),
123+
n-2)
124+
125+
126+
# In[4]:
127+
128+
129+
#initialize
130+
top_left=(0,0)
131+
top_right=(1,0)
132+
bottom_left=(0,-1)
133+
bottom_right=(1,-1)
134+
n=5
135+
136+
137+
# In[5]:
138+
139+
140+
#viz
141+
ax=plt.figure(figsize=(10,10))
142+
jerusalem_cross(top_left,top_right,bottom_left,bottom_right,n)
143+
plt.xlim(top_left[0],top_right[0])
144+
plt.ylim(bottom_right[1],top_left[1],)
145+
plt.axis('off')
146+
plt.show()
147+

0 commit comments

Comments
 (0)