|
| 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 | + |
0 commit comments