Hilbert curve: Difference between revisions

J: implement and show ascii art.
(J: implement and show ascii art.)
Line 1,162:
 
=={{header|J}}==
 
Note: J's {{ }} syntax requires a recent version of J (9.02 or more recent).
 
<lang J>iter=: (, 1 , +@|.) @: (,~ 0j_1 ,~ 0j_1*|.)
hilbert=: 3 : '{{0j1+(%{:) +/\0,iter ^: y '''''}}
asciiart=:{{
coords=. 1 3*"1 +. y % <./(,+.y)-.0
canvas=. (2+>./coords)$' '
pairs=. 2 ]\<.coords
horizontal=. =/"1 {."1 pairs
canvas=. '_' (0 1+"1<./"2 horizontal#pairs)} canvas
canvas=. '_' (0 2+"1<./"2 horizontal#pairs)} canvas
vertical=. -.horizontal
canvas=. '|' (>./"2 vertical#pairs)} canvas
}}
</lang>
 
For a graphical presentation, you could use (for example):
 
<lang J>require'plot'
plot hilbert 5</lang>
 
For asciiart, you could instead use:
iter=: (, 1 , +@|.) @: (,~ 0j_1 ,~ 0j_1*|.)
hilbert=: 3 : '0j1+(%{:) +/\0,iter ^: y '''''
 
plot<lang J> asciiart hilbert 54
__ __ __ __ __ __ __ __ __ __
</lang>
|__ |__| __| |__ |__| __| |__ |__|
__| __ | __ | __ |__ __| __
|__ __| | |__| |__| | |__ __| |__ __| |
__ __ | __ __ |__ __ __ __ __|
|__ |__| | |__| | __| |__ __| |__
__| __ |__ __| | __ | | __ |
|__ __| |__ __| |__ |__| |__| |__| |__|
__ __ __ __ __| __ __ __ __
|__ |__| __| |__ | |__| | | |__| |
__| __ | __ | |__ __| |__ __|
|__ __| | |__| |__| __| |__ __ __| |__
__ __ | __ __ | __ __ __ __ |
|__ |__| | |__| | |__| __| |__ |__|
__| __ |__ __| __ |__ __| __
|__ __| |__ __| |__ __| |__ __| |__ __| | </lang>
 
The idea is to represent the nth order hilbert curve as list of complex numbers that can be summed to trace the curve. The 0th order hilbert curve is an empty stringlist. The first half of the n+1 the curve is formed by rotating the nth right by 90 degrees and reversing, appending -i and appending the nth curve. The whole n+1th curve is the first half appended to 1 appended to the conjugate of the reverse of the first half.
 
=={{header|Java}}==
6,951

edits