Homework 05 Due 02102015 1155pm Triangles Oh Fractals Object

Homework 05 Due 02/10/2015 11:55pm Triangles! Oh Fractals! Objective: 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.

Solution

Here is the code:

Fratctal.java

package trai;

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

class FrctalPanel extends JPanel
{
   private String type;
   private Color fregrnd, bckgrnd;

   public FrctalPanel(String type, Color fregrnd, Color bckgrnd)
   {
       super();
       this.type = type;
       this.fregrnd = fregrnd;
       this.bckgrnd = bckgrnd;
   }

   private static Coordinates mid(Coordinates a, Coordinates b)
   {
       return new Coordinates((a.x+b.x)/2, (a.y+b.y)/2);
   }

   private static Coordinates third(Coordinates a, Coordinates b)
   {
       return new Coordinates((b.x-a.x)/3+a.x, (b.y-a.y)/3+a.y);
   }

   private static double dist(Coordinates a, Coordinates b)
   {
       return Math.sqrt(Math.pow(b.x-a.x,2)+Math.pow(b.y-a.y,2));
   }

   private static void drawKochSide(Graphics2D g2, Coordinates a1, Coordinates b1)
   {
       //if lines are only 1 pixels in length
       if (dist(a1,b1)<=1)
       {
           g2.drawLine((int)a1.x, (int)a1.y, (int)b1.x, (int)b1.y);
       }
       else
       {
           Coordinates c = new Coordinates(Math.cos(Math.PI/3.0 + Math.atan2(b1.y-a1.y,b1.x-a1.x))*
               (dist(a1,b1)/3)+third(a1,b1).x,
               Math.sin(Math.PI/3.0 + Math.atan2(b1.y-a1.y,b1.x-a1.x))*
               (dist(a1,b1)/3)+third(a1,b1).y);

           drawKochSide(g2, a1, third(a1,b1));
           drawKochSide(g2, third(a1,b1), c);
           drawKochSide(g2, c, mid(third(a1,b1), b1));
           drawKochSide(g2, mid(third(a1,b1),b1), b1);
       }
   }

   private static void drawSierpinski(Graphics2D g, Coordinates a, Coordinates b, Coordinates c)
   {
       if (dist(a,b)<=2) // if lines are only 2 pixels in length
       {
           g.drawLine((int)a.x, (int)a.y, (int)b.x, (int)b.y);
           g.drawLine((int)b.x, (int)b.y, (int)c.x, (int)c.y);
           g.drawLine((int)c.x, (int)c.y, (int)a.x, (int)a.y);
       }
       else
       {
           drawSierpinski(g, a, mid(a,b), mid(a,c));
           drawSierpinski(g, mid(a,b), b, mid(b,c));
           drawSierpinski(g, mid(a,c), mid(b,c), c);
       }
   }

   public void paintComponent(Graphics g)
   {
       double w = getSize().getWidth();
       double h = getSize().getHeight();

       g.setColor(bckgrnd);
       g.fillRect(0, 0, (int)w, (int)h);
       g.setColor(fregrnd);
       Graphics2D g2 = (Graphics2D)g;

       if (\"Sierpinski\".equalsIgnoreCase(type))
       {
           drawSierpinski(g2, new Coordinates(0, h), new Coordinates(w/2.0, 0), new Coordinates(w, h));
       }
       else
       {
           double ox = 0.0, oy = 0.0;

           if ((w*2.0*Math.sqrt(3.0))<h*3.0)
           {
               h = (w * 2.0 * Math.sqrt(3.0)) / 3.0;
               oy = (getSize().getHeight()-1 - h)/2.0;
           }
           if ((w*2.0*Math.sqrt(3.0))>h*3.0)
           {
               w = (h * 3.0) / (2 * Math.sqrt(3.0));
               ox = (getSize().getWidth()-1 - w)/2.0;
           }
           drawKochSide(g2, new Coordinates(w/2.0+ox, oy), new Coordinates(ox, h*0.75+oy));
           drawKochSide(g2, new Coordinates(w+ox, h*0.75+oy), new Coordinates(w/2.0+ox, oy));
           drawKochSide(g2, new Coordinates(ox, h*0.75+oy), new Coordinates(w+ox, h*0.75+oy));
       }
   }
}

class Coordinates
{
   public double x, y;
   public Coordinates(double x, double y)
   {
       this.x = x;
       this.y = y;
   }
}

class GraphWindow extends JFrame
{
   private Container c;
   public GraphWindow()
   {
       super(\"Fractal Maker\");
       c = getContentPane();
       c.add(new FrctalPanel(\"Kosh\", Color.white, Color.darkGray));
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setSize(600,520);
   }
}

public class Fractal extends JApplet
{
   public void init()
   {
       Container cont = getContentPane();
       cont.add(new FrctalPanel(\"Sierpinski\", Color.blue, Color.white));
   }

   public static void main(String [] args)
   {
       GraphWindow app = new GraphWindow();
       Dimension scrn = Toolkit.getDefaultToolkit().getScreenSize();
       Dimension frme = app.getSize();

       if (frme.height > scrn.height) frme.height = scrn.height;
       if (frme.width > scrn.width) frme.width = scrn.width;

       app.setLocation((scrn.width - frme.width) / 2, (scrn.height - frme.height) / 2);
       app.setVisible(true);
   }
}

Homework 05 Due 02/10/2015 11:55pm Triangles! Oh Fractals! Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal usin
Homework 05 Due 02/10/2015 11:55pm Triangles! Oh Fractals! Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal usin
Homework 05 Due 02/10/2015 11:55pm Triangles! Oh Fractals! Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal usin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site