cwtests/cw05/mand2.fun
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 03 Dec 2021 21:56:55 +0000
changeset 856 23273e3a120f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
856
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Mandelbrot program (with character constants)
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
val Ymin: Double = -1.3;
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
val Ymax: Double =  1.3;
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
val Ystep: Double = 0.05;  //0.025;
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
val Xmin: Double = -2.1;
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
val Xmax: Double =  1.1;
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
val Xstep: Double = 0.02;  //0.01;
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
val Maxiters: Int = 1000;
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
def m_iter(m: Int, x: Double, y: Double,
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
                   zr: Double, zi: Double) : Void = {
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
  if Maxiters <= m
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
  then print_char(' ') 
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
  else {
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
    if 4.0 <= zi*zi+zr*zr then print_char('0' + (m % 10)) 
23273e3a120f updated
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) 
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
  }
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
};
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
def x_iter(x: Double, y: Double) : Void = {
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
  if x <= Xmax
23273e3a120f updated
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) }
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
  else skip()
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
};
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
def y_iter(y: Double) : Void = {
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  if y <= Ymax
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
  then { x_iter(Xmin, y) ; print_char('\n') ; y_iter(y + Ystep) }
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
  else skip() 
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
};    
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
y_iter(Ymin)