Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Christmas Tree Generator
WITH params AS( SELECT --- tree params 30 as _p_halfwidth, -- max half width of the tree 4 as _p_height, -- heigt of single tree block 5 as _p_num_blocks, -- number of tree blocks 1.1 as _p_scale_inter, -- inter scale of block widening 1.15 as _p_scale_outer, --- outer scale of blocks ---- the look of tree '.'::text as _p_tree_char, 'X'::text as _p_star_char, 'x'::text as _p_chain_char, 'O'::text as _p_decor1_char, '8'::text as _p_decor2_char, --- density and location of decors : 3 as _p_chain_start, 0 as _p_decor1_start, 0 as _p_decor2_start, 2 as _p_chain_helicity, 1 as _p_decor1_helicity, 1 as _p_decor2_helicity, 8 as _p_chain_step, 5 as _p_decor1_step, 7 as _p_decor2_step, 0.99 as _p_chain_prob, -- chain sometimes ends... 0.1 as _p_decor1_prob, 0.1 as _p_decor2_prob ), christmas_tree as ( ---- build a table of float numbers b in blocks_ WITH RECURSIVE tree(b, pos,l, lv) AS ( (WITH RECURSIVE t(b,pos, l) AS ( SELECT 1 -(abs(generate_series(-_p_halfwidth, _p_halfwidth, 1)))::float8 /(_p_halfwidth) b, -- to draw tree generate_series (0, 2*_p_halfwidth, 1) as pos, -- to find positions of decorations 0 as l from params UNION ALL SELECT (b *_p_scale_inter), pos, l + 1 FROM t,params WHERE l < _p_height-1 ) select b, pos, l, 1 AS lv from t order by l asc ) UNION ALL SELECT b * _p_scale_outer, pos, l + (lv * (2*_p_halfwidth+1)) AS l, lv + 1 FROM tree,params WHERE lv < _p_num_blocks ) select l,array_to_string(array_agg( (CASE WHEN b>=1 THEN CASE WHEN l=0 THEN _p_star_char ELSE CASE WHEN mod(pos+_p_chain_helicity*l + _p_chain_start,_p_chain_step)=0 and random()<_p_chain_prob THEN _p_chain_char WHEN mod(pos+_p_decor1_helicity*l + _p_decor1_start,_p_decor1_step)=0 and random()<_p_decor1_prob THEN _p_decor1_char WHEN mod(pos+_p_decor2_helicity*l + _p_decor2_start,_p_decor2_step)=0 and random()<_p_decor2_prob THEN _p_decor2_char ELSE _p_tree_char END END ELSE ' ' END)::text), '' ) AS "Merry Christmas" from tree,params group by l order by l asc ) select "Merry Christmas" from christmas_tree order by l
run
|
edit
|
history
|
help
0
SQL - Social Network Analysis by Patrick Lin
Projeto
a
PostgreSQL Sandbox: General Resale
conditionally add “another row” in SQL Server?
SQL primes, sqrt sieve
SQL Practice Sample: Movie Database
Postgres CrossTab for query with variable number of columns
filme
extract_pairs