Advertisement
Guest User

Mandelbrot set in Excel VBA Code

a guest
Aug 25th, 2016
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. Public Sub drawMandelbrotBW()
  2. Sheet1.Activate
  3. ActiveSheet.Cells(1, 1).Select
  4. Dim width As Integer
  5. Dim height As Integer
  6. Dim max As Integer
  7. Dim Row As Integer
  8. Dim Col As Integer
  9. Dim black As Long
  10.  
  11. width = 1920
  12. height = 1080
  13. max = 30
  14. black = RGB(0, 0, 0)
  15.  
  16. Dim c_re As Double
  17. Dim c_im As Double
  18. Dim x As Double
  19. Dim y As Double
  20. Dim iterations As Integer
  21. Dim x_new As Double
  22. Dim color_val As Double
  23.  
  24. For Row = 1 To height
  25. For Col = 1 To width
  26. c_re = (Col - width / 2) * 4 / width
  27. c_im = (Row - height / 2) * 4 / width
  28. x = 0
  29. y = 0
  30. iterations = 0
  31. Do While (x ^ 2 + y ^ 2) < 4 And iterations < max
  32. x_new = x ^ 2 - y ^ 2 + c_re
  33. y = 2 * x * y + c_im
  34. x = x_new
  35. iterations = iterations + 1
  36. Loop
  37. If iterations < max Then
  38. color_val = 255 - (iterations * 8.5)
  39. Cells(Row, Col).Interior.Color = RGB(color_val, color_val, color_val)
  40. Else
  41. Cells(Row, Col).Interior.Color = RGB(0, 0, 0)
  42. End If
  43. Next Col
  44. Next Row
  45. End Sub
  46.  
  47. Public Sub drawMandelbrotColor()
  48. Sheet1.Activate
  49. ActiveSheet.Cells(1, 1).Select
  50. Dim width As Integer
  51. Dim height As Integer
  52. Dim max As Integer
  53. Dim Row As Integer
  54. Dim Col As Integer
  55. Dim black As Long
  56.  
  57. width = 1920
  58. height = 1080
  59. max = 40
  60. black = RGB(0, 0, 0)
  61.  
  62. Dim c_re As Double
  63. Dim c_im As Double
  64. Dim x As Double
  65. Dim y As Double
  66. Dim iterations As Integer
  67. Dim x_new As Double
  68. Dim color_val As Double
  69. Dim pallete(40) As Long
  70. Call initializePallete(pallete, max)
  71.  
  72. For Row = 1 To height
  73. For Col = 1 To width
  74. c_re = (Col - width / 2) * 4 / width
  75. c_im = (Row - height / 2) * 4 / width
  76. x = 0
  77. y = 0
  78. iterations = 0
  79. Do While (x ^ 2 + y ^ 2) < 4 And iterations < max
  80. x_new = x ^ 2 - y ^ 2 + c_re
  81. y = 2 * x * y + c_im
  82. x = x_new
  83. iterations = iterations + 1
  84. Loop
  85. If iterations < max Then
  86. Cells(Row, Col).Interior.Color = pallete(iterations)
  87. Else
  88. Cells(Row, Col).Interior.Color = RGB(0, 0, 0)
  89. End If
  90. Next Col
  91. Next Row
  92. End Sub
  93.  
  94. Public Sub initializePallete(pallete() As Long, max As Integer)
  95. colorchunks = max / 4
  96.  
  97.  
  98. c1R = 0
  99. c1G = 7
  100. c1B = 100
  101.  
  102. c2R = 32
  103. c2G = 107
  104. c2B = 203
  105.  
  106. c3R = 237
  107. c3G = 255
  108. c3B = 255
  109.  
  110. c4R = 255
  111. c4G = 170
  112. c4B = 0
  113.  
  114. c5R = 0
  115. c5G = 2
  116. c5B = 0
  117.  
  118. r = (c2R - c1R) / colorchunks
  119. g = (c2G - c1G) / colorchunks
  120. b = (c2B - c1B) / colorchunks
  121.  
  122. For i = 0 To colorchunks
  123. pallete(i) = RGB(c1R, c1G, c1B)
  124. c1R = c1R + r
  125. c1G = c1G + g
  126. c1B = c1B + b
  127. Next
  128.  
  129. r = (c3R - c2R) / colorchunks
  130. g = (c3G - c2G) / colorchunks
  131. b = (c3B - c2B) / colorchunks
  132. For i = colorchunks To colorchunks * 2
  133. pallete(i) = RGB(c2R, c2G, c2B)
  134. c2R = c2R + r
  135. c2G = c2G + g
  136. c2B = c2B + b
  137. Next
  138.  
  139. r = (c4R - c3R) / colorchunks
  140. g = (c4G - c3G) / colorchunks
  141. b = (c4B - c3B) / colorchunks
  142. MsgBox (CStr(r) + " " + CStr(g) + " " + CStr(b))
  143. For i = (colorchunks * 2) To (colorchunks * 3)
  144. pallete(i) = RGB(c3R, c3G, c3B)
  145. c3R = c3R + r
  146. c3G = c3G + g
  147. c3B = c3B + b
  148. Next
  149.  
  150. r = (c5R - c4R) / colorchunks
  151. g = (c5G - c4G) / colorchunks
  152. b = (c5B - c4B) / colorchunks
  153. For i = colorchunks * 3 To colorchunks * 4
  154. pallete(i) = RGB(c4R, c4G, c4B)
  155. c4R = c4R + r
  156. c4G = c4G + g
  157. c4B = c4B + b
  158. Next
  159. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement