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.
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
Oh thanks a lot. That’s a half hour I’ll never get back.
Talking of mandelbrot…
http://www.andypope.info/fun/mandelbrot.htm
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
here’s my code for generating random numbers using simple arithmetic and no random function:
the center column (as far as I can tell) can not be expressed by a mathematical formula…
sorry … that did not come across right …
If n1 > 0 And n1 0 And n2 0 And n3 < 5 Then n3 = 1 Else n3 = 0
replace the 7th line of code with the 3 lines above …
hmm … still did not come across right …
sorry … need to look into how to post a section of code …
Enclose the code in pre tags and it won’t eat the less than signs. I took a stab at editing your first comment, but I’m not sure I got it right.