progs/mandelbrot.scala
changeset 464 73ced118f73d
parent 444 7a0735db4788
child 470 86a456f8cb92
equal deleted inserted replaced
463:0315d9983cd0 464:73ced118f73d
     1 // Mandelbrot pictures
     1 // Mandelbrot pictures
     2 //=====================
     2 //=====================
     3 //
     3 //
     4 //   see https://en.wikipedia.org/wiki/Mandelbrot_set
     4 //   see https://en.wikipedia.org/wiki/Mandelbrot_set
     5 // 
     5 // 
     6 // under scala 2.13.XX needs to be called with
     6 // under scala 3.2.2 needs to be called with
     7 // 
     7 // 
     8 // scala -cp scala-parallel-collections_2.13-0.2.0.jar mandelbrot.scala
     8 // scala -cp scala-parallel-collections_3-1.0.4.jar
       
     9 //
       
    10 // !! UPDATE: On my faster Mac-M1 machine the times
       
    11 // !! are ca. 4 secs for the sequential version and
       
    12 // !! around 0.7 secs for the par-version.
       
    13 
     9 
    14 
    10 import java.awt.Color
    15 import java.awt.Color
    11 import java.awt.Dimension
    16 import java.awt.Dimension
    12 import java.awt.Graphics
    17 import java.awt.Graphics
    13 import java.awt.Graphics2D
    18 import java.awt.Graphics2D
    29   def abs() = Math.sqrt(this.re * this.re + this.im * this.im)
    34   def abs() = Math.sqrt(this.re * this.re + this.im * this.im)
    30 }
    35 }
    31 
    36 
    32 // to allow the notation n + m * i
    37 // to allow the notation n + m * i
    33 object i extends Complex(0, 1)
    38 object i extends Complex(0, 1)
    34 implicit def double2complex(re: Double) = Complex(re, 0)
    39 
       
    40 // implicit conversion from Doubles to Complex
       
    41 given Conversion[Double, Complex] = Complex(_, 0)
    35 
    42 
    36 
    43 
    37 // some customn colours for the "sliding effect"
    44 // some customn colours for the "sliding effect"
    38 val colours = List(
    45 val colours = List(
    39   new Color(66, 30, 15),    new Color(25, 7, 26),
    46   new Color(66, 30, 15),    new Color(25, 7, 26),
   102   
   109   
   103   // deltas for each grid step 
   110   // deltas for each grid step 
   104   val d_x = (end.re - start.re) / W
   111   val d_x = (end.re - start.re) / W
   105   val d_y = (end.im - start.im) / H
   112   val d_y = (end.im - start.im) / H
   106    
   113    
   107   for (y <- (0 until H).par) {
   114   for (y <- (0 until H)) {
   108     for (x <- (0 until W).par) {
   115     for (x <- (0 until W)) {
   109     
   116     
   110      val c = start + 
   117      val c = start + 
   111       (x * d_x + y * d_y * i)
   118       (x * d_x + y * d_y * i)
   112      val iters = iterations(c, max) 
   119      val iters = iterations(c, max) 
   113      val col = 
   120      val colour = 
   114        if (iters == max) black 
   121        if (iters == max) black 
   115        else colours(iters % 16)
   122        else colours(iters % 16)
   116 
   123 
   117      pixel(x, y, col)
   124      pixel(x, y, colour)
   118     }
   125     }
   119     viewer.updateUI()
   126     viewer.updateUI()
   120   }   
   127   }   
   121 }
   128 }
   122 
   129