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