Seirpinski Triangle through Chaos Game

sciwizeh 0 Tallied Votes 525 Views Share

This is a simple implementation of the Chaos Game to produce a Seirpinski Triangle.

import java.awt.*;
import javax.swing.*;
/**
* Main Frame of the Sierpinski Triangle frame.
* @author SciWizEH
* @version 1.0
*/
public class Sierpinski extends JFrame
{
    /**make a new Sierpinski*/
    public Sierpinski()
    {
        //add STPane to frame
        this.add(new STPane());
        //pack
        this.pack();
        //set visible
        this.setVisible(true);
    }
    /**main*/
    public static void main(String[] args){
        //make a new frame
        new Sierpinski();
    }
}
/**
 * Point class holds a 2 dimentional integer point.
 */
class Point {
    //two portions of the point
    int x,y;
    //default constructor does nothing
    public Point(){}
    //constructor that takes two arguments
    public Point(int nx,int ny){
        x=nx;
        y=ny;
    }
}
/**
 * class that computes and displays the Sierpinski Triangle
 */
class STPane extends JComponent {
    //holds the initial triangle for the sierpenski
    Point[] triangle = new Point[3];
    //first point
    Point fp;
    //current point
    Point cp=new Point();
    //default constructor simply sets the original size
    public STPane(){
        this.setPreferredSize(new Dimension(500,500));
    }
    //simple method to ge a 0-2 range random number to select a new point
    public int rand0to2(){
        return (int)(Math.random()*100)%3;
    }
    //paint where the magic of the chaos happens
    public void paint(Graphics g){
        //get the current size of the component
        Dimension d = super.getSize();
        
        //make an isocilese triangle from the size of the 
        //component for the original points
        triangle[0] = new Point((int)d.getWidth()/2,0);
        triangle[1] = new Point(0,(int)d.getHeight());
        triangle[2] = new Point((int)d.getWidth(),(int)d.getHeight());
        
        //get an arbitrary point in the middle of the screen somewhere
        fp=new Point((int)(Math.random()*d.getWidth()),(int)(Math.random()*d.getHeight()/2));
        
        //get a random triangle point
        Point p = triangle[rand0to2()];
        //get the first current point by going half way from the first
        //point to the chosen triangle point
        cp.x=(p.x+fp.x)/2;
        cp.y=(p.y+fp.y)/2;
        
        //draw the first point
        g.drawRect(cp.x,cp.y,0,0);
        
        //compute 40000 points in the same fashion but use the current 
        //point rather than the very original point
        for(int i = 0; i<40000;i++){
            p = triangle[rand0to2()];
            cp.x=(p.x+cp.x)/2;
            cp.y=(p.y+cp.y)/2;
            g.drawRect(cp.x,cp.y,0,0);            
        }
    }
}
subtercosm 0 Light Poster

There's no need to write (int)(Math.random()*100)%3 . Just write (int)(Math.random() * 3) .

sciwizeh 62 Posting Pro in Training

I know, but, i've tested out some random stuff, and my way makes a better spread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.