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