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