Tuesday, November 9, 2010

HW 7: Fractals

In this homework you will practice recursion by drawing some well-known fractal curves. You have to use the Point and Vector classes I provide below to do your arithmetic. I will explain them in class today. The code below also creates a window and draws a line. You will use that as a starting point for your project.
The first curve you will implement is known as the Levy C Curve and its stages are shown on the first figure. A level 0 curve is just a line between two points:start and end. At level 1, instead of drawing a line between the two points, we
  1. find the midpoint between the two points,
  2. find the vector that is perpendicular to the line, scale it by .5
  3. add the perpendicular vector to the midpoint to get a new point, call it NP
  4. recursively draw a c-curve from the start to NP and another one from NP to the end point.
The above set of steps is pseudo-code for the Java recursive function you will write to draw the C Curve.
The second curve you will implement is the Dragon Curve which is nearly identical to the C curve expect that sometimes we use one perpendicular (right) and sometimes the other (left). You will implement another method which draws the Dragon curve.
The third fractal you will implement is the Sierpinsky triangle. This one is different in that, instead of drawing a line by breaking it up into two pieces, you will be drawing a triangle by breaking it up into three smaller triangles. The recursion will be similar but the drawing code will be different.
Finally, after you get these to run then go back and change them so that they calculate and printout the length of the fractal they have just drawn. That is, the sum of the length of all the little line segments.
This homework is due on Thursday, November 18 @9am. Below is the code for Point and Vector which you must use as well as a short program that draws a line, to get you started.
public class Point {
 
 public double x;
 public double y;
 
 /**
  * We return the nearest int to the current x value.
  * By keeping points as double we increase the precision of the final plot.
  * We only convert to int when absolutely necessary, that is, when plotting on screen.
  * @return
  */
 public int getX() {
  return (int) Math.round(x);
 }
 
 public int getY() {
  return (int) Math.round(y);
 }
 
 public Point(int x, int y) {
  this.x = x;
  this.y = y;
 }
 
 public Point(double x, double y) {
  this.x = x;
  this.y = y;
 }
 
 public String toString() {
  return x + "," + y;
 }
 
 /**
  * Make a vector from this point to other
  * @param other
  * @return a new Vector
  */
 public Vector getVectorTo(Point other) {
  return new Vector(other.x - x,other.y - y);
 }
 
 /**
  * Returns a Point that lies midway on the line between this point and other
  * @param other
  * @return a new Point
  */
 public Point getMidPoint(Point other) {
  return new Point((x + other.x)/ 2, (y +other.y)/ 2);
 }

 /**
  * Move this point by vector v.
  * @param v
  * @return a new Point
  */
 public Point addVector(Vector v) {
  return new Point(x + v.x, y + v.y);
 }
 
}


public class Vector extends Point {

 public Vector(double x, double y) {
  super(x,y);
 }

 /**
  * The magnitude
  * @return the magnitude.
  */
 public double getMagnitude() {
  return Math.sqrt(x*x+y*y);
 }

 /**
  * Creates a new vector that is perpendicular to this one, pointing to the right.
  * @return a new vector
  */
 public Vector getPerpendicularRight() {
  return new Vector(-y, x);
 }

 /**
  * Creates a new vector that is perpendicular to this one, pointing to the left.
  * @return a new vector
  */
 public Vector getPerpendicularLeft() {
  return new Vector(y, -x);
 }

 /**
  * Creates a new vector like this one but scaling both coordinates by d
  * @param d
  * @return a new vector
  */
 public Vector multiplyBy(double d) {
  return new Vector(x*d, y*d);
 }

}

/** Jose M Vidal 
 * If that's not me on the line above then I deserve 0 points on this homework. */

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

public class Fractal extends JPanel{

 public int numLevels;

 public void paintComponent(Graphics g) {
  g.setColor(Color.black);
  System.out.println("Drawing");
  g.drawLine(200, 200, 400, 200);
 }
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  JFrame frame = new JFrame("Fractal Curve");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Fractal panel = new Fractal();

  frame.add(panel);
  frame.setSize(600,600);
  frame.setVisible(true);
  try {
   do {
    String userInput = JOptionPane.showInputDialog("How Many Levels?");
    panel.numLevels = Integer.parseInt(userInput);
    frame.repaint(); //don't forget to re-paint it !
   } while (true);
  } catch (NumberFormatException e) {
   //ignore it, just exit program
   System.exit(0);
  }
 }
}

No comments: