Home / Expert Answers / Computer Science / a-sierpinski-triangle-is-a-recursive-structure-of-triangles-within-triangles-the-nbsp-order-nbsp-of-pa293

(Solved): A Sierpinski triangle is a recursive structure of triangles within triangles. The order of ...



A Sierpinski triangle is a recursive structure of triangles within triangles. The order of the Sierpinski triangle refers to how many levels of nested triangles to draw. There is one outer triangle that points upward. Then we can draw a downward facing triangle within it. This then creates three new upward facing triangles (above, left, and right of the downward one), in which we can repeat this process. For example:

Write a program that takes the order as a command-line argument (e.g., java Sierpinski 2) and draws the corresponding Sierpinski triangle. You can use Sierpinski.java as a starting point. Your program should use recursion. Due to integer rounding, your images might look slightly different than those above.

To draw a triangle, you may want to use the g.drawPolygon(xs, ys, n) method, where xs is a list of x-coordinates, ys a list of y-coordinates, and n the number of vertices.

OUTPUT

Javac Sierpinski.java

java Sierpinski 2 ---> should give order 2 figure 



//Start of the code using this

import java.awt.*;
import javax.swing.*;

public class Sierpinski
{
    public int n;
    public int size;

    public void paint(Graphics g)
    {
        // TODO
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Sierpinski drawing = new Sierpinski();
        drawing.n = Integer.parseInt(args[0]);
        drawing.size = 700;
        drawing.setSize(drawing.size, drawing.size);
        frame.add(drawing);
        frame.pack();
        frame.setVisible(true);
    }
}


We have an Answer from Expert

View Expert Answer

Expert Answer


ANSWER : SIERPINSKI TRIANGLE: The Sierpinski triangle exercise exemplifies the basic ideas of fractals, showing how a pattern may repeat itself repeatedly at various scales and how this complex shape can be created using
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe