solution/cw5/mand.fun
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 28 Oct 2022 09:08:13 +0100
changeset 893 54a483a33763
parent 864 b5b1bc0a603b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Mandelbrot program (without character constants)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
val Ymin: Double = -1.3;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
val Ymax: Double =  1.3;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
val Ystep: Double = 0.05;  //0.025;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
val Xmin: Double = -2.1;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
val Xmax: Double =  1.1;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
val Xstep: Double = 0.02;  //0.01;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
val Maxiters: Int = 1000;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
def m_iter(m: Int, x: Double, y: Double,
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
                   zr: Double, zi: Double) : Void = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
  if Maxiters <= m
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
  then print_star() 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
  else {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
    if 4.0 <= zi*zi+zr*zr then print_space() 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
    else m_iter(m + 1, x, y, x+zr*zr-zi*zi, 2.0*zr*zi+y) 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
};
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
def x_iter(x: Double, y: Double) : Void = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
  if x <= Xmax
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
  then { m_iter(0, x, y, 0.0, 0.0) ; x_iter(x + Xstep, y) }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
  else skip()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
};
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
def y_iter(y: Double) : Void = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  if y <= Ymax
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
  then { x_iter(Xmin, y) ; new_line() ; y_iter(y + Ystep) }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
  else skip() 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
};    
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
y_iter(Ymin)