Fill in the methods in the ArrowheadCurve.java file. Your program must not use r
ID: 3801463 • Letter: F
Question
Fill in the methods in the ArrowheadCurve.java file.
Your program must not use recursion.
You must implement each of the methods in ArrowheadCurve.java that have TODO comments.
import edu.support.AnimatedTurtle;
import edu.support.EndWorld;
public class ArrowheadCurve {
/**
* Given the character, return the appropriate production for that character.
* Implements the Sierpinski Arrowhead Curve refer to
* https://en.wikipedia.org/wiki/L-system#Example_5:_Sierpinski_triangle
* (make sure to scroll down to the part about the Sierpinski Arrowhead Curve).
*
* @param c the character to expand.
* @return the appropriate production for the given character.
*/
public static String getProd(char c) {
// TODO: replace with your implementation
return "";
} // end getProd
/**
* Given the String representing the current generation, return a new
* String that is the result of applying the getProd rules to each
* character in the current generation.
* @param curGen a String that is the current generation.
* @return a new String that is the result of applying the
* getProd rules to each character in the current generation.
*/
public static String nextGen(String curGen) {
// TODO: replace with your implementation
return "";
} // end nextGen
/**
* Draw the given current generation using the given turtle and amount
* for forward steps.
*
* @param curGen a String representing the current generation.
* @param t the turtle to use for drawing.
* @param forwardSteps the amount of steps to move on a forward.
*/
public static void draw(String curGen, AnimatedTurtle t, int forwardSteps) {
// TODO: replace with your implementation
} // end draw
/**
* Given the initial generation, and number of reps, advance the initGen
* numRep times. Then create an EndWorld and AnimatedTurtle and
* draw the results using the given forwardSteps value.
*
* @param initGen the initial or starting String
* @param numReps the number of times to run nextGen on initGen
* @param forwardSteps the number of forward steps to use when drawing.
*/
public static void demo(String initGen, int numReps, int forwardSteps) {
// TODO: replace with your implementation
} // end demo
public static void main(String[] args) {
// for your own use
} // end main
} // end ArrowheadCurve
Explanation / Answer
import java.awt.*;
import javax.swing.*;
public class ArrowheadCurve 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)
{
One= new Point(120, 320);
Two = new Point(440, 320);
Three = new Point(280, 40);
drawArrowheadCurve(g, level, pointOne, pointTwo, pointThree);
}
private void drawArrowheadCurve(Graphics g, int lev,
Point p1, Point p2, Point p3)
{
drawSegment(g, lev, p1, p2);
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);
drawSegment(g, lev - 1, pA, pTip);
drawSegment(g, lev - 1, pTip, pB);
drawSegment(g, lev - 1, pB, pTwo);
}
}
}
import java.awt.*;
import javax.swing.*;
public class ArrowheadCurve 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)
{
One= new Point(120, 320);
Two = new Point(440, 320);
Three = new Point(280, 40);
drawArrowheadCurve(g, level, pointOne, pointTwo, pointThree);
}
private void drawArrowheadCurve(Graphics g, int lev,
Point p1, Point p2, Point p3)
{
drawSegment(g, lev, p1, p2);
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);
drawSegment(g, lev - 1, pA, pTip);
drawSegment(g, lev - 1, pTip, pB);
drawSegment(g, lev - 1, pB, pTwo);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.