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