Triangle Fractal

Chip described a way to create a fractal. I paraphrase: Start with an equalateral triangle. Find the current point by picking a vertex at random. Put a mark at the current point (the vertex you just picked). Change the current point by picking a vertex at random and making the current point the midway point between the old point and the current point. Put a mark there and repeat. Do that about 50,000 times and see what you get. Or paste this code into a workbook and run SheetTriangle. Make sure you run it from Excel and not the VBE – it’s cooler to watch than just see the end result.

Sub SheetTriangle()
 
    Dim CurrX As Double
    Dim CurrY As Double
    Dim Vertices(1 To 3, 1 To 2) As Double
    Dim NextVert As Long
    Dim i As Long
    Dim wsh As Worksheet
   
    Vertices(1, 1) = 128
    Vertices(1, 2) = 1
    Vertices(2, 1) = 1
    Vertices(2, 2) = 227
    Vertices(3, 1) = 256
    Vertices(3, 2) = 227
   
    Set wsh = ThisWorkbook.Worksheets.Add
    wsh.Cells.RowHeight = 1.5
    wsh.Cells.ColumnWidth = 0.17
       
    ‘Start at the third vertex
   NextVert = 3
    CurrX = Vertices(NextVert, 1)
    CurrY = Vertices(NextVert, 2)
   
    ‘loop ten thousand times
   For i = 1 To 50000
        NextVert = Int(3 * Rnd + 1)  ‘pick a random vertext
       GetNewPoint CurrX, CurrY, Vertices(NextVert, 1), _
            Vertices(NextVert, 2) ‘find the midway point
       PlacePointWsh CLng(CurrX), CLng(CurrY), wsh ‘color a cell at that point
   Next i
End Sub
 
Sub GetNewPoint(ByRef CurrX As Double, ByRef CurrY As Double, _
     ByVal RandX As Double, ByVal RandY As Double)
 
    CurrX = CurrX + ((RandX – CurrX) / 2)
    CurrY = CurrY + ((RandY – CurrY) / 2)
 
End Sub
Sub PlacePointWsh(ByVal NewX As Long, ByVal NewY As Long, ByRef wsh As Worksheet)
 
    wsh.Cells(NewY, NewX).Interior.Color = vbBlack
   
End Sub

I started doing this with shapes instead of colored cells. It didn’t look quite as good and I’d end up with 10,000 shapes on a sheet, which can’t be too stable. I changed to coloring cells and upped the iterations to 50,000, which is roughly 256 x 227. I don’t know if 50k is too many or too few, but it definitely gives you a good idea. The 227 is what I calculated the height in cells to be to make the legs of the triangle 256. You don’t need an equalateral triangle, though, it’s just the way it was first presented to me. I also heard that the randomness isn’t necessary, but I couldn’t make it work otherwise. I’m sure it’s possible, I’m just not up to it.

I asked Chip to whom credit was due, and he said he came up with it himself. I’m not too shocked, he’s a pretty smart guy. I think 1,000 monkeys on a 1,000 typewriters would have a better chance coming up with this than me. Some guy named Sierpinski beat him to it by about 90 years, albeit via a different method.

Posted in Uncategorized

4 thoughts on “Triangle Fractal

  1. Fractals are beautiful aren’t they?

    I really like animated fractals too, like zooming into a mandelbrot.

    The method you describe is pretty much how my fractal landscape generator works.

    After I created the formula page, I noticed how big the workbook got – about 2MB. Being wary of large downloads, I decided to create the formula page on workbook open.
    A fractal generator generator.

    Rob

  2. Although black and white is what sierpinski saw, it is more colourfull to whatch it with:

    Sub PlacePointWsh(ByVal NewX As Long, ByVal NewY As Long, ByRef wsh As Worksheet)

    wsh.Cells(NewY, NewX).Interior.Color = Choose(Int(7 * Rnd + 1), _
    vbBlack, vbRed, vbBlue, vbGreen, vbYellow, vbMagenta, vbWhite)

    End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *