How To Draw Sierpinski Triangle In Java Rcursivley
Homework iii: Recursive Graphics
Goals
- Learn about iterated fractal systems.
- Use functions and recursion.
Submission
Submit
Sierpinski.java,
Art.java, and, if desired, a zip file containing whatever supplementary image files needed by
Art.java. Finally, submit a completed readme_sierpinski.txt file.
Background
The Sierpinski triangle is another case of a fractal blueprint like the H-tree pattern from Section 2.3 of the textbook. The Shine mathematician Wacław Sierpiński described the blueprint in 1915, only it has appeared in Italian fine art since the 13th century. Though the Sierpinski triangle looks complex, it tin can be generated with a brusk recursive program. Your tasks are to write a recursive program that draws the Sierpinski triangle and a second program that draws another design of your choosing using recursion.
Hither's a fractal in polymer clay or a fractal cookie.
Here is a Hall-of-Fame Fine art Gallery from past semesters for the second office of this consignment.
Here's a Sierpinski candy corn video. Bank check out her other videos while you're at it!
Part I: The Sierpinski Triangle
Write a program Sierpinski.java with a recursive office sierpinski() and a main() function that calls the recursive function in one case, and plots the consequence using standard cartoon.
- Review the H-Tree instance from the textbook and lecture.
- Cartoon a triangle. Write a function singleTriangle() that uses StdDraw.filledPolygon() to draw a filled, equilateral triangle (see the booksite for help with StdDraw.) The side length and triangle location should be arguments to the role. Information technology is upward to you whether to specify the location of the triangle center, a particular vertex, or something else (we recommend using the bottom vertex). Compile. Use the figure below to work out the coordinates of the triangle's vertices:
- Write a main() function that calls singleTriangle() to draw a triangle with sides of length 0.5 with the lesser vertex located at (0.5, 0). Later on you will change main to depict the full Sierpinski triangle. Compile and run. The triangle should be in the bottom center of your window.
- The recursive construction. Write a function sierpinski() that takes two arguments due north and size. Your function should print n and size, so recursively call itself three times with the arguments due north - 1 and size / 2. The recursion should stop when n is 0. After this recursion is tested, you volition add in a telephone call to the triangle-drawing function. Compile.
- Modify main() to take one integer, command-line argument Due north and have it telephone call sierpinski(N, 0.5). Your programme should produce the following output:
% coffee Sierpinski 0 [ No output. ] % java Sierpinski 1 i 0.5 % coffee Sierpinski 2 2 0.5 1 0.25 one 0.25 1 0.25 % coffee Sierpinski 3 iii 0.v 2 0.25 ane 0.125 1 0.125 1 0.125 two 0.25 1 0.125 1 0.125 1 0.125 2 0.25 1 0.125 1 0.125 1 0.125
- Note: You should remove these print statements before submitting. The final output of your program will be just the graphical representation.
- Cartoon the Sierpinski triangle. Modify sierpinski() to take 2 additional arguments, x and y, and draw a Sierpinski triangle of order n of size size with the bottom vertex at (x,y). Recall recursively: a Sierpinski triangle of lodge n is just a solid triangle surround by three smaller Sierpinski triangle (half the size) of order n - ane to the left and correct and in a higher place it. You already accept written the code to draw the triangle and to do the recursion from the previous steps - you simply demand to make the advisable function calls. (The triangular outline in the figures beneath is for illustration. You do non need to draw it in your own program.)
% java Sierpinski 1 % java Sierpinski 2 % java Sierpinski 3
% java Sierpinski iv % coffee Sierpinski 5 % java Sierpinski six
- Warning: Practise non call StdDraw.save(). It will interruption our test scripts and you lot will receive a large point deduction.
- Warning: Practice non call StdDraw.setXscale(), StdDraw.setXscale(), or StdDraw.setCanvasSize(). They will interruption our examination scripts and you lot will receive a large point deduction.
Function Two: Take Fun
In this part you will create your own recursive pattern. Write a programme
Art.javathat takes one integer command-line argument
Northto control the depth of the recursion and produces a recursive design of your ain choosing.
It's upward to you what to write, every bit long every bit it follows the rules beneath. Check out the Art Gallery from by semesters for ideas, also as the Famous Fractals in Fractals Unleashed, and Wikipedia'southward listing of fractals by Hausdorff dimension. Some pictures are harder to generate than others (and some require trig); consult a TA for advice if you're unsure.
Some examples from the lists above that fulfill the requirements for Art.coffee are shown beneath:
Rules:
- The pattern can be based on recursively drawing a design, similar Sierpinski, on recursive subdivision, like the dragon curve or fractal Brownian movement, or on any combination of the two. Just information technology must be recursive (sad, no Mandelbrot fractals).
- The recursive structure of your programme must be different from Sierpinski, H-Tree, and Brownian — just changing the triangle in Sierpinski to a foursquare, for example, is not enough. If the number of recursive calls is different, or the order in which the calls are made is unlike, you should be fine.
- Your programme must not exist tail-recursive. Tail recursive functions brand a single recursive call at the very beginning or finish of the function.
- Your cartoon must stay with the unit of measurement square (the area betwixt 0 and i in both x- and y-coordinates). The window shows a bit more than this, so if you draw the foursquare in your program, y'all volition be able to see if your drawing goes outside it.
- The background of your drawing should be white, to save toner, unless you apply a background paradigm. If you employ a background image, it may extend outisde the unit square. Please crop it as all-time yous can to the correct size however, considering it makes grading easier.
- Warnings: All the aforementioned warnings apply to Art.java as Sierpinski.coffee.
- You may optionally submit a nix file containing any additional images, sound or other data required for your program. Your programme can rely on blitheness for effect if you wish, as long as it is recursive.
- Be artistic and have fun (but run across the extra credit note below).
Actress Credit
You will receive up to 2 points of extra credit for espeically creative submissions. Extra credit volition be rewarded for espeically cool recursions, just as well for especially absurd designs, even if the recursion is fairly simple. Equally long as your Art.coffee meets the minimum recursion required (non tail-recursive, and different from our examples), it will exist eligible for extra credit.
Since this assignment is on a compressed schedule before the midterm, we will let extra credit submissions up to one week late . You must all the same submit an Fine art.java with your assignment for grading and so you go the practice writing recursive functions. Just you can submit something basic for the grade, and something more creative for actress credit later.
Afterward the regular deadline, we volition open a dissever assignment for submitting the extra credit. If you receive extra credit, information technology will still exist entered on your original homework. If your original Art.java is worthy of extra credit, that is fine. You practise non have to make a divide submission to receive extra credit.
Often Asked Questions and Common Problems
- What training practice I need before beginning this consignment? Read Sections two.1-2.3 of the textbook.
- Do I need to do whatsoever error checking? No. In particular, y'all may assume your programs will be run with exactly 1 argument which will exist a positive integer (possible 0).
- I run into a bare window, even though I am drawing a triangle. Brand certain all your coordinates are between 0 and 1. Otherwise, the figure you lot are drawing is outside the window and will not be visible.
- Tin can I use color? Yep! You lot can utilize StdDraw.setPenColor() to prepare the drawing colour in both Sierpinski.java and Art.java. StdDraw defines names for standard colors, similar StdDraw.RED.
- Tin I apply colors not defined in StdDraw? Yes, but information technology requires some syntax that we will not cover until afterwards in the semester. For now you will need to take the instructions below equally magic, or look upwardly the meaning on your own. You can create a color like this:
java.awt.Color c = new Colour(r, thou, b);
where r, g, and b are integers betwixt 0 and 255 (inclusive). c is a variable, only similar whatever other variable, except that it contains a color rather than a number or a string. You lot tin can call StdDraw.setColor(c) to describe in the color y'all just created.
- My function for Art.java takes several parameters, but the assignment says that I can only read in one command-line argument Northward. What should I exercise? Choose a few of the best parameter values and do something similar the following:
if (Due north == ane) { x = 0.55; y = 0.75; n = 3; } else if (N == two) { x = 0.55; y = 0.75; n = 5; } else if (N == 3) { x = 0.32; y = 0.71; due north = 8; } else if ... - Is my Art.java good plenty to get full credit? Meet the directions for Part Two. This is the most common question we go far role hours for this assignment, but you need to decide for yourself. We exercise not grade homework before information technology is submitted, and we can never promise in advance that something is correct.
- Is my Fine art.java cool plenty to get extra credit? See above.
Enrichment: Fractal Dimension
In grade school, we learn that the dimension of a line segment is 1, the dimension of a foursquare is two, and the dimension of a cube is three. Simply you probably didn't learn what is actually meant past dimension. How can nosotros express what it means mathematically or computationally? Formally, we can ascertain the Hausdorff dimension or similarity dimension of a self-similar figure by segmentation the figure into a number of self-similar pieces of smaller size. Nosotros define the dimension to be the log (# self similar pieces) / log (scaling factor in each spatial direction). For case, we can decompose the unit foursquare into 4 smaller squares, each of side length 1/2; or we tin can decompose it into 25 squares, each of side length 1/5. Here, the number of cocky-similar pieces is 4 (or 25) and the scaling factor is 2 (or v). Thus, the dimension of a square is two since log (4) / log(2) = log (25) / log (v) = 2. We can decompose the unit cube into eight cubes, each of side length ane/2; or nosotros can decompose information technology into 125 cubes, each of side length 1/5. Therefore, the dimension of a cube is log(8) / log (2) = log(125) / log(5) = three.
Nosotros can also utilise this definition directly to the (prepare of white points in) Sierpinski triangle. We tin decompose the unit Sierpinski triangle into three Sierpinski triangles, each of side length one/2. Thus, the dimension of a Sierpinski triangle is log (3) / log (2) ≈ 1.585. Its dimension is fractional—more than than a line segment, but less than a square! With Euclidean geometry, the dimension is always an integer; with fractal geometry, it can be whatsoever fraction. Fractals are widely applicable for describing physical objects like the coastline of Britain.
Source: https://www.cis.upenn.edu/~cis110/13sp/hw/hw03/sierpinski.shtml
Posted by: lewissithen.blogspot.com

0 Response to "How To Draw Sierpinski Triangle In Java Rcursivley"
Post a Comment