-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjerusalem cross.py
147 lines (115 loc) · 5.48 KB
/
jerusalem cross.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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!
import matplotlib.pyplot as plt
# In[2]:
#compute euclidean distance
def euclidean_distance(point1,point2):
return ((point1[0]-point2[0])**2+(point1[1]-point2[1])**2)**0.5
# In[3]:
#recursively plot jerusalem cross
#it kinda looks like flag of georgia (n=2)
#i mean the eurasian country not a yankee state
#i call it jerusalem cross but it is aka cross menger square,jerusalem square
#it is a 2d version of jerusalem cube
#a good reference to jerusalem cube
# https://robertdickau.com/jerusalemcube.html
#a good understanding of sierpiński carpet is helpful as well
# https://github.com/je-suis-tm/recursion-and-dynamic-programming/blob/master/sierpi%C5%84ski%20carpet.py
#do not confuse it with quadratic cross,which creates new crosses from the tips
# https://onlinemathtools.com/generate-quadratic-cross-fractal
#or fibonacci snowflakes,which is more like koch snowflake
# http://www.slabbe.org/Publications/2011-fibo-snowflakes.pdf
#or vicsek fractal,which is more similar to crosslet cross
# https://en.wikipedia.org/wiki/Vicsek_fractal
def jerusalem_cross(top_left,top_right,bottom_left,bottom_right,n):
if n<=0:
return
else:
#compute the length
length=euclidean_distance(top_left,top_right)
#create the cross
plt.fill_between(
[top_left[0]+length*(2**0.5-1),top_right[0]-length*(2**0.5-1)],
[bottom_left[1]+length*(2**0.5-1)**2,bottom_left[1]+length*(2**0.5-1)**2],
[top_left[1]-length*(2**0.5-1)**2,top_left[1]-length*(2**0.5-1)**2],
color='k')
plt.fill_between(
[top_left[0]+length*(2**0.5-1)**2,top_right[0]-length*(2**0.5-1)**2],
[bottom_left[1]+length*(2**0.5-1),bottom_left[1]+length*(2**0.5-1)],
[top_left[1]-length*(2**0.5-1),top_left[1]-length*(2**0.5-1)],
color='k')
#top left corner recursion
jerusalem_cross(top_left,(top_left[0]+length*(2**0.5-1),top_left[1]),
(top_left[0],top_left[1]-length*(2**0.5-1)),
(top_left[0]+length*(2**0.5-1),
top_left[1]-length*(2**0.5-1)),n-1)
#top right corner recursion
jerusalem_cross((top_right[0]-length*(2**0.5-1),top_left[1]),top_right,
(top_right[0]-length*(2**0.5-1),
top_left[1]-length*(2**0.5-1)),
(top_right[0],top_left[1]-length*(2**0.5-1)),n-1)
#bottom left corner recursion
jerusalem_cross((bottom_left[0],bottom_left[1]+length*(2**0.5-1)),
(bottom_left[0]+length*(2**0.5-1),
bottom_left[1]+length*(2**0.5-1)),
bottom_left,
(bottom_left[0]+length*(2**0.5-1),bottom_left[1]),n-1)
#bottom right corner recursion
jerusalem_cross((bottom_right[0]-length*(2**0.5-1),
bottom_right[1]+length*(2**0.5-1)),
(bottom_right[0],
bottom_right[1]+length*(2**0.5-1)),
(bottom_right[0]-length*(2**0.5-1),
bottom_right[1]),
bottom_right,n-1)
#top mid corner recursion
jerusalem_cross((top_left[0]+length*(2**0.5-1),top_left[1]),
(top_right[0]-length*(2**0.5-1),top_left[1]),
(top_left[0]+length*(2**0.5-1),
top_left[1]-length*(2**0.5-1)**2),
(top_right[0]-length*(2**0.5-1),
top_left[1]-length*(2**0.5-1)**2),n-2)
#bottom mid corner recursion
jerusalem_cross((bottom_left[0]+length*(2**0.5-1),
bottom_left[1]+length*(2**0.5-1)**2),
(bottom_right[0]-length*(2**0.5-1),
bottom_left[1]+length*(2**0.5-1)**2),
(bottom_left[0]+length*(2**0.5-1),
bottom_left[1]),
(bottom_right[0]-length*(2**0.5-1),
bottom_left[1]),n-2)
#left mid corner recursion
jerusalem_cross((bottom_left[0],
top_left[1]-length*(2**0.5-1)),
(bottom_left[0]+length*(2**0.5-1)**2,
top_left[1]-length*(2**0.5-1)),
(bottom_left[0],bottom_left[1]+length*(2**0.5-1)),
(bottom_left[0]+length*(2**0.5-1)**2,
bottom_left[1]+length*(2**0.5-1)),n-2)
#right mid corner recursion
jerusalem_cross((bottom_right[0]-length*(2**0.5-1)**2,
top_right[1]-length*(2**0.5-1)),
(bottom_right[0],
top_right[1]-length*(2**0.5-1)),
(bottom_right[0]-length*(2**0.5-1)**2,
bottom_right[1]+length*(2**0.5-1)),
(bottom_right[0],bottom_right[1]+length*(2**0.5-1)),
n-2)
# In[4]:
#initialize
top_left=(0,0)
top_right=(1,0)
bottom_left=(0,-1)
bottom_right=(1,-1)
n=5
# In[5]:
#viz
ax=plt.figure(figsize=(10,10))
jerusalem_cross(top_left,top_right,bottom_left,bottom_right,n)
plt.xlim(top_left[0],top_right[0])
plt.ylim(bottom_right[1],top_left[1],)
plt.axis('off')
plt.show()