Please help with adding the lines of code that will output the Koch fractal plot
ID: 3883492 • Letter: P
Question
Please help with adding the lines of code that will output the Koch fractal plot. Here is the code as is:
The input should be koch(7, matrix, matrix). An example of a matrix p and t is:
Please attach the new updated code with plot capabilities
function p-koch (lev, p,t) if lev= 0, return ; end p = koch (1ev-1, p,t); % get pattern from lower level n = size (p, 2) ; m = size (t, 2) ; pp = repmat (p,n, 1); % initialize coordinates of inserted points for k=2. u-diff (p(:, [k-1, k]), 1,2); % vector between consecutive points a-angle (complex (u (1), u(2))); % angle between vector and horizontal line c-cos (a) ssin(a) R = [q-s,s,c); % rotation matrix g = R*(t*norm (u)); % scale and rotate line segment pp ( : , k-1) = pp ( : , k-1) +q ( : ) ; % shift line segment end "P- [p(:, 1), reshape (pp (3:24m, 1 : n-1), 2, (m-1)*(n-1))); % delete duplicate pointsExplanation / Answer
//package name should write here if your using any IDE(integrated Development Environment)
import java.awt.*;
import javax.swing.*;
//class name is koch and filename should be koch
public class koch extends JApplet {
int level = 0;
public void init() {
//initializing size
setSize(640, 480);
//reading input values for levels
String levelStr = JOptionPane.showInputDialog("Please Enter the Level: ");
level = Integer.parseInt(levelStr);
}
//to plot window
public void paint(Graphics g) {
Point point1 = new Point(120, 320);
Point point2 = new Point(440, 320);
Point point3 = new Point(280, 40);
drawingSnowflake(g, level, point1, point2, point3);
}
//drwaing triangle
private void drawingSnowflake(Graphics g, int l, Point p1, Point p2, Point p3) {
drawingSegments(g, l, p1, p2);
drawingSegments(g, l, p2, p3);
drawingSegments(g, l, p3, p1);
}
//drawing segment
private void drawingSegments(Graphics g, int level, Point pOne, Point pTwo) {
//if only one level
if (level == 0) {
g.drawLine(pOne.x, pOne.y, pTwo.x, pTwo.y);
}
//if level is more than one
if (level >= 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)
);
//calling drawing segment recursively if level is more than one
drawingSegments(g, level - 1, pOne, pA);
drawingSegments(g, level - 1, pA, pTip);
drawingSegments(g, level - 1, pTip, pB);
drawingSegments(g, level - 1, pB, pTwo);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.