Last active
October 29, 2020 14:58
-
-
Save youz/e67acc1d51f759121931e7872560e8cf to your computer and use it in GitHub Desktop.
Penrose tiling in SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- requires SQLite ver.3.34.0 or later | |
| -- ref. How to construct Penrose tilings | |
| -- https://tartarus.org/~simon/20110412-penrose/penrose.xhtml | |
| WITH consts AS ( | |
| SELECT | |
| 8 AS n, -- number of iterations | |
| 500 AS l, -- width & height of image | |
| 0.7265425280053609 AS tan36, -- tan(π/5) | |
| 0.6180339887498948 AS d -- 1/φ | |
| ), tiles (i, t, px, py, qx, qy, rx, ry) AS ( | |
| SELECT n, 1, -l/tan36, 0, 0, l, l/tan36, 0 FROM consts | |
| UNION ALL | |
| SELECT i-1, 0, rx, ry, px*(1-d)+qx*d, py*(1-d)+qy*d, qx, qy | |
| FROM tiles, consts WHERE t = 0 and i > 0 | |
| UNION ALL | |
| SELECT i-1, 1, rx, ry, px*(1-d)+qx*d, py*(1-d)+qy*d, px, py | |
| FROM tiles, consts WHERE t = 0 and i > 0 | |
| UNION ALL | |
| SELECT i-1, 1, px*(1-d)+rx*d, py*(1-d)+ry*d, px*(1-d)+qx*d, py*(1-d)+qy*d, px, py | |
| FROM tiles, consts WHERE t = 1 and i > 0 | |
| UNION ALL | |
| SELECT i-1, 0, px*(1-d)+rx*d, py*(1-d)+ry*d, px*(1-d)+qx*d, py*(1-d)+qy*d, qx, qy | |
| FROM tiles, consts WHERE t = 1 and i > 0 | |
| UNION ALL | |
| SELECT i-1, 1, rx, ry, px*(1-d)+rx*d, py*(1-d)+ry*d, qx, qy | |
| FROM tiles, consts WHERE t = 1 and i > 0 | |
| ) | |
| SELECT printf('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="%d" height="%d" viewBox="%d %d %d %d"><g id="t">', | |
| l, l, -l/2, -l/2, l, l) | |
| FROM consts | |
| UNION ALL SELECT | |
| printf('<g stroke="#044a64" stroke-width="0.3" stroke-linejoin="bevel" fill="%s">%s</g>', | |
| CASE t WHEN 0 THEN '#044a64' ELSE '#fff' END, | |
| group_concat(printf('<path d="M%f %fL%f %fL%f %fz"/>', px, py, qx, qy, rx, ry), char(10)) | |
| ) | |
| FROM tiles WHERE i = 0 GROUP BY t | |
| UNION ALL SELECT '</g><use xlink:href="#t" transform="scale(1 -1)"/></svg>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment