I am a freshman in high school who needs a math related project, so I decided on the topic of fractals. Being an avid developer, I thought it would be awesome to write a Ruby program that can calculate a fractal. The only problem is that I am not some programming god, and I have not worked on any huge projects (yet). So I need a basic-ish fractal 'type' to do the project on. I am a very quick learner, and my math skills greatly outdo that of my peers (I was working on derivatives by myself last year). So does anybody have any good ideas? Thanks!!!! :)

PS: my school requires a live resource for every project we do, so would anybody be interested in helping? :)

  • 2
    $\begingroup$ The Mandelbrot fractal is not so difficult to generate and is quite spectacular. Do you have any knowledge of complex numbers? $\endgroup$ Sep 14, 2012 at 18:20
  • $\begingroup$ haha, ummmm, yes, well, not a solid understanding, but some... and as I said, I was blessed with the gift off being able to learn quickly $\endgroup$ Sep 14, 2012 at 18:21
  • 1
    $\begingroup$ The wikipedia page even contains the basic algorithm. $\endgroup$ Sep 14, 2012 at 18:25
  • $\begingroup$ Skimming, I got as far as "complex quadratic polynomials" o.O $\endgroup$ Sep 14, 2012 at 18:25

4 Answers 4


A rather simple one (that is given as an exercise in recursion in many LOGO classes) is to draw the Koch Snowflake.

A slightly more complex one would be the Mandelbrot set. The basic concept behind it is this:

You have a set of numbers, called Complex numbers. The basic concept is that they define $i=\sqrt{-1}$. Yep, it's a number which doesn't exist, but it's extremely useful. A Complex number is any "number" of the form $a+bi$. These don't exist either (unless $b=0$), so another way to look at a complex number is that it is a point on the coordinate plane, $(a,b)$. Just like you can plot numbers on a number line, complex numbers can be plotted on the Cartesian plane. They multiply and divide normally, just pretend that $i$ is any run-of-the-mill variable and replace all $i^2$s with $-1$ when they appear.

So how does this relate to a fractal? Well, let's consider this recursive relation for a given complex number $c$:


Basically, this means you take the number $c$, add it to $z_0^2=0$, and get $z_1$. You plug $z_1$ back into the equation (square it and add $c$), and you get $z_2$. And so on.

Now, you check if this sequence of $z$s escapes to infinity (i.e., the $a$ or $b$ becomes infinite). If it doesn't do so within some fixed number of iterations, the number belongs in this "Mandelbrot set".

Now, all you do is take some fixed area of the coordinate plane (The most common is to let x range from $-2$ to $1$, and y from $-1$ to $1$), and check which points in the area belong to the set (By this, I mean "check if the complex number $x+iy$ corresponding to the point $(x,y)$ is in this set"). Plot the point if it belongs to the set, and you get a beautiful snowman-like thing.

It gets even more interesting if you replace the $z^2$ with some other power (note that fractional powers will require you to know a bit more about complex numbers)

  • $\begingroup$ Alright, that makes sense, and doesnt seem TOO hard to iterate :) Thank you again Manish ^^ $\endgroup$ Sep 17, 2012 at 13:50

Many fractals are fairly simple to write. Most of them will have a list of 'something' (say lines) and a function that splits each 'something' into several parts thus generating a new, larger list from the old one. Then it only remains to draw the list after several iterations. Of course, each fractal will have slightly different algorithm.

Note that I'm talking about fractals like Koch snowflake or Sierpinski triangle. Things like Mandelbrot set are quite different as they do not have such discrete parts. There the simplest idea is to test each point for membership.

Example: Sierpinski triangle

Have a structure containing three points that define a triangle. Have a function, that takes one triangle and returns an array of 3 smaller triangles formed from corners and midpoints of sides of the input triangle like this. Start with an array containing one large triangle. Then do 'map' and 'flatten' on that array to fill it with small triangles. Finally draw each of them.

  • $\begingroup$ Well, I have created a tiny Ruby program that, given the original size of the rectangle and the "decay factor", finds the first 10 iterations of a rectangle getting smaller and smaller... $\endgroup$ Sep 14, 2012 at 18:28
  • $\begingroup$ That much skill should be enough, assuming you know how to deal with lists. $\endgroup$ Sep 14, 2012 at 18:32
  • $\begingroup$ Okay! :) thank ya :) $\endgroup$ Sep 14, 2012 at 19:31

Maybe you want to consider iterated function systems


Mandelbrot set in Ruby :


See also :




You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .