Please answer this in java, and please try to follow the example of the Sierpens
ID: 3667291 • Letter: P
Question
Please answer this in java, and please try to follow the example of the Sierpenski carpet (it will obviously have to be different, but same methodology and approach). So the only big difference is there will be a drawTriangle instead of Gaske, and what is inside will differ as well. I cannot turn in anything that has methods and bits of code I have not learned yet, and we just started this section.
Write a program in which draws (yes it actually makes a picture) a triangular fractal using recursion. This is best if done using a java applet.
Suggested Methodology
The idea for it is this
First draw a filled equilateral triangle
Next draw another filled equilateral triangle of a different color that’s upside down in the middle of that triangle
Using the other triangles formed repeat step 2 until a pixel limit of 4 is reached
HINTS:
It may be a good idea to look at the examples I gave 02/12/2015
The method fillPolygon(int[] xPoints, int[] yPoint, numberOfPoints) as called by the graphics device is important
The method setColor(Color aColor) is important for picking different colors to draw things.
import java.applet.*;//Needed to create the window
import java.awt.*;//Needed for drawing
import java.util.*;
public class SierpenskiCarpet extends Applet{//When it extends applet it creates a window and calls certain methods
private Image display;//Used as the flat image
private Graphics drawingArea;//Used to draw item in the image
/*
*This is called by the parent Applet, as its an overridden method, and it initializes all of the instance variables.
* Think of this as a variation of a constructor, but called by another, hidden piece of code
*/
public void init()
{
//get the height and width from the Applet
int height = getSize().height;
int width = getSize().width;
//Creates an image using the height and width in the applet
display = createImage(width,height);
//Sets up the drawing area for the image above to be drawn on
drawingArea = display.getGraphics();
//This draws the random fractal
drawGasket(0,0,height,drawingArea);
}
/*
* This updates every frame and draws the image in the applet. This is also called by
* the applet as it is an overridden method.
*/
public void paint(Graphics g)
{
g.drawImage(display,0,0,null);
}
public void drawSquare(Graphics g)
{
g.fillRect(10, 10, 10, 10);
}
/*
* This creates the carpet by first dividing the square into 9 squares. Then fills in the center square.
* Finally is recursively called on the 8 other squares until a pixel limit is reached.
*/
public static void drawGasket(int x, int y, int side, Graphics g)
{
//Divides each side by 3. Since it's a square we only need to calculate this once
int sub = side / 3;
//Draws the middle square. The square is drawn from the top left position to a specified width and height.
g.fillRect(x+sub, y+sub, sub, sub);
//If it hasn't reached the pixel limit the call the function recursively on the 8 remaining squares.
if(sub >= 3)
{
//Top 3 squares
drawGasket(x,y,sub,g); //Left
drawGasket(x+sub,y,sub,g); //Middle
drawGasket(x+2*sub,y,sub,g); //Right
//Middle 2 squares
drawGasket(x, y+sub,sub,g); //Left
drawGasket(x+2*sub,y+sub,sub,g); //Right
//Bottom 3 Squares
drawGasket(x,y+2*sub,sub,g); //Left
drawGasket(x+sub,y+2*sub,sub,g); //Middle
drawGasket(x+2*sub,y+2*sub,sub,g); //Right
}
}
}
Explanation / Answer
// recursiveSierpinski class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package recursivesierpinski;
/**
*
* @author HARE KRISHNA
*/
public class recursiveSierpinski {
public static void draw(int n, double x0, double y0, double x1,
double y1, double x2, double y2) {
// if reach base case, method return
if (n==0) return;
// define array xm, ym to store x and y values of midpoints
double [] xm = new double[3];
double [] ym = new double[3];
// assign midpoints’ values to xm and ym
xm[0]= (x0+x1)/2;
xm[1]= (x1+x2)/2;
xm[2]= (x2+x0)/2;
ym[0]= (y0+y1)/2;
ym[1]= (y1+y2)/2;
ym[2]= (y2+y0)/2;
StdDraw.filledPolygon(xm, ym); //this makes triangle
draw(n-1,xm[0],ym[0],xm[1],ym[1],x1,y1);
draw(n-1,xm[1],ym[1],xm[2],ym[2],x2,y2);
draw(n-1,xm[2],ym[2],xm[0],ym[0],x0,y0);
}
}
// Main Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package recursivesierpinski;
/**
*
* @author HARE KRISHNA
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// N levels of recursion
int N = Integer.parseInt(args[0]);
// outline the triangle
double t = Math.sqrt(3.0) / 2.0;
StdDraw.setPenColor(StdDraw.BLACK);
// fill arrays initially to draw black solid TRIANGLE xm, ym = 0.0, 0.0, 0.5, t, 1.0, 0.0
StdDraw.filledPolygon(xm, ym);
StdDraw.setPenColor(StdDraw.WHITE);
draw(N, 0.0, 0.0, 0.5, t, 1.0, 0.0);
}
private static void draw(int N, double d, double d0, double d1, double t, double d2, double d3) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
}