Last active
January 9, 2024 04:55
-
-
Save youz/e9c12701c59e58d0803b98b85cd612a1 to your computer and use it in GitHub Desktop.
drawing dragon curve 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 version 3.44.0 or higher** | |
make db: | |
$ sqlite3 dc1s.db < dragon-curve-one-stroke.sql | |
usage: | |
$ echo 'update param set n=15; select * from dragon_curve;' | sqlite3 dc1s.db > dc1s.svg | |
*/ | |
create table param as select 12 as n; | |
create view dragon_curve as | |
with r(i, o, sx, sy, ex, ey) as ( | |
select n, 0, 0.0, 0.0, 1.0, 0.0 from param | |
union all select i-1, o*2, sx, sy, (sx+ex+(sy-ey)*(1-o%2*2))/2, ((ex-sx)*(1-o%2*2)+sy+ey)/2 from r where i > 0 | |
union all select i-1, o*2+1, (sx+ex+(sy-ey)*(1-o%2*2))/2, ((ex-sx)*(1-o%2*2)+sy+ey)/2, ex, ey from r where i > 0 | |
) | |
select | |
'<svg xmlns="http://www.w3.org/2000/svg" width="960" height="640" viewBox="-100 -100 450 300">' | |
|| '<path stroke="#093" stroke-width="0.25" fill="none" d="M 0 0' | |
|| group_concat(printf(' L %s %s', ex*256, ey*256), '' order by o) | |
|| '"/></svg>' | |
from r where i = 0; |
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
/* | |
make db: | |
$ sqlite3 dc.db < dragon-curve.sql | |
usage: | |
$ echo 'update param set n=15; select * from dragon_curve' | sqlite3 dc.db > dc.svg | |
*/ | |
create table param as select 12 as n; | |
create view dragon_curve as | |
with r(i, sx, sy, ex, ey) as ( | |
select n, 0.0, 0.0, 1.0, 0.0 from param | |
union all select i-1, (sx-sy)/2, (sx+sy)/2, (ex-ey)/2, (ex+ey)/2 from r where i > 0 | |
union all select i-1, 1-(sx+sy)/2, (sx-sy)/2, 1-(ex+ey)/2, (ex-ey)/2 from r where i > 0 | |
) | |
select '<svg xmlns="http://www.w3.org/2000/svg" width="960" height="640" viewBox="-100 -100 450 300">' | |
union all select '<g stroke="#093" stroke-width="0.25">' | |
union all select printf('<path d="M %s %s L %s %s"/>', sx*256, sy*256, ex*256, ey*256) | |
from r where i = 0 | |
union all select '</g></svg>'; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment