5 Replies - 598 Views - Last Post: 29 October 2012 - 12:57 PM Rate Topic: -----

#1 08cmoss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-October 12

Starting project. Don't know where to start.

Posted 23 October 2012 - 09:23 AM

I am confused on how to start this project. Professor is very unclear. Any help would be awesome.
Corner class:
public class Corner
{
  protected int w;
  protected int d;
  
  public Corner(int w, int d)
  {
    this.w = w;
    this.d = d;
  }
  
  public Corner mid(Corner that)
  {
    return new Corner((this.w + that.w) / 2, (this.d + that.d) / 2);
  }
 
  public int len(Corner that)
  {
    return (int) Math.sqrt(Math.pow((this.w - that.w), 2) 
                         + Math.pow((this.d - that.d), 2));
  }
}


Triangle class:
import javax.swing.*;
import java.awt.*;

/**
 * Triangle class for fractal pattern generation
 */

public class Triangle
{
  // indices for sub-triangles
  public static final int CORNER_ONE = 0;
  public static final int CORNER_TWO = 1;
  public static final int CORNER_THREE = 2;
  public static final int EDGE_ONE = 3;
  public static final int EDGE_TWO = 4;
  public static final int EDGE_THREE = 5;
  
  // smallest perimeter for triangle to be drawn
  public static final int SMALLEST = 30;
  
  private Corner x;
  private Corner y;
  private Corner z;
    
  public Triangle(Corner x, Corner y, Corner z)
  {
    this.x = x;
    this.y = y;
    this.z = z;
  }
  
  public void draw(Graphics screen)
  {
   screen.drawLine(x.w, x.d, y.w, y.d);
   screen.drawLine(y.w, y.d, z.w, z.d);
   screen.drawLine(z.w, z.d, x.w, x.d);
  }
  
  public int size()
  {
    // write your code here
    return 0;
  }
  
  public Triangle getNextLevel(int index)
  {
    Triangle t = null;
    // write your code here
    
    return t;
  }
}



Iterative Class:
import javax.swing.*;
import java.awt.*;
import java.util.*;

/**
 * Applet with fractal pattern repetition using 
 * iterative drawing of triangles within triangles
 */

public class Iterative extends JApplet
{
 public void paint(Graphics screen)
 {
   screen.clearRect(0, 0, this.getWidth(), this.getHeight());
   screen.drawString("Your Name", 0 , 10);
   Corner x = new Corner(0, this.getHeight());
   Corner y = new Corner(this.getWidth(), this.getHeight());
   Corner z = new Corner(this.getWidth() / 2, 0);
   
   drawTriangle(screen, new Triangle(x, y, z));  
 }
 
 
 private void drawTriangle(Graphics screen, Triangle t)
 {
   // add your code here

 }
} 




Recursive class
import javax.swing.*;
import java.awt.*;
import java.util.Stack;

/**
 * Applet with fractal pattern repetition using
 * recursive drawing of triangles within triangles
 */

public class Recursive extends JApplet
{

 public void paint(Graphics screen)
 {
   screen.clearRect(0, 0, this.getWidth(), this.getHeight());
   screen.drawString("Your name", 0 , 10);
   Corner x = new Corner(0, this.getHeight());
   Corner y = new Corner(this.getWidth(), this.getHeight());
   Corner z = new Corner(this.getWidth() / 2, 0);
   
   drawTriangle(screen, new Triangle(x, y, z));  
 }
 
 /**
  * recursive version of drawing
  */

 private void drawTriangle(Graphics screen, Triangle t)
 {
   // Write your code here
  
 }
} 




Any help would be much appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Starting project. Don't know where to start.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Starting project. Don't know where to start.

Posted 23 October 2012 - 09:27 AM

And what is the project goal ? Or class assigment ?

You are not much clearer than your teacher :)
Was This Post Helpful? 0
  • +
  • -

#3 08cmoss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-October 12

Re: Starting project. Don't know where to start.

Posted 23 October 2012 - 09:32 AM

This was the instructions given to us:
The Triangle Class:
The triangle class encapsulates three Corner objects that define the w and d position of each corner of the triangle that it represents. The Triangle class has a constructor that creates a Triangle object with three Corner objects provided as parameters. This overloaded constructor is provided for you.

The getNextLevel(int index) method calculates the corners of a selected (index) lower level triangle and returns it as a Triangle object. You must write the code for this method to create and return one of the lower level triangles shown in the screen capture based on the index supplied. You must figure out how to calculate the three new corners using the midpoints of the edges of the current triangle. There is a mid method in the Corner class that may be useful. Be sure to generate the corners of each of the new triangles so that it is oriented upside down relative to the current triangle.

The size( ) method calculates the size of the triangle and returns the int value. This value is used in each of your applet classes to determine if the current triangle has gotten too small to continue drawing its sub-triangles. You must write the code for this method also. (Base the size on its geometric perimeter.) There is a len method in the Corner class that may be useful.

The Corner class:
The Corner class encapsulates the w and d pixel values on the screen for one corner of a triangle. As shown in the legend on the screen capture, the w (width) dimension goes left to right and the d (down) dimension goes top to bottom. The code for this class is provided to you.

Iterative Class:
In the iterative version of the Applet, you should instantiate the stack that you need to save the Triangle at each level. You push the initial triangle received as a parameter onto the stack to start the iteration.

Then use a while loop with a condition based on whether there is still another triangle in the stack. In the body of the loop, pop the next triangle off the stack. Process it by drawing it and based on its size determine if you are going to draw its next lower level. If so, get each of the six next lower level triangles from the Triangle getNextLevel(int index) method and push it onto the stack. When the body of the loop stops pushing lower level triangles onto the stack, the while loop will complete the drawing of the triangles down to that level and exit. The Triangle class has a symbolic constant SMALLEST that you can use to terminate drawing before the triangles get too small to see.

