Thursday, November 18, 2010

HW7 Solution

Below is my solution to the homework:

import java.awt.Graphics;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Fractal extends JPanel {

 private double length;

 public int numLevels;

 public void paintComponent(Graphics g) {
  g.setColor(Color.black);
  System.out.println("Drawing");
  Point from = new Point(150,200);
  Point to = new Point(450,200);
  length = 0;
//  drawFractalLineDragon(g,from,to,numLevels,Direction.Right);
  drawFractalLine(g,from,to,numLevels);
  
//  from = new Point(100,400);
//  Point top = new Point(300,100); 
//  to = new Point(500,400);
//  drawFractalLineSierpinsky(g,from,top,to,numLevels);
  System.out.println("Length of curve is " + length);
 }
 
 
 /**
  * Draw the Sierpinsky triangle http://en.wikipedia.org/wiki/Sierpinski_triangle
  * @param g
  * @param from bottom-left
  * @param top 
  * @param to bottom-right
  * @param level
  */
 public void drawFractalLineSierpinsky(Graphics g, Point from, Point top, Point to, int level) {
  if (level == 0) {
   g.drawLine(from.getX(), from.getY(), to.getX(), to.getY());
   length += from.getVectorTo(to).getMagnitude();
   g.drawLine(from.getX(), from.getY(), top.getX(), top.getY());
   length += from.getVectorTo(top).getMagnitude();
   g.drawLine(top.getX(), top.getY(), to.getX(), to.getY());
   length += top.getVectorTo(to).getMagnitude();
   return;
  }
  Point midFromTop = from.getMidPoint(top);
  Point midFromTo = from.getMidPoint(to);
  Point midTopTo = top.getMidPoint(to);
  
  drawFractalLineSierpinsky(g,from, midFromTop, midFromTo,level-1);
  drawFractalLineSierpinsky(g,midFromTop, top, midTopTo, level-1);
  drawFractalLineSierpinsky(g,midFromTo, midTopTo, to, level-1);
 }

 /**
  * Draws the Levy C Curve http://en.wikipedia.org/wiki/Levy_C_curve
  * @param g
  * @param from start point
  * @param to end point
  * @param level number of recursive levels.
  */
 public void drawFractalLine(Graphics g, Point from, Point to, int level) {
  if (level == 0) {
   g.drawLine(from.getX(), from.getY(), to.getX(), to.getY());
   length += from.getVectorTo(to).getMagnitude();
   //   System.out.println(from + "  " + to);
   return;
  }
  Point mid = from.getMidPoint(to);
  Vector delta = from.getVectorTo(to);
  delta = delta.getPerpendicularRight();
  delta = delta.multiplyBy(0.5); //half its magnitude
  mid = mid.addVector(delta);
  drawFractalLine(g,from,mid,level-1);
  drawFractalLine(g,mid,to,level-1);
 }

 public enum Direction {Right,Left};

 /**
  * Draws the Dragon curve http://en.wikipedia.org/wiki/Dragon_curve
  * @param g
  * @param from
  * @param to
  * @param level
  * @param d
  */
 public void drawFractalLineDragon(Graphics g, Point from, Point to, int level, Direction d) {
  if (level == 0) {
   g.drawLine(from.getX(), from.getY(), to.getX(), to.getY());
   length += from.getVectorTo(to).getMagnitude();
   //   System.out.println(from + "  " + to);
   return;
  }
  Point mid = from.getMidPoint(to);
  Vector delta = from.getVectorTo(to);
  if (d == Direction.Right) {
   delta = delta.getPerpendicularRight();
  } else {
   delta = delta.getPerpendicularLeft();
  }
  delta = delta.multiplyBy(0.5); //half its magnitude
  mid = mid.addVector(delta);
  drawFractalLineDragon(g,from,mid,level-1,Direction.Right);
  drawFractalLineDragon(g,mid,to,level-1,Direction.Left);
 }

 /**
  * @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: