The Sierpinski Triangle is a fractal. According to Wikipedia it was named after "mathematician Waclaw Sierpinski who described it in 1915. However, similar patterns appear already in the 13th-century Cosmati mosaics in the cathedral of Anagni, Italy." You can create the Sierpinski Triangle (and very similar fractals) with surprisingly little code. The following picture and instructions are also from Wikipedia.
Start with any triangle, though the usual Sierpinski triangle uses an equilateral triangle. Shrink the triangle to ½ height and ½ width, make three copies, and position the three shrunken triangles so that each triangle touches the two other triangles at a corner (image 2). Note the emergence of the central hole - because the three shrunken triangles can between them cover only 3/4 of the area of the original. (Holes are an important feature of Sierpinski's triangle.) Repeat step 2 with each of the smaller triangles (image 3 and so on).
-
You may find slides #1 - 46 and 70 - 73 of the Recursion presentation and the Recursion 1 worksheet helpful.
-
Fork and clone down this repository. If you are working online, fork this repl.it (Optional: Create your own repository from scratch. Look at the bottom of this page for instructions)
-
Write the
sierpinski
function:- If
len
is less than or equal to 20 (or some variable)- Draw a triangle with the left corner at (
x
,y
) and a base and height equal tolen
.
- Draw a triangle with the left corner at (
- else
- recursively call the
sierpinksi
function to draw a triangle with the left corner at (x
,y
) and a base and height equal tolen/2
. - Again, call the
sierpinksi
function a second time to draw another triangle a distance oflen/2
to the right of the first triangle. - Now, call the
sierpinksi
function a third time to draw a triangle a distance oflen/4
to the right andlen/2
up from the first triangle. This triangle should "sit on top" of the first two.
- recursively call the
- If
-
Once you have the
sierpinksi
function completed, call it once indraw()
to start the process. You can change the number and size of the triangles by changing the base case limit in theif
from 20 to some variable and adjusting the value of the variable. Some ways to make the program interactive include usingmousePressed()
,keyPressed()
,mouseDragged()
ormouseMoved()
to change the base case limit. -
Feel free to create your own individual variation of the Sierpinski triangle. Your recursive triangle doesn't have to look like any other. It doesn't even need to use triangles. The mathematically adventurous might want to check out the sierpinski triangle page to end most sierpinski triangle pages ™.
-
You could also create a Sierpinski carpet, Wallis sieve, Hawaiian earring, Menger sponge or similiar as an alternative to the Sierpinski Triangle.
It's not hard to create your own repostiory.
- Create a folder called SierpinskiTriangle in your apjava folder.
- Copy
processing.js
from a previous assignment and move it to the SierpinskiTriangle folder (Or, better practice, download the latest development version) - Use Sublime to create the following 3 files, all of which are stored in the SierpinskiTriangle folder. Choose New File and then Save As and give the file the correct name
SierpinskiTriangle.pde
index.html
(can be copied from a previous assignment, you will just need to change the canvas id tag in the body to match the nameSierpinskiTriangle
)styles.css
(can be copied from a previous assignment)
- Now, go to GitHub and create a new empty SierpinskiTriangle repository. GitHub will provide the commands that you use to link your local SierpinskiTriangle folder to the GitHub repository
- To display your program on the web, you will want to enable GitHub pages. Go to the settings for your repository, scroll down to the GitHub pages section, choose
Master branch
and click Save.
Johnny
Yanzhu
Edison
Dylan
Antonio
Marlon
Gabriela
Ben
Noah
Rebecca
Tomas
Rebecca
Tommy
Dylan
Marlon
Eric
Justine
Nathan
Gabriela
Theo
Kai
Claire
Kazu
Matt
Tommy
Ling Feng
Jonathan
Riley
Robert
Kelly
Bryan
Angel
Mia
David
Brandon
David
Henry
Chris
Nathan
Lawrence
Andy
Anson
Raymond
Eric
Andy
Gee
Timmy
Sean
Richard
Joshua
Jason
Frida
Amy
Ruixin
Christine
Ian
Andy
Kam
Lucian
Jalen
Dalton
Jessica
Diego
Lauren
Emma
Lily
Jayde
Gordon
Calvin
Alex
Norton
Marc
Esme
Daniel
Jessie
Alvin
Carmen
Jennifer
Michelle
Freesia
Iryne
Esther
Vicky
Eloy
Angela
Adam
Brendan
Andy
Lianne
Melinda
Ryan
Ravi
Slavik
Chris
Emily
Joey
Jonathan
Alex
Ryan
Emma
Gordon
Andrew
Joe
Albert
Mike
Kenny
Victor
Desmond
Farya
Garvin
Preston
Jayden
Makoi
Thanawat
Lydia
Heath
Kennety
Kirby
Andrea
Aaron
Erica
Vivian
Sophie
Otto
Joshua
Michael
Eric
Karen
Hannah
Erica
Yolanda
Janet
Nathan
Felix
Mi-Kaela
Kervin
Joanna
Emma
Andrew
Schuyler
Ryan
Steven
Katie
Elton
Colin
Raymond
Preston
Eric
Anya
Nathan
Sasha
Oliver