Fractal tree: Difference between revisions

PL/pgSQL fractal tree code
(PL/pgSQL fractal tree code)
Line 2,429:
{{out}}
[https://commons.wikimedia.org/wiki/File:Fractal-tree.png]
 
=={{header|PL/pgSQL}}==
{{works with|Postgres|11}}
 
This piece of code generates the coordinates of each branch, builds a version in the standardized geometry representation format: WKT.
 
A temporary table contains the results: coordinates and WKT representation of each branch.
 
In a query (Postgres + postgis function), we can draw a unique geometry that can be displayed in a tool like QGis or DBeaver database manager for example.
 
The query exploits the notion of CTE and its recursive form.
 
[[File:plpgsql-tree.png||200px|thumb|rigth|Pl/PgSQL fractal tree]]
 
<syntaxhighlight lang="sql">
drop table if exists my_temp_tree_table;
 
do $$
declare
_length numeric := 1;
_random_length_reduction_max numeric := 0.6;
_fork_angle numeric := pi()/12;
_random_angle numeric := pi()/12;
_depth numeric := 9 ;
 
begin
create temporary table my_temp_tree_table as
WITH RECURSIVE branch(azimuth, x1, y1, x2, y2, len, n) AS (
VALUES (pi()/2, 0.0, 0.0, 0.0, _length, _length, _depth)
UNION all
select azimuth+a,
x2, y2,
round((x2+cos(azimuth+a)*len)::numeric, 2), round((y2+sin(azimuth+a)*len)::numeric, 2),
(len*(_random_length_reduction_max+(random()*(1-_random_length_reduction_max))))::numeric,
n-1
FROM branch
cross join (
select ((-_fork_angle)+(_random_angle)*(random()-0.5)) a
union
select ((_fork_angle)+(_random_angle)*(random()-0.5)) a2
) a
WHERE n > 0
)
select x1, y1, x2, y2, 'LINESTRING('||x1||' '||y1||','||x2||' '||y2||')' as wkt from branch
;
end $$
;
 
-- coordinates and WKT
select * from my_temp_tree_table;
 
-- binary version (postgis) of each branch
select ST_GeomFromEWKT('SRID=4326;'||wkt) geom from my_temp_tree_table;
 
-- a unique geometry
select st_union(ST_GeomFromEWKT('SRID=4326;'||wkt)) geom from my_temp_tree_table;
</syntaxhighlight>
{{out}}
coordinates and WKT
<pre>
x1 |y1 |x2 |y2 |wkt |
-----+----+-----+----+---------------------------------+
0.0| 0.0| 0.0| 1|LINESTRING(0.0 0.0,0.0 1) |
0.0| 1| 0.15|1.99|LINESTRING(0.0 1,0.15 1.99) |
0.0| 1|-0.29|1.96|LINESTRING(0.0 1,-0.29 1.96) |
0.15|1.99| 0.36|2.68|LINESTRING(0.15 1.99,0.36 2.68) |
0.15|1.99| 0.05|2.70|LINESTRING(0.15 1.99,0.05 2.70) |
...
 
</pre>
 
=={{header|PostScript}}==
9

edits