Recursive Class:
In the Recursive version of the Applet, you do not need to use an explicit stack. Also, there must be NO iteration statements (while, for, or do while). You must use selection statements (if else or switch) and recursive calls only.

The process is similar except that the recursive drawTriangle method calls it self. Process each triangle by drawing it and based on its size determine if you are going to draw its next lower level. If so, call drawTriangle six times once with each of the next lower level triangles obtained from the Triangle getNextLevel(int index) method. When your code returns from each recursive call to drawTriangle, the context of the previous level of recursion will be automatically restored.

Thanks for replying.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7876
  • View blog
  • Posts: 13,367
  • Joined: 19-March 11

Re: Starting project. Don't know where to start.

Posted 23 October 2012 - 09:38 AM

And what is it you're having trouble with?
Questions, I can answer, but my boss would rather I didn't spend all day reading through your source code.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Starting project. Don't know where to start.

Posted 23 October 2012 - 09:40 AM

So seems a lot clearer now
What is the part you have a problem with ?
Your Corner and Triangle class seem OK to me
Was This Post Helpful? 0
  • +
  • -

#6 08cmoss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-October 12

Re: Starting project. Don't know where to start.

Posted 29 October 2012 - 12:57 PM

Ok! I have written much more code to this project, but I have some questions.

Triangle class:
import javax.swing.*;
import java.awt.*;

/**
 * Triangle class for fractal pattern generation
 */

public class Triangle
{
  // indices for sub-triangles
  public static final int CORNER_ONE = 0;
  public static final int CORNER_TWO = 1;
  public static final int CORNER_THREE = 2;
  public static final int EDGE_ONE = 3;
  public static final int EDGE_TWO = 4;
  public static final int EDGE_THREE = 5;
  
  // smallest perimeter for triangle to be drawn
  public static final int SMALLEST = 30;
  
  private Corner x;
  private Corner y;
  private Corner z;
    
  public Triangle(Corner x, Corner y, Corner z)
  {
    this.x = x;
    this.y = y;
    this.z = z;
  }
  
  public void draw(Graphics screen)
  {
   screen.drawLine(x.w, x.d, y.w, y.d);
   screen.drawLine(y.w, y.d, z.w, z.d);
   screen.drawLine(z.w, z.d, x.w, x.d);
  }
  
  public int size()
  {
    // write your code here
    return x.len(y) + y.len(z) + z.len(x);
  }
  
  public Triangle getNextLevel(int index)
  {
    Triangle t = null;
    // write your code here
    //switch statement to select CORNER or EDGEs
    switch(index){
      case CORNER_ONE: 
        t = new Triangle(x.mid(x.mid(y)), y.mid(y.mid(z)), z);
        break;
      case CORNER_TWO:
        t = new Triangle(y.mid(y.mid(x)), y, y.mid(y.mid(z)));
        break;
      case CORNER_THREE:
        t = new Triangle(x, z.mid(z.mid(y)), z.mid(z.mid(x)));
        break;
      case EDGE_ONE:
        t = new Triangle(x.mid(x.mid(y)), y.mid(y.mid(z)), z);
        break;
      case EDGE_TWO:
        t = new Triangle(y.mid(y.mid(x)), y, y.mid(y.mid(z)));
        break;
      case EDGE_THREE:
        t = new Triangle(x, z.mid(z.mid(y)), z.mid(z.mid(x)));
        break;
      default:
        break;
    }
    return t;
  }
}



Iterative Class:
import javax.swing.*;
import java.awt.*;
import java.util.*;

/**
 * 
 * Applet with fractal pattern repetition using 
 * iterative drawing of triangles within triangles
 */

public class Iterative extends JApplet
{
 public void paint(Graphics screen)
 {
   screen.clearRect(0, 0, this.getWidth(), this.getHeight());
   screen.drawString("Your Name", 0 , 10);
   Corner x = new Corner(0, this.getHeight());
   Corner y = new Corner(this.getWidth(), this.getHeight());
   Corner z = new Corner(this.getWidth() / 2, 0);
   
   drawTriangle(screen, new Triangle(x, y, z));  
 }
 
 /** 
  * iterative version of drawing
  * Note: It can work with either stack or queue for saving triangles
  * in five places in the code below: comment/uncomment one or the other ***
  * The only difference is in the order that sub-triangles are drawn.
  */
 
 private void drawTriangle(Graphics screen, Triangle t)
 {
   // add your code here
   //create a stack of triangles
   Stack<Triangle> myStack = new Stack<Triangle>();
   myStack.push(t);
   
   while(!myStack.isEmpty())
   {
     t = myStack.pop();
     //draw triangle on top of stack
     //drawTriangle(screen);
     if(t > Triangle.SMALLEST){
      //draw six sub triangles 
       // get the next six from getNextLevel(int index)
     }
   }
 }
} 




Corner class:
/**
 * 
 * Corner class for Triangles in fractal pattern generation
 */

public class Corner
{
  protected int w;
  protected int d;
  
  public Corner(int w, int d)
  {
    this.w = w;
    this.d = d;
  }
  
  public Corner mid(Corner that)
  {
    return new Corner((this.w + that.w) / 2, (this.d + that.d) / 2);
  }
 
  public int len(Corner that)
  {
    return (int) Math.sqrt(Math.pow((this.w - that.w), 2) 
                         + Math.pow((this.d - that.d), 2));
  }
}


How do I pass screen to the draw method on my triangle?
Correctly check if you are at the Triangle.SMALLEST? That is the smallest you can draw.
How would I then draw the six subtriangles after that check?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1