Sierpinski Triangle in JApplet

I made this simple Sierpinski triangle JApplet for a demonstration of a recursive function for my Programming Logic cheat sheet.

I’m an Zelda fanboy so this seem like a natural choice to demonstrate how a recursive function can be used. A Sierpinski triangle is a triangle inside an triangle. Check out this Wikipedia page, it has animated gifs and a good explanation of what a Sierpinski triangle is.
 
http://en.wikipedia.org/wiki/Sierpinski_triangle
 
The majority of the JApplet code isn’t important, only the draw function is doing bulk of the work. For those who are new to Java and want to play around with this, just chuck it over to eclipse or netbeans and run it. Since this is a JApplet, you won’t need a main function to have the code execute. I also included an Thread.sleep(500), which will draw each line at a rate of 500 milliseconds, if you want to see how it is constructed.

 
Using a depth of 1 you’ll get a normal triage and the draw function won’t act in a recursive manor.
 

 
Next, here is a Sierpinksi Triagnle looks like with a depth of 2. The tri-force is strong with this one.
 

 
My final screen cap is with using a depth of 3.
 

 
SierpinksiTriangle source code:
 

import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JApplet;
import javax.swing.JOptionPane;


public class SierpinskiGasket extends JApplet {
	private int depth;
	
	public void init(){
		this.depth = Integer.parseInt( JOptionPane.showInputDialog("Enter Sierpinski gasket depth:") );
	}
	
	public void paint(Graphics graph){
		// Points that make up the initial triangle
		Point pt1 = new Point(60,160);
		Point pt2 = new Point(220,160);
		Point pt3 = new Point(140,20);
		
		try {
			draw(graph, depth, pt1, pt2, pt3);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void draw(Graphics graph, int level, Point pt1, Point pt2, Point pt3) throws InterruptedException {
		
		if (level > 0){
			//Connecting the dots, la-la-la-la
			Thread.sleep(500);
			graph.drawLine(pt1.x, pt1.y, pt2.x, pt2.y);
			graph.drawLine(pt2.x, pt2.y, pt3.x, pt3.y);
			graph.drawLine(pt3.x, pt3.y, pt1.x, pt1.y);
			
			// Mid-points between given points
			Point pt1_pt2 = new Point( (pt1.x + pt2.x) / 2,
					                   (pt1.y + pt2.y) / 2 ); 
			Point pt2_pt3 = new Point( (pt2.x + pt3.x) / 2,
					                   (pt2.y + pt3.y) / 2 );
			Point pt3_pt1 = new Point( (pt3.x + pt1.x) / 2,
					                   (pt3.y + pt1.y) / 2 );
			
			//Recursively graph midpoints
			draw(graph, level - 1, pt1, pt1_pt2, pt3_pt1);
			draw(graph, level - 1, pt2, pt2_pt3, pt1_pt2);
			draw(graph, level - 1, pt3, pt3_pt1, pt2_pt3);
			
		}
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s