Drawing the Peano curve
GFA Basic 3.51 (Atari ST), 156 134 124 bytes
A manually edited listing in .LST format. All lines end with CR
, including the last one.
PRO f(n)
DR "MA0,199"
p(n,90)
RET
PRO p(n,a)
I n
n=n-.5
DR "RT",a
p(n,-a)
DR "FD4"
p(n,a)
DR "FD4"
p(n,-a)
DR "LT",a
EN
RET
Expanded and commented
PROCEDURE f(n) ! main procedure, taking the number 'n' of iterations
DRAW "MA0,199" ! move the pen to absolute position (0, 199)
p(n,90) ! initial call to 'p' with 'a' = +90
RETURN ! end of procedure
PROCEDURE p(n,a) ! recursive procedure taking 'n' and the angle 'a'
IF n ! if 'n' is not equal to 0:
n=n-0.5 ! subtract 0.5 from 'n'
DRAW "RT",a ! right turn of 'a' degrees
p(n,-a) ! recursive call with '-a'
DRAW "FD4" ! move the pen 4 pixels forward
p(n,a) ! recursive call with 'a'
DRAW "FD4" ! move the pen 4 pixels forward
p(n,-a) ! recursive call with '-a'
DRAW "LT",a ! left turn of 'a' degrees
ENDIF ! end
RETURN ! end of procedure
Example output
Perl 6, 117 bytes
{map ->\y{|map {(((++$+y)%2+$++)%3**(y+$^v,*/3...*%3)??$^s[$++%2]!!'│')xx$_*3},<┌ ┐>,$_,<└ ┘>,1},^$_}o*R**3
Try it online!
0-indexed. Returns a 2D array of Unicode characters. The basic idea is that for lower rows, the expression
(x + (x+y)%2) % (3 ** trailing_zeros_in_base3(3*(y+1)))
yields the pattern
|....||....||....||....||.. % 3
..||....||....||....||....| % 3
|................||........ % 9
..||....||....||....||....| % 3
|....||....||....||....||.. % 3
........||................| % 9
|....||....||....||....||.. % 3
..||....||....||....||....| % 3
|.......................... % 27
For upper rows, the expression is
(x + (x+y+1)%2) % (3 ** trailing_zeros_in_base3(3*(y+3**n)))
Explanation
{ ... }o*R**3 # Feed $_ = 3^n into block
map ->\y{ ... },^$_ # Map y = 0..3^n-1
|map { ... },<┌ ┐>,$_,<└ ┘>,1 # Map pairs (('┌','┐'),3^n) for upper rows
# and (('└','┘'),1) for lower rows.
# Block takes items as s and v
( ... )xx$_*3 # Evaluate 3^(n+1) times, returning a list
(++$+y)%2 # (x+y+1)%2 for upper rows, (x+y)%2 for lower rows
( +$++) # Add x
(y+$^v,*/3...*%3) # Count trailing zeros of 3*(y+v) in base 3
3** # nth power of 3
% # Modulo
??$^s[$++%2] # If there's a remainder yield chars in s alternately
!!'│' # otherwise yield '│'
K (ngn/k), 37 27 26 bytes
{+y,(|'y:x,,~>+x),x}/1,&2*
Try it online!
returns a boolean matrix
|'y
is syntax specific to ngn/k. other dialects require a :
to make an each-ed verb monadic: |:'y