1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
aleksklad [387]
2 years ago
8

The Polish mathematician Wacław Sierpiński described the pattern in 1915, but it has appeared in Italian art since the 13th cent

ury. Though the Sierpinski triangle looks complex, it can be generated with a short recursive function. Your main task is to write a recursive function Sierpinski() that plots a Sierpinski triangle of order n to standard drawing. Think recursively: Sierpinski() should draw one filled equilateral triangle (pointed downwards) and then call itself recursively three times (with an appropriate stopping condition). It should draw 1 filled triangle for n = 1; 4 filled triangles for n = 2; and 13 filled triangles for n = 3; and so forth.
API specification:
When writing your program, exercise modular design by organizing it into four functions, as specified in the following API:

public class Sierpinski {

// Height of an equilateral triangle whose sides are of the specified length.
public static double height(double length)

// Draws a filled equilateral triangle whose bottom vertex is (x, y)
// of the specified side length.
public static void filledTriangle(double x, double y, double length)

// Draws a Sierpinski triangle of order n, such that the largest filled
// triangle has bottom vertex (x, y) and sides of the specified length.
public static void Sierpinski(int n, double x, double y, double length)

// Takes an integer command-line argument n;
// draws the outline of an equilateral triangle (pointed upwards) of length 1;
// whose bottom-left vertex is (0, 0) and bottom-right vertex is (1, 0); and
// draws a Sierpinski triangle of order n that fits snugly inside the outline.
public static void main(String[] args)
}

Restrictions: You may not change either the scale or size of the drawing window.
Observe the following rules:

DO NOT use System.exit().
DO NOT add the project or package statements.
DO NOT change the class name.
DO NOT change the headers of ANY of the given methods.
DO NOT add any new class fields.
ONLY display the result as specified by the example for each problem.
DO NOT print other messages, follow the examples for each problem.
USE StdIn, StdOut, and StdDraw libraries.
Computers and Technology
1 answer:
soldi70 [24.7K]2 years ago
7 0

Answer:

/ Sierpinski.java

public class Sierpinski {

     // method to find the height of an equilateral triangle with side length =

     // length

     public static double height(double length) {

           // formula= length*sqrt(3)/2

           double h = (length * Math.sqrt(3)) / 2.0;

           return h;

     }

     // method to draw a filled triangle (pointed downwards) with bottom vertex

     // x,y

     public static void filledTriangle(double x, double y, double length) {

           // finding height

           double h = height(length);

           // filling triangle as a polygon

           // passing x, x-length/2, x+length/2 as x coordinates

           // and y, y+h, y+h as y coordinates

           StdDraw.filledPolygon(new double[] { x, x - (length / 2.0),

                       x + (length / 2.0) }, new double[] { y, y + h, y + h });

     }

     // method to draw an n level sierpinski triangle

     public static void sierpinski(int n, double x, double y, double length) {

           // checking if n is above 0 (base condition)

           if (n > 0) {

                 // drawing a filled triangle with x, y as bottom coordinate, length

                 // as side length

                 filledTriangle(x, y, length);

                 // drawing the triangle(s) on the top

                 sierpinski(n - 1, x, y + height(length), length / 2);

                 // drawing the triangle(s) on the left side

                 sierpinski(n - 1, x - (length / 2.0), y, length / 2);

                 // drawing the triangle(s) on the right side

                 sierpinski(n - 1, x + (length / 2.0), y, length / 2);

           }

     }

     public static void main(String[] args) {

           // parsing first command line argument as integer, if you dont provide

           // the value while running the program, this program will cause

           // exception.

           int n = Integer.parseInt(args[0]);

           // length of the outline triangle

           double length = 1;

           // finding height

           double h = height(length);

           // drawing a triangle (pointed upwards) to represent the outline.

           StdDraw.polygon(new double[] { 0, length / 2, length }, new double[] {

                       0, h, 0 });

           // drawing n level sierpinski triangle(s) with length / 2, 0 as x,y

           // coordinates and length / 2 as initial side length

           sierpinski(n, length / 2, 0, length / 2);

     }

}

You might be interested in
While working on a forty-slide PowerPoint presentation, a user needs to quickly look over the six slides that have orange backgr
guapka [62]

Answer:

B

Explanation:

8 0
3 years ago
Which of the following can a cell contain?
Ksju [112]
The answer is choice D-All of these :)
8 0
3 years ago
Read 2 more answers
You want to securely erase data from your hard drive what can you use to do this and what is the process called
tankabanditka [31]
You could Factory Reset a computer or you could smash it with  a hammer
7 0
3 years ago
Read 2 more answers
The use of IDPS sensors and analysis systems can be quite complex. One very common approach is to use an open source software pr
lawyer [7]

Answer:

IDP sensors can be complicated.An open-source software program called snort program.

Explanation:

Snort is an open-source network intrusion detection system (IDS) and prevention system. It is created in 1998 by Martin. Snort's open source network-based can perform analysis and packet logging on an Internet Protocol network.

The snort program can be used to detect probes or attacks. Snort configured three modes

sniffer

pocket logger

network intrusion detection.

6 0
3 years ago
Read 2 more answers
The physical parts of the computer that you can see and touch are called______.
Tatiana [17]
Physical parts of computer: Computer Hardware
5 0
2 years ago
Other questions:
  • In 4-bit sign magnitude representation, what is the binary encoding of the number -5?
    15·1 answer
  • A student opens a recently purchased software package and begins reading the enclosed materials. What information might be inclu
    6·1 answer
  • A=1/2h(a+b) solve for h
    6·1 answer
  • ou use productivity apps on your iPad tablet device while traveling between client sites. You're concerned that you may lose you
    11·1 answer
  • The owner of a candle shop has asked for your help. The shop sells three types of candles as shown below:
    9·1 answer
  • Write a program, TwoDimentionalGrid. Ask the user to enter the size of the 2 dimensional array. Here we are not doing any input
    13·1 answer
  • 1. What is hydrolics?​
    6·1 answer
  • 9.
    8·1 answer
  • . Question 2 Fill in the blank: R Markdown notebooks can be converted into HTML, PDF, and Word documents, slide presentations, a
    9·1 answer
  • Which internet explorer security feature restricts the browsing session information that can be tracked by external third-party
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!