Draw the Sierpinski Arrowhead Curve

14

2

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.

Oliver Ni

Posted 2016-11-18T19:20:21.300

Reputation: 9 650

Answers

14

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

flawr

Posted 2016-11-18T19:20:21.300

Reputation: 40 560

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. – Stewie Griffin – 2016-11-23T11:57:36.670

3

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

Angs

Posted 2016-11-18T19:20:21.300

Reputation: 4 825

Great, I didn't know about Diagrams! – flawr – 2016-11-22T19:40:33.147

@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 – 2016-11-22T19:50:13.893

2

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.

for Monica

Posted 2016-11-18T19:20:21.300

Reputation: 1 172

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 – 2016-12-03T13:47:58.960

@Neil So I just include 60 in the byte count? – for Monica – 2016-12-04T04:17:07.667

I'm not sure that it's that simple, but I don't know the language myself. – Neil – 2016-12-04T11:10:06.387

1

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.

BookOwl

Posted 2016-11-18T19:20:21.300

Reputation: 291

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. https://trinket.io/python/a803546939

– mbomb007 – 2016-11-29T20:47:57.960

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. – BookOwl – 2016-11-30T17:21:46.567

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. – mbomb007 – 2016-11-30T17:27:12.820

Where does the challenge say that the rotations have to be the same? – BookOwl – 2016-11-30T18:47:14.863

The rules say this: 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. ✓ (I'm generating an image). You may not use built-ins to generate this curve. ✓ My entry breaks none of the rules, so it is valid. – BookOwl – 2016-11-30T18:48:04.133

Also, if you say that the graphics output has to be the same as the pictures in the challenge, then the logo entry is disqualified too. It doesn't have the same rotation as the pictures. – BookOwl – 2016-11-30T18:52:21.093

It says "[the] limit is Sierpinski's Triangle". Your output doesn't follow that. – mbomb007 – 2016-11-30T19:41:40.127

Yes, it does. The triangle is just upside down for even n. That is just as valid as having it be sideways. – BookOwl – 2016-11-30T20:46:13.663

1Any math major can tell you that a limit must converge. Yours does not. – mbomb007 – 2016-11-30T20:52:00.890

I'm temporarily removing this post in compliance with our policy about answers not meeting the challenge specification. Please feel free to edit your answer and to flag it for moderator attention so it can be undeleted.

– Dennis – 2016-12-02T01:22:39.470

1

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}]

enter image description here enter image description here

chyanog

Posted 2016-11-18T19:20:21.300

Reputation: 1 078

0

Mathematica, 62 bytes

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

alephalpha

Posted 2016-11-18T19:20:21.300

Reputation: 23 988

How does that work? – BookOwl – 2016-11-23T22:06:05.417

0

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]

Neil

Posted 2016-11-18T19:20:21.300

Reputation: 95 035