14
\$\begingroup\$

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:

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

Sierpinski Arrowhead Curve Evolution

Your task

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

You may choose to 0- or 1-index, but please specify in your answer.

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

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

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

\$\endgroup\$
14
\$\begingroup\$

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

enter image description here

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Stewie Griffin Nov 23 '16 at 11:57
3
\$\begingroup\$

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.

Arrowheads

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Neil Dec 3 '16 at 13:47
  • \$\begingroup\$ @Neil So I just include 60 in the byte count? \$\endgroup\$ – lastresort Dec 4 '16 at 4:17
  • \$\begingroup\$ I'm not sure that it's that simple, but I don't know the language myself. \$\endgroup\$ – Neil Dec 4 '16 at 11:10
3
\$\begingroup\$

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 /¯\.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Great, I didn't know about Diagrams! \$\endgroup\$ – flawr Nov 22 '16 at 19:40
  • \$\begingroup\$ @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. \$\endgroup\$ – Angs Nov 22 '16 at 19:50
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – mbomb007 Nov 29 '16 at 20:47
  • \$\begingroup\$ 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. \$\endgroup\$ – BookOwl Nov 30 '16 at 17:21
  • \$\begingroup\$ 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. \$\endgroup\$ – mbomb007 Nov 30 '16 at 17:27
  • \$\begingroup\$ Where does the challenge say that the rotations have to be the same? \$\endgroup\$ – BookOwl Nov 30 '16 at 18:47
  • 1
    \$\begingroup\$ Any math major can tell you that a limit must converge. Yours does not. \$\endgroup\$ – mbomb007 Nov 30 '16 at 20:52
0
\$\begingroup\$

Mathematica, 62 bytes

Graphics@Line@AnglePath[Pi/3Nest[Flatten@{-#,1,#,1,-#}&,0,#]]&
\$\endgroup\$
  • \$\begingroup\$ How does that work? \$\endgroup\$ – BookOwl Nov 23 '16 at 22:06
0
\$\begingroup\$

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]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.