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