Write simplistic recursive Java code for Sierpinski
I made a psuedo code tell me if anything wrong with it:
// x - left edge, y - right edge, z - peak, num - number of times the program is supposed to run
public void drawTriangle (int x, int y, int z, int num)
maketriangle(x,y,z); //imaginary method to create triangle using coordinates.
if (num == 0) {
return;
}
else {
int midxy = (y-x)/2;
int midxz = (z-x)/2;
int midyz = (z-y)/2;
drawTriangle(x, midxy, midxz, num);
drawTriangle(y, midxy, midyz, num);
drawTriangle(z, midxz, midyz, num);
num--;
}
}
Please note you must find problems in the above simple code that I wrote and also write the code which will take coordinates of 3 points instead of 3 points. The parameters for the method that you write will contain 7 parameters.
Hello, I have answered similar question before. Here is the completed code for drawing sierpinski triangles with 5 levels. You can find the main recursive method to draw sierpinski triangles and find out the working of it (Highlighted in bold text). Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: In the question, you mentioned that the method should have 7 parameters. But all I could think of is 5 parameters – 3 Points to denote the coordinates, 1 Graphics object to draw the triangles and 1 integer denoting the number of levels of triangles. If you need to change this, you should provide me info regarding that 7 parameters.
// Sierpinski.java
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Sierpinski extends JFrame {
public static final int SIZE = 400; // height/width of DrawingPanel
private JPanel drawPanel; // panel to which the triangles are drawn
public Sierpinski() {
// setting up panel and adding to frame
drawPanel = new JPanel();
drawPanel.setPreferredSize(new Dimension(SIZE, SIZE));
add(drawPanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
// getting a graphics object from drawing panel
Graphics graphics = drawPanel.getGraphics();
// finding the coordinates of top center, bottom left and bottom right
Point p1 = new Point(drawPanel.getWidth() / 2, 0);
Point p2 = new Point(0, drawPanel.getHeight());
Point p3 = new Point(drawPanel.getWidth(), drawPanel.getHeight());
// drawing sierpinski triangles with levels = 5
drawSierpinski(p1, p2, p3, graphics, 5);
}
public static void main(String[] args) {
new Sierpinski();
}
/*
* draws a single triangle
*
* @ Graphics g graphics object to draw the triangle
*
* @ Point p1,p2,p3 - coordinates
*/
public static void drawTriangle(Point p1, Point p2, Point p3, Graphics g) {
/**
* creating a polygon with the points and filling it
*/
Polygon p = new Polygon();
p.addPoint(p1.x, p1.y);
p.addPoint(p2.x, p2.y);
p.addPoint(p3.x, p3.y);
g.fillPolygon(p);
}
/**
* The actual method that draws a general level-n Sierpinski triangle whose
* vertices are (p1, p2, p3)
*
* @ int n: number of levels
*
* @ Graphics g graphics object
*
* @ Point p1,p2,p3 coordinates of the triangle
*/
public static void drawSierpinski(Point p1, Point p2, Point p3, Graphics g,
int n) {
if (n == 1) {
// base case: simple triangle
drawTriangle(p1, p2, p3, g);
} else {
// get the three middle points between p1 and p2, p1 and p3, p2 and
// p3
Point p4 = new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
Point p5 = new Point((p2.x + p3.x) / 2, (p2.y + p3.y) / 2);
Point p6 = new Point((p1.x + p3.x) / 2, (p1.y + p3.y) / 2);
// using recursive call to draw three sub-triangles, with one less
// value for n
drawSierpinski(p1, p4, p6, g, n - 1);
drawSierpinski(p4, p2, p5, g, n - 1);
drawSierpinski(p6, p5, p3, g, n - 1);
}
}
}
/*OUTPUT*/

Write simplistic recursive Java code for Sierpinski I made a psuedo code tell me if anything...
Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...
Assignment Λ You shall write a Java program that accepts 5 command-line arguments and generates an image of a Sierpinski triangle, as a 24- bit RGB PNG image file. Specifications The command-line arguments shall consist of the following 1. The width (in pixels) of the image, as a positive decimal integer 2. The height (in pixels) of the image, as a positive decimal integer 3. The minimum area (in pixels) that a triangle must have in order to be drawn,...
Please explain clearly with steps. Its a very small code . thanks I would rate positively. Write a class pixel in java with following properties. A Pixel has x, y coordinates, which are both integer values. A Pixel also has the following behaviors: It can return the value of its x and y coordinates. Use the follwoing method signatures to implement these behaviors. public int getX( ) public int getY( ) It can change the value of its x and...
I need java code for the following problem.
Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this: public static double divide(double operand1, double operand2) { return operand1 / operand2; } The random method is supposed to return a double within a lower limit and...
JAVA Code Requried Copy the file java included below. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Below the main method, but in the Geometry class, create a static method called printMenu that has no parameter list and does not return a value. It will...
This is my code that i need to finish. In BoxRegion.java I have no idea how to create the constructor. I tried to use super(x,y) but It is hard to apply. And Also In BoxRegionHashTable, I don't know how to create displayAnnotation BoxRegion.java ------------------------------------------------ public final class BoxRegion { final Point2D p1; final Point2D p2; /** * Create a new 3D point with given x, y and z values * * @param x1, y1 are the x,y coordinates for point...
java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main { { return true; } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main { { String ans = name.toUpperCase();...
Need code written for a java eclipse program that will follow
the skeleton code.
Exams and assignments are weighted
You will design a Java grade calculator for this assignment. A user should be able to calculate her/his letter grade in COMS/MIS 207 by inputting their scores obtained on worksheets, assignments and exams into the program. A skeleton code named GradeCompute.java containing the main method and stubs for a few other methods, is provided to you. You must not modify/make changes...
Use PYTHON!!! NOT Java or anything else!! Function name : crazy_scrabble Parameters : word (string), current points (int), double word score (boolean) Returns: int Description : You are playing scrabble with a friend, and you thought you knew how to calculate how many points you have, but your friend suddenly starts making up all these crazy rules. Write a function that helps keep track of how many points you have with these new rules: ● Each 'k', 'm', 'p', 'x',...