progs/mandelbrot.scala
changeset 266 ca48ac1d3c3e
parent 265 59779ce322a6
child 309 b192bc772613
--- a/progs/mandelbrot.scala	Sat Jun 22 08:39:52 2019 +0100
+++ b/progs/mandelbrot.scala	Wed Jul 24 14:22:06 2019 +0100
@@ -1,5 +1,9 @@
 // Mandelbrot pictures
+//
 //   see https://en.wikipedia.org/wiki/Mandelbrot_set
+// 
+// under scala 2.13 needs to be called with 
+// scala -cp `coursier fetch -p org.scala-lang.modules:scala-parallel-collections_2.13:0.2.0` mandelbrot.scala
 
 import java.awt.Color
 import java.awt.Dimension
@@ -10,7 +14,7 @@
 import javax.swing.JPanel
 import javax.swing.WindowConstants
 import scala.language.implicitConversions    
-
+import scala.collection.parallel.CollectionConverters._
 
 // complex numbers
 case class Complex(val re: Double, val im: Double) { 
@@ -39,7 +43,7 @@
   new Color(255, 170, 0),   new Color(204, 128, 0),
   new Color(153, 87, 0),    new Color(106, 52, 3))
 
-// the viewer panel with a canvas
+// the viewer panel with an image canvas
 class Viewer(width: Int, height: Int) extends JPanel {
   val canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
   
@@ -56,7 +60,7 @@
   }  
 }
 
-// initialising the viewer
+// initialising the viewer panel
 def openViewer(width: Int, height: Int) : Viewer = {
   val frame = new JFrame("XYPlane")
   val viewer = new Viewer(width, height)
@@ -68,28 +72,28 @@
   viewer
 }
 
-// some hardcoded data
+// some hardcoded parameters
 val W = 900   // width
 val H = 800   // height
 val black = Color.black
 val viewer = openViewer(W, H)
 
-// drawing a pixel on the canvas
+// draw a pixel on the canvas
 def pixel(x: Int, y: Int, color: Color) = 
   viewer.canvas.setRGB(x, y, color.getRGB())
 
 
-// calculating the number of iterations using lazy streams
+// calculates the number of iterations using lazy lists (streams)
 //   the iteration goes on for a maximum of max steps,
 //   but might leave early when the pred is satisfied
 def iterations(c: Complex, max: Int) : Int = {
   def next(z: Complex) = z * z + c    
   def pred(z: Complex) = z.abs < 2    // exit condition
-  Stream.iterate(0.0 * i, max)(next).takeWhile(pred).size
+  LazyList.iterate(0.0 * i, max)(next).takeWhile(pred).size
 }
 
 // main function 
-//    start and end are the upper-left and lower right corners 
+//    start and end are the upper-left and lower-right corners, 
 //    max is the number of maximum iterations
 def mandelbrot(start: Complex, end: Complex, max: Int) : Unit = {
   viewer.clearCanvas(black)
@@ -145,6 +149,8 @@
 
 //time_needed(mandelbrot(exc1, exc2, 1000))
 
+
+
 // some more computations with example 3
 
 val delta = (exc2 - exc1) * 0.0333
@@ -155,6 +161,7 @@
      mandelbrot(exc1 + delta * n, 
                 exc2 - delta * n, 100)) 
 */
+
 /*
 time_needed(
   for (n <- (0 to 12))