progs/mandelbrot.scala
changeset 491 e2ffe8642f55
parent 490 4778fefecd0c
child 492 4ffba2f72692
equal deleted inserted replaced
490:4778fefecd0c 491:e2ffe8642f55
     1 // Mandelbrot pictures
       
     2 //=====================
       
     3 //
       
     4 //   see https://en.wikipedia.org/wiki/Mandelbrot_set
       
     5 //
       
     6 // needs to be called with
       
     7 //
       
     8 //   scala-cli --extra-jars scala-parallel-collections_3-1.0.4.jar
       
     9 //
       
    10 // the jar-file is uploaded to KEATS
       
    11 //
       
    12 //
       
    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.
       
    17 
       
    18 
       
    19 import javax.swing.{JFrame, JPanel, WindowConstants}
       
    20 import java.awt.{Color, Dimension, Graphics, Graphics2D}
       
    21 import java.awt.image.BufferedImage
       
    22 
       
    23 import scala.language.implicitConversions
       
    24 import scala.collection.parallel.CollectionConverters.*
       
    25 
       
    26 // complex numbers
       
    27 // represents the complex number re + im * i
       
    28 case class Complex(val re: Double, val im: Double) {
       
    29   
       
    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)
       
    36 }
       
    37 
       
    38 // to allow the usual mathmo notation n + m * i
       
    39 object i extends Complex(0, 1)
       
    40 
       
    41 // implicit conversion from Doubles to Complex
       
    42 given Conversion[Double, Complex] = Complex(_, 0)
       
    43 
       
    44 // some customn colours for the "sliding effect"
       
    45 val colours = List(
       
    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))
       
    54 
       
    55 // the viewer panel with an image canvas
       
    56 class Viewer(width: Int, height: Int) extends JPanel {
       
    57   val canvas = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
       
    58 
       
    59   override def paintComponent(g: Graphics) =
       
    60     g.asInstanceOf[Graphics2D].drawImage(canvas, null, null)
       
    61 
       
    62   override def getPreferredSize() =
       
    63     Dimension(width, height)
       
    64 
       
    65   def clearCanvas(color: Color) = {
       
    66     for (x <- 0 to width - 1; y <- 0 to height - 1)
       
    67       canvas.setRGB(x, y, color.getRGB())
       
    68     repaint()
       
    69   }
       
    70 }
       
    71 
       
    72 // initialising the viewer panel
       
    73 def openViewer(width: Int, height: Int) : Viewer = {
       
    74   val frame = JFrame("XYPlane")
       
    75   val viewer = Viewer(width, height)
       
    76   frame.add(viewer)
       
    77   frame.pack()
       
    78   frame.setVisible(true)
       
    79   frame.setResizable(false)
       
    80   frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
       
    81   viewer
       
    82 }
       
    83 
       
    84 // some hardcoded parameters
       
    85 val W = 900   // width
       
    86 val H = 800   // height
       
    87 val black = Color.black
       
    88 val viewer = openViewer(W, H)
       
    89 
       
    90 // draw a pixel on the canvas
       
    91 def pixel(x: Int, y: Int, color: Color) =
       
    92   viewer.canvas.setRGB(x, y, color.getRGB())
       
    93 
       
    94 
       
    95 // calculates the number of iterations using lazy lists (streams)
       
    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 = {
       
    99   def next(z: Complex) = z * z + c
       
   100   def pred(z: Complex) = z.abs() < 2    // exit condition
       
   101   LazyList.iterate(0.0 * i, max)(next).takeWhile(pred).size
       
   102 }
       
   103 
       
   104 // main function
       
   105 //    start and end are the upper-left and lower-right corners,
       
   106 //    max is the number of maximum iterations
       
   107 def mandelbrot(start: Complex, end: Complex, max: Int) : Unit = {
       
   108   viewer.clearCanvas(black)
       
   109 
       
   110   // deltas for each grid step
       
   111   val d_x = (end.re - start.re) / W
       
   112   val d_y = (end.im - start.im) / H
       
   113 
       
   114   for (y <- (0 until H).par) {
       
   115     for (x <- (0 until W).par) {
       
   116 
       
   117      val c = start + x * d_x + y * d_y * i
       
   118      val iters = iterations(c, max)
       
   119      val colour = 
       
   120         if (iters == max) black
       
   121         else colours(iters % 16)
       
   122 
       
   123      pixel(x, y, colour)
       
   124     }
       
   125     viewer.updateUI()
       
   126   }
       
   127 }
       
   128 
       
   129 
       
   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 
       
   142 
       
   143 // example 1
       
   144 val exa1 = -2.0 + -1.5 * i
       
   145 val exa2 =  1.0 +  1.5 * i
       
   146 
       
   147 println(s"${time_needed(mandelbrot(exa1, exa2, 1000))} secs")
       
   148 
       
   149 // example 2
       
   150 val exb1 = -0.37465401 + 0.659227668 * i
       
   151 val exb2 = -0.37332410 + 0.66020767 * i
       
   152 
       
   153 //time_needed(mandelbrot(exb1, exb2, 1000))
       
   154 
       
   155 // example 3
       
   156 val exc1 = 0.435396403 + 0.367981352 * i
       
   157 val exc2 = 0.451687191 + 0.380210061 * i
       
   158 
       
   159 //time_needed(mandelbrot(exc1, exc2, 1000))
       
   160 
       
   161 
       
   162 
       
   163 // some more computations with example 3
       
   164 
       
   165 val delta = (exc2 - exc1) * 0.0333
       
   166 
       
   167 println(s"${time_needed(
       
   168   for (n <- (0 to 25))
       
   169      mandelbrot(exc1 + delta * n,
       
   170                 exc2 - delta * n, 1000))} secs")
       
   171 
       
   172 
       
   173 
       
   174 // Larry Paulson's example
       
   175 val exl1 = -0.74364990 + 0.13188170 * i
       
   176 val exl2 = -0.74291189 + 0.13261971 * i
       
   177 
       
   178 //println(s"${time_needed(mandelbrot(exl1, exl2, 1000))} secs")
       
   179 
       
   180 
       
   181 // example by Jorgen Villadsen
       
   182 val exj1 = 0.10284 - 0.63275 * i
       
   183 val exj2 = 0.11084 - 0.64075 * i
       
   184 
       
   185 //time_needed(mandelbrot(exj1, exj2, 1000))
       
   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))