author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 21 Jul 2025 16:38:07 +0100 | |
changeset 494 | 253d1ccb65de |
parent 493 | 244df77507c2 |
permissions | -rw-r--r-- |
187 | 1 |
// Mandelbrot pictures |
309 | 2 |
//===================== |
266 | 3 |
// |
187 | 4 |
// see https://en.wikipedia.org/wiki/Mandelbrot_set |
490 | 5 |
// |
494 | 6 |
// You can run the file on the commandline with |
490 | 7 |
// |
493 | 8 |
// scala mandelbrot.sc |
471 | 9 |
// |
494 | 10 |
// Or inside the REPL with |
490 | 11 |
// |
494 | 12 |
// scala --extra-jars scala-parallel-collections_3-1.0.4.jar |
464
73ced118f73d
updated to scala 3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
444
diff
changeset
|
13 |
// |
491 | 14 |
// !! UPDATE ON TIMING: On my faster Mac-M1 machine |
15 |
// !! the times for the first example are ca. 4 secs for |
|
16 |
// !! the sequential version and around 0.7 secs for the |
|
490 | 17 |
// !! par-version. |
464
73ced118f73d
updated to scala 3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
444
diff
changeset
|
18 |
|
491 | 19 |
// for parallel collections |
20 |
//> using dep org.scala-lang.modules::scala-parallel-collections:1.0.4 |
|
21 |
import scala.collection.parallel.CollectionConverters.* |
|
187 | 22 |
|
491 | 23 |
// for graphics |
490 | 24 |
import javax.swing.{JFrame, JPanel, WindowConstants} |
25 |
import java.awt.{Color, Dimension, Graphics, Graphics2D} |
|
124 | 26 |
import java.awt.image.BufferedImage |
490 | 27 |
|
493 | 28 |
// for implicits in comples numbers |
29 |
import scala.language.implicitConversions |
|
491 | 30 |
|
31 |
||
265 | 32 |
|
124 | 33 |
// complex numbers |
491 | 34 |
// represents the complex number re + im * i |
490 | 35 |
case class Complex(val re: Double, val im: Double) { |
491 | 36 |
|
186 | 37 |
def +(that: Complex) = Complex(this.re + that.re, this.im + that.im) |
38 |
def -(that: Complex) = Complex(this.re - that.re, this.im - that.im) |
|
39 |
def *(that: Complex) = Complex(this.re * that.re - this.im * that.im, |
|
40 |
this.re * that.im + that.re * this.im) |
|
41 |
def *(that: Double) = Complex(this.re * that, this.im * that) |
|
42 |
def abs() = Math.sqrt(this.re * this.re + this.im * this.im) |
|
124 | 43 |
} |
44 |
||
490 | 45 |
// to allow the usual mathmo notation n + m * i |
186 | 46 |
object i extends Complex(0, 1) |
464
73ced118f73d
updated to scala 3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
444
diff
changeset
|
47 |
|
73ced118f73d
updated to scala 3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
444
diff
changeset
|
48 |
// implicit conversion from Doubles to Complex |
73ced118f73d
updated to scala 3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
444
diff
changeset
|
49 |
given Conversion[Double, Complex] = Complex(_, 0) |
186 | 50 |
|
491 | 51 |
// some customn colours for the "sliding effect" outside |
52 |
// the mandelbrot set |
|
124 | 53 |
val colours = List( |
490 | 54 |
Color(66, 30, 15), Color(25, 7, 26), |
55 |
Color(9, 1, 47), Color(4, 4, 73), |
|
56 |
Color(0, 7, 100), Color(12, 44, 138), |
|
57 |
Color(24, 82, 177), Color(57, 125, 209), |
|
58 |
Color(134, 181, 229), Color(211, 236, 248), |
|
59 |
Color(241, 233, 191), Color(248, 201, 95), |
|
60 |
Color(255, 170, 0), Color(204, 128, 0), |
|
61 |
Color(153, 87, 0), Color(106, 52, 3)) |
|
124 | 62 |
|
266 | 63 |
// the viewer panel with an image canvas |
124 | 64 |
class Viewer(width: Int, height: Int) extends JPanel { |
490 | 65 |
val canvas = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB) |
66 |
||
67 |
override def paintComponent(g: Graphics) = |
|
186 | 68 |
g.asInstanceOf[Graphics2D].drawImage(canvas, null, null) |
490 | 69 |
|
70 |
override def getPreferredSize() = |
|
71 |
Dimension(width, height) |
|
124 | 72 |
|
186 | 73 |
def clearCanvas(color: Color) = { |
490 | 74 |
for (x <- 0 to width - 1; y <- 0 to height - 1) |
186 | 75 |
canvas.setRGB(x, y, color.getRGB()) |
76 |
repaint() |
|
490 | 77 |
} |
124 | 78 |
} |
79 |
||
266 | 80 |
// initialising the viewer panel |
186 | 81 |
def openViewer(width: Int, height: Int) : Viewer = { |
491 | 82 |
val viewer = Viewer(width, height) |
490 | 83 |
val frame = JFrame("XYPlane") |
186 | 84 |
frame.add(viewer) |
85 |
frame.pack() |
|
86 |
frame.setVisible(true) |
|
87 |
frame.setResizable(false) |
|
88 |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) |
|
89 |
viewer |
|
124 | 90 |
} |
91 |
||
266 | 92 |
// some hardcoded parameters |
186 | 93 |
val W = 900 // width |
94 |
val H = 800 // height |
|
124 | 95 |
val black = Color.black |
96 |
val viewer = openViewer(W, H) |
|
97 |
||
266 | 98 |
// draw a pixel on the canvas |
490 | 99 |
def pixel(x: Int, y: Int, color: Color) = |
124 | 100 |
viewer.canvas.setRGB(x, y, color.getRGB()) |
186 | 101 |
|
124 | 102 |
|
490 | 103 |
// calculates the number of iterations using lazy lists (streams) |
186 | 104 |
// the iteration goes on for a maximum of max steps, |
105 |
// but might leave early when the pred is satisfied |
|
106 |
def iterations(c: Complex, max: Int) : Int = { |
|
490 | 107 |
def next(z: Complex) = z * z + c |
353 | 108 |
def pred(z: Complex) = z.abs() < 2 // exit condition |
266 | 109 |
LazyList.iterate(0.0 * i, max)(next).takeWhile(pred).size |
186 | 110 |
} |
111 |
||
490 | 112 |
// main function |
113 |
// start and end are the upper-left and lower-right corners, |
|
187 | 114 |
// max is the number of maximum iterations |
186 | 115 |
def mandelbrot(start: Complex, end: Complex, max: Int) : Unit = { |
124 | 116 |
viewer.clearCanvas(black) |
490 | 117 |
|
118 |
// deltas for each grid step |
|
186 | 119 |
val d_x = (end.re - start.re) / W |
120 |
val d_y = (end.im - start.im) / H |
|
490 | 121 |
|
481 | 122 |
for (y <- (0 until H).par) { |
123 |
for (x <- (0 until W).par) { |
|
490 | 124 |
|
125 |
val c = start + x * d_x + y * d_y * i |
|
126 |
val iters = iterations(c, max) |
|
491 | 127 |
val colour = |
490 | 128 |
if (iters == max) black |
129 |
else colours(iters % 16) |
|
124 | 130 |
|
464
73ced118f73d
updated to scala 3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
444
diff
changeset
|
131 |
pixel(x, y, colour) |
143 | 132 |
} |
133 |
viewer.updateUI() |
|
490 | 134 |
} |
124 | 135 |
} |
136 |
||
187 | 137 |
|
491 | 138 |
|
139 |
||
124 | 140 |
// Examples |
141 |
//========== |
|
142 |
||
143 |
//for measuring time |
|
144 |
def time_needed[T](code: => T) = { |
|
145 |
val start = System.nanoTime() |
|
146 |
code |
|
147 |
val end = System.nanoTime() |
|
148 |
(end - start) / 1.0e9 |
|
149 |
} |
|
150 |
||
151 |
||
353 | 152 |
|
124 | 153 |
// example 1 |
186 | 154 |
val exa1 = -2.0 + -1.5 * i |
155 |
val exa2 = 1.0 + 1.5 * i |
|
124 | 156 |
|
309 | 157 |
println(s"${time_needed(mandelbrot(exa1, exa2, 1000))} secs") |
124 | 158 |
|
136 | 159 |
// example 2 |
186 | 160 |
val exb1 = -0.37465401 + 0.659227668 * i |
161 |
val exb2 = -0.37332410 + 0.66020767 * i |
|
124 | 162 |
|
186 | 163 |
//time_needed(mandelbrot(exb1, exb2, 1000)) |
124 | 164 |
|
165 |
// example 3 |
|
186 | 166 |
val exc1 = 0.435396403 + 0.367981352 * i |
167 |
val exc2 = 0.451687191 + 0.380210061 * i |
|
124 | 168 |
|
166 | 169 |
//time_needed(mandelbrot(exc1, exc2, 1000)) |
124 | 170 |
|
171 |
// some more computations with example 3 |
|
186 | 172 |
|
491 | 173 |
val delta = (exc2 - exc1) * 0.01 |
124 | 174 |
|
471 | 175 |
println(s"${time_needed( |
490 | 176 |
for (n <- (0 to 25)) |
177 |
mandelbrot(exc1 + delta * n, |
|
178 |
exc2 - delta * n, 1000))} secs") |
|
266 | 179 |
|
124 | 180 |
|
191 | 181 |
// Larry Paulson's example |
182 |
val exl1 = -0.74364990 + 0.13188170 * i |
|
183 |
val exl2 = -0.74291189 + 0.13261971 * i |
|
189 | 184 |
|
309 | 185 |
//println(s"${time_needed(mandelbrot(exl1, exl2, 1000))} secs") |
189 | 186 |
|
191 | 187 |
|
188 |
// example by Jorgen Villadsen |
|
189 |
val exj1 = 0.10284 - 0.63275 * i |
|
190 |
val exj2 = 0.11084 - 0.64075 * i |
|
191 |
||
195 | 192 |
//time_needed(mandelbrot(exj1, exj2, 1000)) |
394 | 193 |
|
194 |
||
195 |
// another example |
|
196 |
val exA = 0.3439274 + 0.6516478 * i |
|
197 |
val exB = 0.3654477 + 0.6301795 * i |
|
198 |
||
199 |
//time_needed(mandelbrot(exA, exB, 1000)) |