Subscribe to RSS
get email updates
home | about | pixDif AIR app | video tutorials
polyGeek.com

Register for 360Flex in DC using the ad below and you will automatically be entered in a drawing for a free ticket. Read more.
place your ad here

Web Premium





Random generation of the Sierpinski triangle

August 14th, 2007 . by polyGeek

On the first page of the book Superfractals is one of those cool mathematical curiosities that makes math geeks like me go all Archimedian. (No, I didn’t run naked to the bookstore counter and buy the book. But I did go home and order it at Amazon.)

Here’s the gist of the curiosity, which I created an algorithm for below:

Pick 3 points on the xy-plane and label them A, B, and C. (As a group I’ll call them ABC.)

Now pick a fourth point and label it X0. Now randomly pick a point from the ABC group. From X0 make a new point that is the mid-point between your random ABC selection and X0. Label that point X1.

Now from X1 do the same thing again. Pick randomly between ABC group and then from X1 go to the mid-point between X1 and your random selection.

Repeat that a few millions times.

You might think that eventually you’ll fill up the points in between the ABC group and have a solid triangle. But no. You’ll end up with a skewed Sierpinski triangle as you can see below.

Serendipitous errors

In my first version of the algorithm I made a mistake in the equation that calculates the mid-point. The equation should be as such:

xmid = ( x1 + x2 ) / 2

And the same for the y-coordinate.

In my haste I wrote the incorrectly and calculated a difference instead of a sum between the two values.

Now that sould produce something random because what on earth could the relationship of the difference between the first and second points divided by two have anything to do with?

But you don’t get a random dispersion. You get something that closely resembles the inverse of the Kosh snowflake. Why? Ask a mathematician because I have no idea.

Grdenizing the code

This is exactly the sort of algorithm where code optimization can make a big difference. John Grden just wrote a wonderful post on optimizing Actionscript by using bitwise operations and a few other tricks that I applied here.

In the application above you can check/uncheck the optimized code checkbox to select which version of the method gets used. Then you can see the results displayed and judge for yourself how much code optimization can benefit you in cases where you are iterating over large sets of data.

View source code here.

The obvious place to optimize is with the mid-point equation. Instead of dividing by two I’m using the equivalent bitwise operation: >> 1.

That helped but using int(num) instead of Math.floor(num) made a huge difference. I’ll never use Math.floor() again that’s for sure.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

11 Responses to “Random generation of the Sierpinski triangle”


comment number 1 by: Keith Peters

That’s hilarious. I just made almost the same thing last weekend. Found this article via Quasimondo’s flickr page: http://www.flickr.com/photos/quasimondo/1021737246/in/photostream/

They’ve been sitting there ready to post on my blog. You beat me to it!

comment number 2 by: John Grden

“Grdenizing the code” – Love it

LOVE IT!

comment number 3 by: polyGeek

@Keith, thanks for the link. I can’t wait to check out that site this weekend. Right now I’m just trying to catch up with work after the 360Flex conference.

@John, I had a feeling you might. Nice work and thanks a heap for all the optimization ideas. You’ve save me hundreds, no wait, thousands of milliseconds! :-)


[...] Function Systems and Attractors Covers the mathematical iteration I used here. Share and Enjoy: These icons link to social bookmarking sites where readers can share and [...]

comment number 5 by: beckie

i love sierpinski


[...] along those lines is a Flex app that creates a Sierpinski triangle by choosing random numbers. Share and [...]

comment number 7 by: Jaque

Hello,

i find Your application very interresting, but i would have a question for you: do you think a Sierpinski triangle would emerge if instead of inputing random variables, we would input random variables and in random moments of input time we change the randomly choosen variable to the opposite one? For instance if at a moment t (which should be chosen randomly), the RNG chooses randomly the path to a particular vortex of the triangle, but the program instead switches to one of the opposite ones. It would be interesting to see if in this case the Sierpinski triangle is constructed. Could you make such an algorithm??

comment number 8 by: polyGeek

@Jaque, If I understand you correctly then it wouldn't make any difference to the output at all. You would get exactly the same thing.

comment number 9 by: Jaque

hello,

first thanx for the answer. But this puzzles me a little, since ive read in a math book that the condition for a sierpinski triangle to occur is that the vortexes should be picked randomly. In the same book was stated that even if a very sofisticated algorithm (which appears to be random because of its complexity ) would be used in picking the vortexes – then in this case the result wouldnt be a sierpinski triangle. So in the example i given , the vortexes would be picked in a random + deterministic way sometimes. What do you think?

I thought about this idea, because i asked myself if the method of randomly constructed sierpinski triangle could be used for testing for example the randomness of sport results or lottery results, or even financial markets

comment number 10 by: mark

What is the Sierpinski triangle used for in math

comment number 11 by: polyGeek

@mark, I wouldn't say that the Sierpinski triangle is used in mathematics. It's probably more accurate to say that it falls out in many different, and sometimes surprising, areas. For a better understading I'd visit the Wikipedia page.

   Welcome back (Change)

Leave a Reply

comment feed RSS   subscribe to this comment thread

Recent Posts

   



polyGeek.com

© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites
Local Directory for Los Angeles, CA

Better Tag Cloud