progs/mandelbrot.scala
changeset 187 4d300409f2fe
parent 186 f211d9cb856e
child 189 ff815ca0bbcf
equal deleted inserted replaced
186:f211d9cb856e 187:4d300409f2fe
       
     1 // Mandelbrot pictures
       
     2 //   see https://en.wikipedia.org/wiki/Mandelbrot_set
       
     3 
     1 import java.awt.Color
     4 import java.awt.Color
     2 import java.awt.Dimension
     5 import java.awt.Dimension
     3 import java.awt.Graphics
     6 import java.awt.Graphics
     4 import java.awt.Graphics2D
     7 import java.awt.Graphics2D
     5 import java.awt.image.BufferedImage
     8 import java.awt.image.BufferedImage
    17                                  this.re * that.im + that.re * this.im)
    20                                  this.re * that.im + that.re * this.im)
    18   def *(that: Double) = Complex(this.re * that, this.im * that)
    21   def *(that: Double) = Complex(this.re * that, this.im * that)
    19   def abs() = Math.sqrt(this.re * this.re + this.im * this.im)
    22   def abs() = Math.sqrt(this.re * this.re + this.im * this.im)
    20 }
    23 }
    21 
    24 
       
    25 // to allow the notation n + m * i
    22 object i extends Complex(0, 1)
    26 object i extends Complex(0, 1)
       
    27 implicit def double2complex(re: Double) = Complex(re, 0)
    23 
    28 
    24 implicit def double2complex(re: Double): Complex = Complex(re, 0)
       
    25 
    29 
    26 // some customn colours for the "sliding effect"
    30 // some customn colours for the "sliding effect"
    27 val colours = List(
    31 val colours = List(
    28   new Color(66, 30, 15),    new Color(25, 7, 26),
    32   new Color(66, 30, 15),    new Color(25, 7, 26),
    29   new Color(9, 1, 47),      new Color(4, 4, 73),
    33   new Color(9, 1, 47),      new Color(4, 4, 73),
    82   def pred(z: Complex) = z.abs < 2    // exit condition
    86   def pred(z: Complex) = z.abs < 2    // exit condition
    83   Stream.iterate(0.0 * i, max)(next).takeWhile(pred).size
    87   Stream.iterate(0.0 * i, max)(next).takeWhile(pred).size
    84 }
    88 }
    85 
    89 
    86 // main function 
    90 // main function 
       
    91 //    start and end are the upper-left and lower right corners 
       
    92 //    max is the number of maximum iterations
    87 def mandelbrot(start: Complex, end: Complex, max: Int) : Unit = {
    93 def mandelbrot(start: Complex, end: Complex, max: Int) : Unit = {
    88   viewer.clearCanvas(black)
    94   viewer.clearCanvas(black)
    89   
    95   
    90   // deltas for each grid step 
    96   // deltas for each grid step 
    91   val d_x = (end.re - start.re) / W
    97   val d_x = (end.re - start.re) / W
    92   val d_y = (end.im - start.im) / H
    98   val d_y = (end.im - start.im) / H
    93    
    99    
    94   for (y <- (0 until H).par) {
   100   for (y <- (0 until H)) {
    95     for (x <- (0 until W).par) {
   101     for (x <- (0 until W)) {
    96     
   102     
    97      val c = start + 
   103      val c = start + 
    98       (x * d_x + y * d_y * i)
   104       (x * d_x + y * d_y * i)
    99      val iters = iterations(c, max) 
   105      val iters = iterations(c, max) 
   100      val col = 
   106      val col = 
   104      pixel(x, y, col)
   110      pixel(x, y, col)
   105     }
   111     }
   106     viewer.updateUI()
   112     viewer.updateUI()
   107   }   
   113   }   
   108 }
   114 }
       
   115 
   109 
   116 
   110 // Examples
   117 // Examples
   111 //==========
   118 //==========
   112 
   119 
   113 //for measuring time
   120 //for measuring time