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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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.