Skip to content

Instantly share code, notes, and snippets.

@youz
Last active September 30, 2021 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youz/1dfa69cd54c1aeb2eb6f90f535db540e to your computer and use it in GitHub Desktop.
Save youz/1dfa69cd54c1aeb2eb6f90f535db540e to your computer and use it in GitHub Desktop.
penrose tiling in SQL
-- 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/φ
), gen(p, i, t, px, py, qx, qy, rx, ry, sx, sy, tx, ty) AS (
SELECT 0, n, 1, -l/tan36, 0, 0, l, l/tan36, 0, 0, 0, 0, 0 FROM consts
UNION ALL SELECT 1, i-1, t, px, py, qx, qy, rx, ry, (qx-px)*d+px, (qy-py)*d+py, (rx-px)*d+px, (ry-py)*d+py
FROM gen, consts WHERE p = 0 and i > 0
UNION ALL SELECT 0, i, 0, rx, ry, sx, sy, qx, qy, 0, 0, 0, 0 FROM gen WHERE p = 1 and t = 0
UNION ALL SELECT 0, i, 1, rx, ry, sx, sy, px, py, 0, 0, 0, 0 FROM gen WHERE p = 1 and t = 0
UNION ALL SELECT 0, i, 1, tx, ty, sx, sy, px, py, 0, 0, 0, 0 FROM gen WHERE p = 1 and t = 1
UNION ALL SELECT 0, i, 0, tx, ty, sx, sy, qx, qy, 0, 0, 0, 0 FROM gen WHERE p = 1 and t = 1
UNION ALL SELECT 0, i, 1, rx, ry, tx, ty, qx, qy, 0, 0, 0, 0 FROM gen WHERE p = 1 and t = 1
), tiles(color, paths) AS (
SELECT
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 gen WHERE p = 0 and i = 0 GROUP BY t
)
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) AS "<!--Penrose tiling-->"
FROM consts
UNION ALL SELECT printf('<g stroke="#044a64" stroke-width="0.3" stroke-linejoin="bevel" fill="%s">%s</g>', color, paths)
FROM tiles
UNION ALL SELECT '</g><use xlink:href="#t" transform="scale(1 -1)"/></svg>';
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment