This exercise will explore the queue and stack data structure implemented with linked lists.
The Koch snowflake can be implemented without recursion by using a queue or a stack. Assume that L is either a queue or a stack, L.add( seg ) adds a segment, and L.remove() removes the segment. For a stack, add is push and remove is pop, for a queue, add is enqueue, and remove is dequeue. A pseudo-code description is:
Add the initial segments, L.add(seg1) ... L.add(segN)
while not L.isEmpty()
seg = L.remove()
if seg is smaller than the minimum segment size then
draw seg, no new segments are created
else
create four new segments according to
the rules for Koch snowflakes
add these new segments to L
Four new segments are created by transforming the "____" segment into "_/\_". See kochSegment from the course notes.
Write a class called KochGenerate that accepts six command line arguments, the arguments are
s/q: s means uses a stack, and q means use a queue
width: width of generate png file
height: height of generate png file
name: name of generate png file
levels: number of levels for the snowflake
corners: number of corners for the initial snowflake
Some examples of invoking the program are:
java KochGenerate s 640 480 k1.png 1 3
creates a triangle, since there are 3 corners and 1 level. The generated image is k1.png with a width of 640 and a height of 480. The segments are stored with a stack.
java KochGenerate s 320 320 k2.png 1 4
creates a square, since there are 4 corners and 1 level. The generated image is k2.png with a width of 320 and a height of 320. The segments are stored with a stack.
java KochGenerate q 500 500 k3.png 6 5
creates a 6 level Koch snowflake starting with a pentagon (5 corners). The generated image is k3.png with a width of 500 and a height of 500. The segments are stored with a queue.
The initial segments can be created by generating a regular polygon (all sides the same size) by noting that the corners will lie on a circle. A triangle has three corners, each corner is 120 degrees from the other corner. A rectangle has four corners that are 90 degrees apart. The corners on a pentagon are 72 degrees apart. The radius of the circle should be the minimum of the width and height minus 10.
In addition to generating a png image file, the program should report the following:
the total number of segments generated, and
the maximum size of the stack or queue.
For example: java KochGenerate s 640 480 k1.png 1 3 should print
Number of segments is 3
Maximum size of stack is 3
For example: java KochGenerate q 640 480 k1.png 1 3 should print
Number of segments is 3
Maximum size of queue is 3
Your program must implement:
the stack of segments with a linked list (no Java API can be used),
the queue of segments with a linked list (no Java API can be used),
error reporting in the command line, where bad conversion, not enough arguments should result in a usage message.
To simplify your code, the classes that implement the queue and the stack should implement the following interface.
interface SegmentsList {
void add( Segment seg );
Segment remove(); // return null if list is empty, otherwise return segment
int totalAdded(); // returns the total number of adds
int maximumSize(); // return the maximum size of the list
}
Segment is a class that represents a line segment. You should place all the code in one file, called KochGenerate.java.
In addition to the paper copy of KochGenerate.java, you should provide print outs of the images produced by:
% java KochGenerate q 500 500 k1.png 1 3
% java KochGenerate q 500 500 k1.png 2 3
% java KochGenerate q 500 500 k1.png 5 3
% java KochGenerate s 500 500 k1.png 5 3
% java KochGenerate q 500 500 k1.png 7 6
% java KochGenerate s 500 500 k1.png 7 6
Right now in my code i have it still set up using recursion, What i really need help with is dealing with the stack and queue stuff, and connecting the lines in my drawing,
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.io.IOException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class TEST extends JPanel{
private static int WINDOW_WIDTH;
private static int WINDOW_HEIGHT;
private static int curLevel = 0;
private static Color BG_COLOR = Color.WHITE;
private static String Title;
private int width = -1;
private int height = -1;
public TEST() {
setPreferredSize(new Dimension( WINDOW_WIDTH, WINDOW_HEIGHT ) );
setBackground( BG_COLOR );
}
protected void paintComponent( Graphics g ) {
super.paintComponent( g );
Graphics2D g2d = (Graphics2D)g;
int w = getWidth();
int h = getHeight();
if ( w != width || h != height ) {
width = w;
height = h;
}
g.setColor( Color.red );
int s = Math.max(width,height) / 5;
kochSegment( g2d, curLevel, w/2, s/2, w-s, h-s );
kochSegment( g2d, curLevel, w-s, h-s, s, h-s );
kochSegment( g2d, curLevel, s, h-s, w/2, s/2 );
}
private void kochSegment(
Graphics2D g, int level, int x1, int y1, int x2, int y2 )
{
level--;
if ( level < 0 ) {
g.drawLine( x1, y1, x2, y2 );
return;
}
int dx = (x2-x1); int dy = (y2-y1);
int dx3 = dx/3; int dy3 = dy/3;
int ox = x1 + dx/2 + dy3; int oy = y1 + dy/2 - dx3;
kochSegment( g, level, x1, y1, x1+dx3, y1+dy3);
kochSegment( g, level, x1+dx3, y1+dy3, ox, oy );
kochSegment( g, level, ox, oy, x1+2*dx3, y1+2*dy3 );
kochSegment( g, level, x1+2*dx3, y1+2*dy3, x2, y2 );
}
public static void runApplication( JPanel app ) {
JFrame frame = new JFrame();
frame.setSize( app.getPreferredSize() );
frame.setTitle( Title );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( app );
frame.setVisible( true );
}
public static void main( String[] args )throws IOException {
String Condition = args[0];
WINDOW_WIDTH = Integer.parseInt(args[1]);
WINDOW_HEIGHT = Integer.parseInt(args[2]);
Title = args[3];
curLevel = Integer.parseInt(args[4]);
int Corners = Integer.parseInt(args[5]);
TEST application = new TEST();
runApplication( application );
}
}
^^ That code will take the 6 arguments, and make the file with proper width, height, name, and level of recursion, everything else though i'm not sure how to do, it's very much sailing over my head. What i need help with is:
- Making the Queue and Stack portions
- Drawing the different sided shapes, pending on what the user inputs.
- The Whole questions is posted at the top.
Thanks for any help