Pythagoras tree: Difference between revisions

→‎{{header|J}}: more concise, fewer external dependencies
(Added XPL0 example.)
(→‎{{header|J}}: more concise, fewer external dependencies)
Line 707:
 
=={{header|J}}==
<syntaxhighlight lang="j"J>
Using the bash shell, gnuplot for graphics, with ijconsole installed on the PATH, and having saved the program in the file /tmp/pt.ijs the following command plots the Pythagoras tree:
exPt=: {.{
if. 0 < xm do.
d=. x-y
p2=. x-0j1*d
p3=. y-0j1*d
p4=. (0.5*x+y)-0j1.5*d
pd x,y,p3,p2
p2 (<:m) Pt p4
p4 (<:m) Pt p3
end.
}}
 
<pre>
gnuplot --persist -e 'plot"<ijconsole /tmp/pt.ijs"w l'
</pre>
 
Petri=: {{
<syntaxhighlight lang="j">
pd 'reset'
NB. use on linux: gnuplot --persist -e 'plot"< ijconsole /tmp/pt.ijs"w l'
pd 'type poly'
5r2j4 y Pt 7r2j4
pd 'show'
}}
 
Petri 7</syntaxhighlight>
NB. translated from c
 
[[File:J-pytree.png]]
ex=: {.
why=: {:
but_first=: & NB. just for fun
append=: ,
subtract=: -
 
X=: adverb def ' ex m'
Y=: adverb def 'why m'
 
pt=: dyad define
 
'a b'=. y
 
NB. c.x = b.x - (a.y - b.y);
NB. c.y = b.y - (b.x - a.x);
c=. (b X , a append but_first why b) ,&(-/) (b Y , b ,&ex a)
NB. d.x = a.x - (a.y - b.y);
NB. d.y = a.y - (b.x - a.x);
d=. (a X , a append but_first why b) ,&(-/) (a Y , b ,&ex a)
 
NB. e.x = d.x + ( b.x - a.x - (a.y - b.y) ) / 2;
NB. e.y = d.y - ( b.x - a.x + a.y - b.y ) / 2;
e=. (d X + -: (b -&ex a) - a subtract but_first why b) , d Y - -: -/ b X , a X , a Y , b Y
 
if. 0 < x do.
NB. line(a.x,a.y,b.x,b.y); line(c.x,c.y,b.x,b.y); line(c.x,c.y,d.x,d.y); line(a.x,a.y,d.x,d.y);
echo (a ,: b) , (c ,: b) , (c ,: d) ,: (a ,: d)
echo ''
(<: x) pt"2 (d ,: e) ,: (e ,: c) NB. pythagorasTree(d,e,times-1);pythagorasTree(e,c,times-1);
end.
)
 
 
NB. a.x = 6*side/2 - side/2;
NB. a.y = 4*side;
NB. b.x = 6*side/2 + side/2;
NB. b.y = 4*side;
petri=: 7&$: :(empty@:(pt (x:inv 5r2 7r2 ,. 4)&*))
 
petri 1
exit 0
</syntaxhighlight>
 
=={{header|Java}}==
6,962

edits