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