Question
use recursive drawing to solve this problem

A figure even more remarkable than the Koch curve described in the previous project is the Koch snowflake or Koch star. You can create this snowflake by applying the three-step recursive process used to create a Koch curve to each side of an equilateral triangle rather than to only one line segment: △★本 Use java to write a recursive program that creates a Koch snowflake.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.*;

import javax.swing.*;

public class Snowflake extends JApplet

{

int level = 0;

public void init()

{

setSize(640, 480);

String levelStr = JOptionPane.showInputDialog

("Enter the recursion depth: ");

level = Integer.parseInt(levelStr);

}

public void paint(Graphics g)

{

Point pointOne = new Point(120, 320);

Point pointTwo = new Point(440, 320); // reusing Points from example program * 2;

Point pointThree = new Point(280, 40);

drawSnowflake(g, level, pointOne, pointTwo, pointThree);

}

private void drawSnowflake(Graphics g, int lev,

Point p1, Point p2, Point p3)

{

drawSegment(g, lev, p1, p2); // draw 3 lines to make a triangle

drawSegment(g, lev, p2, p3);

drawSegment(g, lev, p3, p1);

}

private void drawSegment(Graphics g, int lev, Point pOne, Point pTwo)

{

if (lev == 0)

{

g.drawLine(pOne.x, pOne.y, pTwo.x, pTwo.y);

}

if (lev >= 1){

Point distance = new Point( (pTwo.x-pOne.x)/3, (pTwo.y-pOne.y)/3 );

Point pA = new Point( pOne.x+distance.x, pOne.y+distance.y);

Point pB = new Point( pTwo.x-distance.x, pTwo.y-distance.y);

double sin60 = -0.866025403784438646763723170752936183471402626905190;

Point pTip = new Point(

pA.x + (int)(distance.x* 0.5 + distance.y*sin60),

pA.y + (int)(distance.y* 0.5 - distance.x*sin60)

);

drawSegment(g, lev - 1, pOne, pA); // if level 1 or higher,

drawSegment(g, lev - 1, pA, pTip); // recursively call self 4 times

drawSegment(g, lev - 1, pTip, pB);

drawSegment(g, lev - 1, pB, pTwo);

}

}

}

Add a comment
Know the answer?
Add Answer to:
use recursive drawing to solve this problem A figure even more remarkable than the Koch curve...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT