Jason Underdown’s Blog

Math, Physics and Free Software

Sierpinski Triangle C++ Code

with 3 comments

If you like pretty pictures, and I know you do, below is a plot of Sierpinski’s Triangle which you can generate with the short C++ program I wrote. This is an example of a fractal–an object that is self similar at all levels of magnification.

Sierpinski Triangle

On a Linux/UNIX system, to compile and run the code type:

$ g++ sierpinski.cc -o sierpinski
$ ./sierpinski >out
Enter depth (integer > 0, usually 10 is good): 10
Enter contraction mapping constant (for example 0.5):  0.5
$

Now the points are stored in a file called ‘out’. To plot them you can use gnuplot like so:

$ gnuplot
gnuplot> plot 'out' with points pointtype 0

Written by Jason Underdown

June 15, 2007 at 11:20 am

Posted in Math, Programming

3 Responses

Subscribe to comments with RSS.

  1. I see that you are using Gnuplot, so I thought you might be interested to know that there is now a book on it: “Gnuplot in Action”. You can pre-order it directly from the publisher: Manning: Gnuplot in Action.

    If you want to learn more about the book and the author, check out my book page at Principal Value – Gnuplot in Action.

    Let me know if you are interested in a review copy.

    Philipp K. Janert

    March 19, 2008 at 8:30 pm

  2. very nice work!

    Iannis

    April 9, 2008 at 2:00 pm

  3. Thanks!,

    Xqlomgsl

    December 13, 2008 at 9:23 am


Leave a Reply