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

Homework Assignment 6

Starting point

Download the source file Plotter.java. It contains a general 2D plotting function. Open it in jGRASP. It will not work in DrJava.


// David A. Reimann
// Example  code for using Processing from jGRASP
// Note: this does not work quite right with DrJava!

// Note you must also set the classpath to locate Processing's core.jar file
// core.jar located in Path-to-processing-3.2.1\core\library on windows
// where Path-to-processing-3.2.1 is where processing is located on your computer
// Macs may need to do something different



// imports always needed

import processing.core.*; 


// Plotter is the name of this class and code
// must be stored in a file named "Plotter.java"
// Change this name to suit your needs
public class Plotter extends PApplet {
  
// Plotter must also be changed below
   static public void main(String args[]) {
      PApplet.main(new String[] {"Plotter" });  // change lines to match class name
   }
  
  // set size of window here
   public void  settings() {
      size(500,500); 
   }
    
  // typical setup method
   public void setup() {
      noLoop();
   }
  
  
  // typical draw method
   public void draw() {
      float x1 = -10;
      float x2 = 10;
      float u1 = 0;
      float u2 = width;
   
      float y1 = -10;
      float y2 = 10;
      float v1 = height;
      float v2 = 0;
   
      float z1 = -1;
      float z2 = 1;
      float g1 = 0;
      float g2 = 255;
      
      for (int u = 0;  u < width;  u++) {
         for (int v = 0;  v < height;  v++) {
            float x = linint(u,u1,u2,x1,x2);
            float y = linint(v,v1,v2,y1,y2);
            float z = f(x,y);
            int g = (int)linint(z,z1,z2,g1,g2);
            stroke(g);
            point(u,v);
            }
         } 
   }
  
  

   float linint(float t, float a, float b, float c, float d) {
      return c + (t-a)*(d-c)/(b-a);
   }
   
   float f(float x, float y) {
      return (float)(Math.sin(x)*Math.sin(y));
   }
   
}

Using Processing Libraries from Java

You will need to use jGRASP for this project and tell jGRASP to use the Processing libraries. See the video on how to do this. Once this is done, you should be able to compile and run the program above.

Classes and Methods

You will need your Complex.java class file with working complex addition, multiplication, and modulus.

(10 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$.

(10 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$$ $$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$ and $c = -1$, 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$, then $E(0,c,255) = 3$ because the computed values of $f$ will be 0,1,2,5,...

Loop over a grid of points $c$ in the complex plane. Compute the escape function on each point. Color the point in the image based on the escape value with the gray level. Use the point() function to do this. The resulting image should look similar to the image below.

(10 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.


Copyright © 2017, David A. Reimann. All rights reserved.