



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Methods ā Algorithms
Spring 2009
1
I (^) Reading: Textbook, Chapter 5, 6.
I (^) Prelab & Lab
I (^) Attendance
I (^) Lab 8, Exercise 3: set graphics window size to 700 by 700 pixels
2
I (^) Suppose you have learned how to use a hammer, sander, drill, and saw, and to apply polyurethane finishes to wood ā by building a small bird house.
I (^) Now, suppose further that you have been given the task of building a large rollātop desk. And that you will be judged on the sturdiness, usefulness, and elegance of this desk.
I (^) How would you begin? Some sort of plan is needed.
I (^) In programming, this plan is called an algorithm.
3
I (^) Algorithms express the logic of a solution strategy ā the steps necessary to accomplish a task.
I (^) A programming problem should be broken down into logical subāproblems by finding a general algorithm ā one that outlines your overall solution strategy.
I (^) The algorithms for these subāproblems are then further refined, until they are easily implementable.
I (^) These specific algorithms should then be implemented as methods, while the general algorithm provides the plan for the order and way in which these methods will be used to solve the given problem.
4
I (^) Methods are important in programming because
I (^) Algorithms for solving a particular problem can vary widely in their efficiency ā it makes sense to think carefully when developing an algorithm because making a bad choice can be extremely costly.
I (^) read the textbook, notes, etc.
I (^) javadoc: jtf.acm.org/javadoc/student/index.html
I netbeans and acmLibrary inspection
I (^) search the internet for information (java.sun.com)
Is the class part of acmLibrary or another library, such as java.awt?
I (^) The acm.graphics package provides the class GPoint which allows us to combine two double values into a single encapsulated unit.
I (^) A GPoint can represent a point in the graphics window, a point in the Euclidean plane, or just a couple of related values.
I (^) The primary advantage of encapsulating two individual values into a composite object is that the object can then be passed from one method to another as a single entity, via the return statement.
7
new GPoint(x, y) Creates a new GPoint object containing the coordinate values x and y.
object.getX() returns the x component of a GPoint
object.getY() returns the y component of a GPoint
object.setLocation(x, y) Changes the coordinates of the object to the point (x, y)
object.translate(dx, dy) Modifies the GPoint object by adding dx to its x coordinate and dy to its y coordinate.
8
GPoint p = new GPoint(x, y);
GPoint WorldCenter = new GPoint(0.0, 0.0);
GPoint JuliaTerm = new GPoint(-0.9, 0.12);
GPoint JuliaTerm = new GPoint(1.0, 0.0);
Methods are able to return a GPoint object, for example: return p; // as created above or return new GPoint(Re, Im);
9
Given the method header:
public Color JuliaColor(GPoint p)
we can gain access to pās x and y values by:
GPoint Z = new GPoint(p.getX(), p.getY());
10
I (^) To every point (a, b) in the plane we can associate a function of two variables ā referred to as the Julia Map for (a, b) ā denoted by F(a,b).
I (^) F(a,b) is described by the formula:
F(a,b)(x, y ) = (x^2 ā y 2 + a, 2 xy + b).
I (^) For example:
F(ā 1 ,0)(x, y ) = (x^2 ā y 2 + (ā1), 2 xy + 0) = (x^2 ā y 2 ā 1 , 2 xy )
I (^) If we start with a point P 0 = (x 0 , y 0 ) and compute P 1 = F(a,b)(P 0 ), P 2 = F(a,b)(P 1 ), P 3 = F(a,b)(P 2 ), P 4 = F(a,b)(P 3 ) etc., we observe two qualitatively different types of behavior.
I The sequence of points P 0 , P 1 , P 2 , P3,... either
I (^) starts to get farther and farther away from the origin, or
I (^) the sequence stays pretty close to the origin
I (^) The third lab exercise this week is to complete a skeleton program to generate pictures of Julia sets and their surroundings.
I (^) A grid of tiny square blocks (much like the checker boards weāve already seen) will be drawn in a graphics window.
I (^) These blocks represent a grid of points in a square region of the Cartesian plane.
I (^) Each block is to be colored according to how the Julia set coloring algorithm would color the corresponding points in the plane. (The Julia coloring code is provided for you.)
19
I (^) In order to know what color a block in the graphics window should be, we need to know the Julia Color of itās corresponding point in the Cartesian plane.
I (^) The main idea in this problem, then, is to map each block in the graphics window to a corresponding point in the āworldā region that we are representing.
I (^) This is accomplished by the ScreenToWorld() method.
I (^) ScreenToWorld() is passed a GPoint representing a point in the graphics window, and it returns a GPoint representing the coordinates of the corresponding point in the world region.
20
The method Norm() is used by JuliaColor(), and the method NextPoint() is just the Julia map mentioned earlier.
21
// Size of graphics window public static final double ScreenSize = 700;
// Size of the real world (Euclidean plane) region public static final double WorldSize = 4.0;
// Center of the world region public static final GPoint WorldCenter = new GPoint(0.0, 0.0);
// Number of rows and columns in screen grid public static final int GridSize = 350;
22
// size of squares public static final double BlockSize = ScreenSize / GridSize;
// Number of colors used for the display public static final int MaxColors = 11;
// Maximum number of iterations before a // number is declared in the Julia set public static final int MaxIterations = 40;
// Distance beyond which a point will not // return public static final double Threshold = 2.0;
// the position of the world point corresponding // to the screen point
public GPoint ScreenToWorld(GPoint p) { // Replace this code return new GPoint(0.0, 0.0); }
// position of block at row, col
public GPoint BlockCorner(int row, int col) { // Replace this code return new GPoint(0.0, 0.0); }
public class JuliaSet extends DualSliderProgram {
public void init() { setSize(700, 700); super.init(); setRangeA(-75, 75); setRangeB(0, 100); }
public void run() { // the a and b of F_a,b(x,y) double a = getA() / 100.0; double b = getB() / 100.0; GPoint JuliaTerm = new GPoint(a, b);
// add code to draw Julia blocks here
} }
25
4 units
(0, 0)
(0, 0)
700 pixels
700 pixels 4 units
graphics window
Cartesian plane
26
4 units
(0, 0)
700 pixels
700 pixels 4 units
graphics window
(gx, gy)
plane
Cartesian
(cx, cy)
graphics window
WorldSize
WorldSize
ScreenSize
ScreenSize
(gx, gy) (cx, cy)
(0, 0)
Cartesian plane
"Real World"