CS 171 & 171L Introduction to Computer Science I & Lab Fall 2020

## Homework Assignment 6

### Starting point

Download the source file HWM.pde. It contains a general 2D plotting function. Open it in Processing.

float x1, x2, y1, y2;
float u1, u2, v1, v2;

// set size of window here
public void  settings() {
size(400, 400);
}

// typical setup method
public void setup() {
reset();
noLoop();
testStuff();
}

void testStuff() {
// test functions
println("Testing f function");
Complex z = new Complex(0, 0);
Complex c = new Complex(1, 0);
println("z = " + z);
println("c = " + c);
z = f(z, c);
println("f(z,c) = " + z);
z = f(z, c);
println("f(z,c) = " + z);
z = f(z, c);
println("f(z,c) = " + z);

z = new Complex(0, 0);
c = new Complex(-1, 0);
println("z = " + z);
println("c = " + c);
z = f(z, c);
println("f(z,c) = " + z);
z = f(z, c);
println("f(z,c) = " + z);
z = f(z, c);
println("f(z,c) = " + z);
println();

println("Testing escape function");
z = new Complex(0, 0);
c = new Complex(-1, 0);
int m = 255;
println("escape(z,c,m) should be 2: " + escape(z, c, m));
z = new Complex(0, 0);
c = new Complex(1, 0);
println("escape(z,c,m) should be 255: " + escape(z, c, m));
}

public void reset() {
x1 = -3;
x2 = 3;
u1 = 0;
u2 = width;

y1 = -3;
y2 = 3;
v1 = height;
v2 = 0;
}

// typical draw method
public void draw() {
background(0);
println("Computing...");
float z1 = -1;
float z2 = 1;
float g1 = 0;
float g2 = 255;
// loop over every pixel in the output window
for (int u = 0; u < width; u++) {
for (int v = 0; v < height; v++) {
// convert from (u,v) to (x,y)
float x = linint(u1, x1, u2, x2, u);
float y = linint(v1, y1, v2, y2, v);
float zz = f(x, y);
// map from y range [-1,1] to gray level [0,255]
int g = (int) linint(-1, 0, 1, 255, zz);
stroke(g);
point(u, v);
}
}
println("Done");
}

// linear interpolate x from (x1, y1) and (x2, y2)
float linint(float x1, float y1, float x2, float y2, float x) {
return y1 + (y2-y1)*(x-x1)/(x2-x1);
}

float f(float x, float y) {
return (float)(Math.sin(4*x)*Math.sin(3*y));
}

public void mousePressed() {
// map mouseX and mouseY from (u,v) coordinates
// to (x,y) in math world coordinates
float x = linint(u1, x1, u2, x2, mouseX);
float y = linint(v1, y1, v2, y2, mouseY);

float dx = x2-x1;
float dy = y2-y1;
x1 = x-dx/4;
x2 = x+dx/4;
y1 = y-dy/4;
y2 = y+dy/4;

redraw();
}

public void keyPressed() {
if (key == 'r') {
reset();
redraw();
}
if (key == 's') {
save("picture.jpg");
}
}

Complex f(Complex z, Complex c) {
return new Complex();
}

int escape(Complex z, Complex c, int m) {
return 0;
}

You will need your Complex.pde class file from Lab6 with working complex addition, multiplication, and modulus. See your instructor if you have questions about this. Just like in lab, create a new table called Complex and copy your Complex.pde code there.

(15 points) Consider the function $f(z,c) = z^2 - c$ where $z$ and $c$ are both complex numbers. Write a java function named f which takes two complex numbers as parameters and returns a new complex number that is the square of the first minus the second.

Let $c = 1 = 1 + 0i$ and $z = 0 = 0+0i$, then $f(z, c) = 0^2 - 1 = -1$.

Let $c = 1$ and $z = -1 = -1+0i$, then $f(z, c) = (-1)^2 - 1 = 1 - 1 = 0$.

Let $c = -1$ and $z = 0$, then $f(z, c) = (0)^2 - (-1) = 0 + 1 = 1$.

Let $c = -1$ and $z = 1$, then $f(z, c) = (1)^2 - (-1) = 1 + 1 = 2$.

Let $c = -1$ and $z = 2$, then $f(z, c) = (2)^2 - (-1) = 4 + 1 = 5$.

(15 points) Write a function escape that takes three parameters, the first two (say $z$ and $c$) are complex and the third (say $m$) is an integer. The escape function, $E(z,c,m)$ computes $$z_0 = 0 = 0+0i$$ $$z_1 = f(z_0,c)$$ $$z_2 = f(z_1,c)$$ $$...$$ $$z_n = f(z_{n-1},c)$$ Stop the iteration if either of the following happens:

1. $n=m$
2. $|z_n| >= 2$ (where $|z_n|$ is the modulus of $z_n$)
Return the value of $n$ when your iteration stops.

Let $z=0=0+0i$ and $c = -1 = -1 + 0i$, then $E(0,c,255) = 255$ because the computed values of $f$ will be 0,-1,0,-1,0,-1,...

Let $z=0$ and $c = 1 = i + 0i$, then $E(0,c,255) = 3$ because the computed values of $f$ will be 0,1,2,5,...

(30 points) Loop over each pixel $(u,v)$ of the image. You will need two nested loops. That pixel location $(u,v)$ can be mapped to a point $(x,y)$ in the mathematical world. Consider using $x_\text{1} = -3$, $x_\text{2} = 3$, $y_\text{1} = -3$, and $y_\text{2} = 3$ in your mapping. The point $(x,y)$ can be considered a point $c$ in the complex plane, where $c = x+yi$. In your program construct a complex number for $(x,y)$. Compute the escape function on each point. Color the point $(u,v)$ in the display window based on the escape value with the gray level. Use the point() and stroke() functions to do this. The resulting image should look similar to the image below.

Recall you can use the save() function to write your image to a file.

(20 points extra credit) Consider two colors, C1 and C2, each with RGB compoents. Color the image using linear interpolation between these colors based on the escape function value.