473,387 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Recursion Program Help!?!

My Original program was to.......

Write a Java applet that uses recursion to draw a Koch snowflake fractal
of any given order. A Koch snowflake of order 0 is an equilateral
triangle. To create the next-higher-order fractal, each line segment in
the shape is modified by replacing the middle third with a sharp
protrusion made of two line segments, each having the same length as the
replaced one......

and here is my code for this assignment.....

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.TextField;

public class KochSnowFlake
extends java.applet.Applet
implements java.awt.event.ActionListener
{

private final static int DEPTH_DEFAULT = 0;


private final static double cos60 = 0.5;
private final static double sin60 = Math.sqrt(3)*0.5;
private final static double value2root3over3 = Math.sqrt(3)*2/3;

private int curDepth = DEPTH_DEFAULT;

private boolean doLines = true;

private final Button bDraw = new Button("Click to Draw");
private final TextField tfIterations = new TextField("" +
DEPTH_DEFAULT,3);


private boolean infoShown = false;


private final Panel pGfx =
new Panel() {

public void paint(Graphics g) {
drawPicture(g,new Rectangle(this.getSize()));
}
};


public void init() {
Panel pControls = new Panel();
pControls.setLayout(new FlowLayout());
pControls.add(bDraw);
pControls.add(new Panel());
pControls.add(tfIterations);
pControls.add(new Label("How Many Iterations?"));

setLayout(new BorderLayout());
add(pGfx,BorderLayout.CENTER);
add(pControls,BorderLayout.SOUTH);

bDraw.addActionListener(this);
}


public void drawSegment(Graphics g, Point a, Point b, int depth) {


if(depth==0) {
if(doLines)
g.drawLine(a.x,a.y, b.x,b.y);



}
else {

final Point distance = new Point( (b.x-a.x)/3, (b.y-a.y)/3 );
final Point pa = new Point( a.x+distance.x, a.y+distance.y);
final Point pb = new Point( b.x-distance.x, b.y-distance.y);
final Point pTip = new Point(
pa.x + (int)(distance.x*cos60 + distance.y*sin60),
pa.y + (int)(distance.y*cos60 - distance.x*sin60)
);


final int newDepth = depth-1;
drawSegment(g, a,pa ,newDepth);
drawSegment(g, pa,pTip ,newDepth);
drawSegment(g, pTip,pb ,newDepth);
drawSegment(g, pb,b ,newDepth);
}

}



public void drawPicture(Graphics g, Rectangle bounds) {


g.setColor(Color.white);
g.fillRect(bounds.x,bounds.y, bounds.width,bounds.height);


if(false) {
g.setColor(Color.blue);
g.drawRect(bounds.x,bounds.y, bounds.width-1,bounds.height-1);
}


if( (bounds.width<2) || (bounds.height<2) )
return;

if(!doLines)
return;

final double w,h;
if(bounds.height < (int)(bounds.width * value2root3over3)) {

h = bounds.height;
w = h / value2root3over3;
}
else {

w = bounds.width;
h = w * value2root3over3;
}

final int top = bounds.y + (int)((bounds.height-h)*0.5 + (h*0.25));
final Point p1 = new Point(bounds.x +
(int)((bounds.width-w)*0.5), top);
final Point p2 = new Point(bounds.x +
(int)((bounds.width+w)*0.5), top);
final Point p3 = new Point(
bounds.x + (bounds.width>>1),
bounds.y + (int)((bounds.height+h)*0.5)
);

g.setColor(Color.black);
drawSegment(g, p1,p2, curDepth);
drawSegment(g, p2,p3, curDepth);
drawSegment(g, p3,p1, curDepth);

}

public void actionPerformed(java.awt.event.ActionEvent ae) {
java.lang.Object o = ae.getSource();

try {
curDepth = Integer.parseInt(tfIterations.getText());
}
catch(NumberFormatException e) {
curDepth = DEPTH_DEFAULT;
}
if(curDepth < 0)
curDepth = 0;
drawPicture(pGfx.getGraphics(),new Rectangle(pGfx.getSize()));
}
}

.....my code for that part of the assignment was correct it worked perfectly.....

now i have to fix this program or change it rather to implement a
1-dimensional 90 degree angle variant where the Koch(0) will be a
square....
koch(1) will be a straight line and so on...

I have no idea what to change...i know i have to make it draw 4 line segments rather that just 3 and change some math but i don't know how or where to do that within my assignment...

PLEASE HELP!

THANK YOU!!
Feb 17 '10 #1
0 1855

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Jakle | last post by:
Hi all. Need alittle help here. This is an example from "How to Think Like a Computer Scientist: Learning with Python, Chapter 5". It's an open source ebook, so if you feel like it you can find it...
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
2
by: mdeni | last post by:
I am sorry if this was allready asked or discussed, please redirect me. I have to make the program of postorder traversal of the binary search tree NOT using recursion. I have found many solutinos...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
13
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
3
by: JWest46088 | last post by:
Hello everybody. I'm having a little trouble using recursion. I need to accept input from the user and display the input in a square. It has to be done recursively. I would be able to do it...
8
by: smartbeginner | last post by:
The Problem is CFG grammar generation Eg: Input S->ABC A->bcd B->efg C->hij It should produce the o/p bcdefghij substituting for every Capitals found on the way recursively The C pgm I...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.