# Draw the Sierpinski Arrowhead Curve

## Introduction

The Sierpinski Arrowhead Curve is a curve that's limit is Sierpinski's Triangle.

It first starts like this:

 _
/ \


Then, each line is replaced with a rotated version of the first one:

  _
/ \
\ /
_/ \_


Next:

     _
/ \
\ /
_/ \_
/     \
\_   _/
_  \ /  _
/ \_/ \_/ \


Given a number n, output the n-th iteration of the Sierpinski Arrowhead Curve.

You may generate an image, or use Ascii Art in the format I have given above.

You may not use built-ins to generate this curve.

Remember, this is , so the code with the fewest bytes wins.

# Octave, 240 236 221 bytes

This is made with the same idea used here but I had to change it to fit the sierpinsky arrowhead curve.

m=input(0);g=2*pi/6;u=cos(g);v=sin(g);A=[1,0];B=[u,v];C=[-u,v];D=-A;E=-B;F=-C;for k=1:m;f=[E;F;A];b=[A;B;C];A=[B;A;F];d=[C;D;E];C=[D;C;B];E=[F;E;D];B=b;D=d;F=f;end;A=[0,0;cumsum(A)];plot(A(:,1),A(:,2));axis off;axis equal


• u=.5;v=3^u/2;B=[u,v];C=[-u,v];A=C<0; is 16 bytes shorter :) You can also do axis off equal to save another 5 bytes. Nov 23, 2016 at 11:57

# Haskell + diagrams, 176 bytes

import Diagrams.Prelude
import Diagrams.Backend.SVG
g n=renderSVG"a"(mkWidth 99).strokeT.a n
a 0=hrule 1
a n|b<-a(n-1)=b%6<>b<>b%(-6);a%n=rotateBy(1/n).reflectY\$a::Trail V2 Double


Makes a svg file with transparent background called "a".

g 0 outputs a horizontal line, g 1 is /¯\.

• Great, I didn't know about Diagrams! Nov 22, 2016 at 19:40
• @flawr, it's great but the usual Haskell graphical program caveats apply. It would be great to just call the equivalent of of plot()  to open a window.
– Angs
Nov 22, 2016 at 19:50

# MSWLogo (Version 6.5b), 102 bytes

Takes the two functions shapeL, shapeR given here and merges them by adding an extra argument :a, which calls the opposite function when negated.

to s :n :a :l
if :n=0[fd :l stop]
rt :a
s :n-1(-:a):l
lt :a
s :n-1 :a :l
lt :a
s :n-1(-:a):l
rt :a
end


A function s is defined, which takes number of iterations :n (1-based), angle :a, length :l. It is recursive, calling a lower iteration of itself with the angle :a negated in two instances to get the orientation correct.

• rt :a, lt :a rotate the turtle (triangle thingy whose path is traced) right, left by :a degrees.
• fd :l moves the turtle forward by :l steps.

The function is to be called with :a equal to 60.

Here, repeat is essentially a FOR loop, with built-in counter repcount. pu and pd mean "pen up" and "pen down", which stop the turtle from drawing while its position is being set using setxy.

The drawings of each iteration have been called with length :l equal to power 2 (7-repcount), which decreases exponentially; this is because the definition uses the same :l in the recursive step, so with fixed :l the overall size of the output will increase exponentially with :n.

• This is the right language for the job, but technically answers aren't allowed extra data, so ideally you would encode the 60 in your answer.
– Neil
Dec 3, 2016 at 13:47
• @Neil So I just include 60 in the byte count?
– user58632
Dec 4, 2016 at 4:17
• I'm not sure that it's that simple, but I don't know the language myself.
– Neil
Dec 4, 2016 at 11:10

# Python 2, 124 bytes

Based off of the code in the Wikipedia article.

from turtle import*
def c(o,a):
if o:o-=1;c(o,-a);lt(a);c(o,a);lt(a);c(o,-a)
else:fd(9)
n=input()
if n%2==0:lt(60)
c(n,60)


Order 0 is a straight line.

• You have to change your code to use an angle of 60 degrees, otherwise it will not approximate the Sierpinsky triangle. Also, the orientation changes depending on the order, which I don't think is correct. trinket.io/python/a803546939 Nov 29, 2016 at 20:47
• The logo answer also gives a function that takes the angle as a parameter, so I think that it is OK. As far as the orientation goes, it is still the same curve, just rotated. Nov 30, 2016 at 17:21
• The logo answer is always the same rotation, though. Yours is a different rotation for each order, and they are not all the same. This is not okay. Look at the pictures the question contains. Nov 30, 2016 at 17:27
• Where does the challenge say that the rotations have to be the same? Nov 30, 2016 at 18:47
• Any math major can tell you that a limit must converge. Yours does not. Nov 30, 2016 at 20:52

# Mathematica / Wolfram Language 73 bytes

s=1;Region@Line@AnglePath[Nest[Join@@({#,s=-s,s}&/@#)&,{#~Mod~2},#]Pi/3]&


Simple explanation：
AnglePath[{θ1,θ2,θ3,…}] gives the list of 2D coordinates corresponding to a path that starts at {0,0}, then takes a series of steps of unit length at successive relative angles θi.

n=1

Graphics@Line@AnglePath[60°{1,-1,-1}]


n=2

Graphics@Line@AnglePath[60°{0,1,1, -1,-1,-1, -1,1,1}]


n=3

Graphics@Line@AnglePath[60°{1,-1,-1, 1,1,1, 1,-1,-1, -1,1,1, -1,-1,-1, -1,1,1, -1,-1,-1, 1,1,1, 1,-1,-1}]


# Mathematica, 62 bytes

Graphics@Line@AnglePath[Pi/3Nest[Flatten@{-#,1,#,1,-#}&,0,#]]&

• How does that work? Nov 23, 2016 at 22:06

## JavaScript (ES6), 180 bytes

f=(n,d=0,r=n=> .repeat(n))=>n?f(--n,d=3-d%3).map(s=>r([l=s.length/2,0,1,~n&1][d]+l)+s+r([,1,0,~n&1][d]+l)).concat(f(n,d+1).map(s=>s+r(!(d%3))+a.shift(),a=f(n,d+2))):[_/\\[d%3]]
<input type=number min=1 oninput=o.textContent=f(this.value).join\n><pre id=o>

Returns an array of strings. Getting the spacing right was the hardest part! Pure string version for 205 bytes:

f=(n,d=0,r=n=> .repeat(n))=>n?f(--n,d=3-d%3).replace(/.+/g,s=>r([l=s.length/2,0,1,~n&1][d]+l)+s+r([,1,0,~n&1][d]+l))+\n+f(n,d+1).replace(/.+/g,s=>s+r(!(d%3))+a.shift(),a=f(n,d+2).split\n):_/\\[d%3]