]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/blob - source/posts/Fibonacci_golden_spiral.org
c4eeed8b9f9d2adeac9ebbfb9685a0ea21a47d00
[www2.vanrenterghem.biz.git] / source / posts / Fibonacci_golden_spiral.org
1 #+TITLE: Creating a golden spiral in R\r
2 #+AUTHOR: Frederik Vanrenterghem\r
3 #+LANGUAGE: en\r
4 #+PROPERTY: session *R* \r
5 #+PROPERTY: cache yes \r
6 #+PROPERTY: results graphics \r
7 #+PROPERTY: exports both \r
8 #+PROPERTY: tangle yes \r
9 #+OPTIONS: toc:nil  \r
10 #+date: <2019-09-16 22:03:03>\r
11 \r
12 * What \r
13 After having read the first part of a Rcpp tutorial which compared native R vs C++ implementations of a Fibonacci sequence generator, I resorted to drawing the so-called Golden Spiral using R.\r
14 \r
15 * Details\r
16 Libraries used in this example are the following\r
17 \r
18 #+BEGIN_SRC R :session R\r
19 library(ggplot2)\r
20 library(plotrix)\r
21 #+END_SRC\r
22 \r
23 #+RESULTS:\r
24 | plotrix   |\r
25 | ggplot2   |\r
26 | stats     |\r
27 | graphics  |\r
28 | grDevices |\r
29 | utils     |\r
30 | datasets  |\r
31 | methods   |\r
32 | base      |\r
33 \r
34 In polar coordinates, this special instance of a logarithmic spiral's functional representation can be simplified to r(t) = e^(0.0635*t)\r
35 For every quarter turn, the corresponding point on the spiral is a factor of phi further from the origin (r is this distance), with phi the golden ratio - the same one obtained from dividing any 2 sufficiently big successive numbers on a Fibonacci sequence, which is how the golden ratio, the golden spiral and Fibonacci sequences are linked concepts!\r
36 #+BEGIN_SRC R :session R\r
37 polar_golden_spiral <- function(theta) exp(0.30635*theta)\r
38 #+END_SRC\r
39 \r
40 #+RESULTS:\r
41 \r
42 Let's do 2 full circles. First, I create a sequence of angle values theta. Since 2 * PI is the equivalent of a circle in polar coordinates, we need to have distances from origin for values between 0 and 4 * PI.\r
43 #+BEGIN_SRC R :session R\r
44 seq_theta <- seq(0,4*pi,by=0.05)\r
45 \r
46 dist_from_origin <- sapply(seq_theta,polar_golden_spiral)\r
47 #+END_SRC\r
48 \r
49 #+RESULTS:\r
50 |                1 |\r
51 | 1.01543541418402 |\r
52 | 1.03110908037907 |\r
53 | 1.04702467610363 |\r
54 | 1.06318593564018 |\r
55 | 1.07959665091141 |\r
56 | 1.09626067236991 |\r
57 | 1.11318190990159 |\r
58 | 1.13036433374308 |\r
59 | 1.14781197541325 |\r
60 | 1.16552892865913 |\r
61 | 1.18351935041644 |\r
62 | 1.20178746178492 |\r
63 | 1.22033754901874 |\r
64 | 1.23917396453215 |\r
65 | 1.25830112792076 |\r
66 | 1.27772352699844 |\r
67 | 1.29744571885033 |\r
68 | 1.31747233090207 |\r
69 | 1.33780806200553 |\r
70 | 1.35845768354131 |\r
71 | 1.37942604053823 |\r
72 | 1.40071805281016 |\r
73 | 1.42233871611032 |\r
74 | 1.44429310330345 |\r
75 | 1.46658636555607 |\r
76 | 1.48922373354506 |\r
77 |   1.512210518685 |\r
78 | 1.53555211437434 |\r
79 | 1.55925399726085 |\r
80 | 1.58332172852666 |\r
81 | 1.60776095519303 |\r
82 | 1.63257741144533 |\r
83 | 1.65777691997847 |\r
84 | 1.68336539336305 |\r
85 | 1.70934883543265 |\r
86 | 1.73573334269253 |\r
87 |    1.76252510575 |\r
88 | 1.78973041076699 |\r
89 | 1.81735564093491 |\r
90 | 1.84540727797241 |\r
91 | 1.87389190364612 |\r
92 | 1.90281620131498 |\r
93 | 1.93218695749834 |\r
94 | 1.96201106346829 |\r
95 | 1.99229551686655 |\r
96 | 2.02304742334636 |\r
97 | 2.05427399823962 |\r
98 | 2.08598256824992 |\r
99 |  2.1181805731715 |\r
100 | 2.15087556763495 |\r
101 | 2.18407522287968 |\r
102 | 2.21778732855389 |\r
103 | 2.25201979454219 |\r
104 | 2.28678065282156 |\r
105 | 2.32207805934587 |\r
106 |  2.3579202959595 |\r
107 | 2.39431577234054 |\r
108 | 2.43127302797395 |\r
109 | 2.46880073415517 |\r
110 | 2.50690769602466 |\r
111 | 2.54560285463391 |\r
112 | 2.58489528904321 |\r
113 | 2.62479421845192 |\r
114 | 2.66530900436155 |\r
115 | 2.70644915277227 |\r
116 |  2.7482243164133 |\r
117 | 2.79064429700773 |\r
118 | 2.83371904757232 |\r
119 | 2.87745867475275 |\r
120 | 2.92187344119496 |\r
121 |  2.9669737679531 |\r
122 | 3.01277023693458 |\r
123 | 3.05927359338295 |\r
124 | 3.10649474839905 |\r
125 | 3.15444478150108 |\r
126 | 3.20313494322417 |\r
127 | 3.25257665776014 |\r
128 | 3.30278152563795 |\r
129 |  3.3537613264455 |\r
130 | 3.40552802159354 |\r
131 | 3.45809375712212 |\r
132 | 3.51147086655048 |\r
133 | 3.56567187377081 |\r
134 | 3.62070949598677 |\r
135 | 3.67659664669734 |\r
136 |  3.7333464387267 |\r
137 | 3.79097218730088 |\r
138 | 3.84948741317197 |\r
139 | 3.90890584579046 |\r
140 | 3.96924142652657 |\r
141 | 4.03050831194138 |\r
142 | 4.09272087710833 |\r
143 | 4.15589371898609 |\r
144 | 4.22004165984341 |\r
145 | 4.28517975073691 |\r
146 | 4.35132327504251 |\r
147 | 4.41848775204137 |\r
148 | 4.48668894056115 |\r
149 | 4.55594284267357 |\r
150 | 4.62626570744896 |\r
151 | 4.69767403476877 |\r
152 | 4.77018457919694 |\r
153 | 4.84381435391107 |\r
154 | 4.91858063469419 |\r
155 |  4.9945009639882 |\r
156 | 5.07159315500985 |\r
157 | 5.14987529593027 |\r
158 | 5.22936575411901 |\r
159 | 5.31008318045357 |\r
160 | 5.39204651369547 |\r
161 | 5.47527498493387 |\r
162 | 5.55978812209773 |\r
163 |  5.6456057545377 |\r
164 | 5.73274801767868 |\r
165 | 5.82123535774417 |\r
166 | 5.91108853655362 |\r
167 | 6.00232863639374 |\r
168 | 6.09497706496509 |\r
169 | 6.18905556040493 |\r
170 | 6.28458619638769 |\r
171 | 6.38159138730412 |\r
172 | 6.48009389352033 |\r
173 | 6.58011682671816 |\r
174 |  6.6816836553178 |\r
175 | 6.78481820998423 |\r
176 | 6.88954468921862 |\r
177 | 6.99588766503603 |\r
178 | 7.10387208873074 |\r
179 |  7.2135232967306 |\r
180 | 7.32486701654172 |\r
181 | 7.43792937278492 |\r
182 | 7.55273689332534 |\r
183 | 7.66931651549675 |\r
184 | 7.78769559242179 |\r
185 | 7.90790189942989 |\r
186 |  8.0299636405742 |\r
187 | 8.15390945524908 |\r
188 | 8.27976842490986 |\r
189 | 8.40757007989611 |\r
190 | 8.53734440636049 |\r
191 |  8.6691218533043 |\r
192 | 8.80293333972179 |\r
193 | 8.93881026185472 |\r
194 | 9.07678450055882 |\r
195 | 9.21688842878404 |\r
196 | 9.35915491917023 |\r
197 | 9.50361735176004 |\r
198 |  9.6503096218309 |\r
199 | 9.79926614784789 |\r
200 | 9.95052187953938 |\r
201 | 10.1041123060972 |\r
202 | 10.2600734645037 |\r
203 | 10.4184419479868 |\r
204 | 10.5792549146061 |\r
205 | 10.7425500959714 |\r
206 | 10.9083658060953 |\r
207 | 11.0767409503832 |\r
208 | 11.2477150347615 |\r
209 | 11.4213281749469 |\r
210 | 11.5976211058588 |\r
211 | 11.7766351911771 |\r
212 |  11.958412433047 |\r
213 | 12.1429954819344 |\r
214 | 12.3304276466328 |\r
215 | 12.5207529044246 |\r
216 | 12.7140159114002 |\r
217 | 12.9102620129349 |\r
218 | 13.1095372543288 |\r
219 | 13.3118883916102 |\r
220 | 13.5173629025061 |\r
221 |  13.726008997582 |\r
222 | 13.9378756315533 |\r
223 | 14.1530125147717 |\r
224 | 14.3714701248888 |\r
225 | 14.5932997186998 |\r
226 | 14.8185533441694 |\r
227 | 15.0472838526447 |\r
228 | 15.2795449112548 |\r
229 | 15.5153910155034 |\r
230 | 15.7548775020547 |\r
231 | 15.9980605617174 |\r
232 | 16.2449972526286 |\r
233 | 16.4957455136412 |\r
234 | 16.7503641779184 |\r
235 | 17.0089129867378 |\r
236 |  17.271452603508 |\r
237 | 17.5380446280028 |\r
238 | 17.8087516108139 |\r
239 | 18.0836370680272 |\r
240 | 18.3627654961257 |\r
241 | 18.6462023871224 |\r
242 | 18.9340142439267 |\r
243 | 19.2262685959479 |\r
244 | 19.5230340149396 |\r
245 | 19.8243801310889 |\r
246 | 20.1303776493537 |\r
247 | 20.4410983660522 |\r
248 | 20.7566151857085 |\r
249 | 21.0770021381583 |\r
250 | 21.4023343959182 |\r
251 | 21.7326882918241 |\r
252 | 22.0681413369407 |\r
253 | 22.4087722387478 |\r
254 | 22.7546609196083 |\r
255 | 23.1058885355194 |\r
256 |  23.462537495155 |\r
257 | 23.8246914792008 |\r
258 | 24.1924354599887 |\r
259 | 24.5658557214339 |\r
260 | 24.9450398792791 |\r
261 | 25.3300769016527 |\r
262 | 25.7210571299428 |\r
263 | 26.1180722999943 |\r
264 | 26.5212155636329 |\r
265 | 26.9305815105213 |\r
266 | 27.3462661903527 |\r
267 | 27.7683671353873 |\r
268 | 28.1969833833359 |\r
269 | 28.6322155005976 |\r
270 | 29.0741656058555 |\r
271 | 29.5229373940367 |\r
272 | 29.9786361606425 |\r
273 | 30.4413688264541 |\r
274 |  30.911243962619 |\r
275 | 31.3883718161253 |\r
276 | 31.8728643356692 |\r
277 | 32.3648351979214 |\r
278 | 32.8643998341988 |\r
279 |  33.371675457549 |\r
280 | 33.8867810902509 |\r
281 | 34.4098375917422 |\r
282 | 34.9409676869756 |\r
283 | 35.4802959952146 |\r
284 | 36.0279490592724 |\r
285 |  36.584055375203 |\r
286 | 37.1487454224504 |\r
287 | 37.7221516944627 |\r
288 | 38.3044087297792 |\r
289 | 38.8956531435973 |\r
290 | 39.4960236598267 |\r
291 |  40.105661143638 |\r
292 | 40.7247086345141 |\r
293 | 41.3533113798114 |\r
294 | 41.9916168688395 |\r
295 | 42.6397748674667 |\r
296 | 43.2979374532595 |\r
297 | 43.9662590511644 |\r
298 |  44.644896469741 |\r
299 | 45.3340089379542 |\r
300 | 46.0337581425336 |\r
301 | 46.7443082659106 |\r
302 \r
303 Plotting the function using coord_polar in ggplot2 does not work as intended. Unexpectedly, the x axis keeps extending instead of circling back once a full circle is reached. Turns out coord_polar might not really be intended to plot elements in polar vector format.\r
304 #+BEGIN_SRC R :session R :results output graphics :file ../pics/golden_spiral-coord_polar-fail.png :exports both\r
305 ggplot(data.frame(x = seq_theta, y = dist_from_origin), aes(x,y)) +\r
306     geom_point() +\r
307     coord_polar(theta="x")\r
308 #+END_SRC\r
309 \r
310 #+RESULTS:\r
311 [[file:../pics/golden_spiral-coord_polar-fail.png]]\r
312 \r
313 To ensure what I was trying to do is possible, I employ a specialised plotfunction instead\r
314 #+BEGIN_SRC R :session R :results output graphics :file ../pics/golden_spiral-plotrix.png :exports both\r
315 plotrix::radial.plot(dist_from_origin, seq_theta,rp.type="s", point.col = "blue")\r
316 #+END_SRC\r
317 \r
318 With that established and the original objective of the exercise achieved, it still would be nice to be able to accomplish this using ggplot2. To do so, the created sequence above needs to be converted to cartesian coordinates.\r
319 The rectangular function equivalent of the golden spiral function r(t) defined above is a(t) = (r(t) cos(t), r(t) sin(t))\r
320 It's not too hard to come up with a hack to convert one to the other.\r
321 #+BEGIN_SRC R :session R\r
322 cartesian_golden_spiral <- function(theta) {\r
323     a <- polar_golden_spiral(theta)*cos(theta)\r
324     b <- polar_golden_spiral(theta)*sin(theta)\r
325     c(a,b)\r
326 }\r
327 #+END_SRC\r
328 \r
329 #+RESULTS:\r
330 \r
331 Applying that function to the same series of angles from above and stitching the resulting coordinates in a data frame. Note I'm enclosing the first expression in brackets, which prints it immediately, which is useful when the script is run interactively.\r
332 #+BEGIN_SRC R :session R :exports code\r
333 (serie <- sapply(seq_theta,cartesian_golden_spiral))\r
334 df <- data.frame(t(serie))\r
335 #+END_SRC\r
336 \r
337 #+RESULTS:\r
338 : TRUE\r
339 \r
340 * Result\r
341 With everything now ready in the right coordinate system, it's now only a matter of setting some options to make the output look acceptable.\r
342 #+BEGIN_SRC R :session R :results output graphics :file ../pics/golden_spiral-ggplot-coord-fixed.png :width 800 :height 800 :exports both\r
343 ggplot(df, aes(x=X1,y=X2)) +\r
344     geom_path(color="blue") +\r
345     theme(panel.grid.minor = element_blank(),\r
346           axis.text.x = element_blank(),\r
347           axis.text.y = element_blank()) +\r
348     scale_y_continuous(breaks = seq(-20,20,by=10)) +\r
349     scale_x_continuous(breaks = seq(-20,50,by=10)) +\r
350     coord_fixed() +\r
351     labs(title = "Golden spiral",\r
352          subtitle = "Another view on the Fibonacci sequence",\r
353          caption = "Maths from https://www.intmath.com/blog/mathematics/golden-spiral-6512\nCode errors mine.",\r
354          x = "",\r
355          y = "")\r
356 #+END_SRC\r
357 \r
358 * Note on how this post was written.\r
359 After a long hiatus, I set about using emacs, org-mode and ESS together to create this post. All code is part of an .org file, and gets exported to markdown using the orgmode conversion - C-c C-e m m.\r