Seirpinski Triangle through Chaos Game UserPageVisits:471 active 80 80 DaniWeb 561 60 2009-02-07T21:35:17+00:00

Seirpinski Triangle through Chaos Game


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

About the Author

easy going, don't care about much, keep a small group of friends, can get along with a lot of people, unless they bug me, i like food especially pasta, i self taught java then took a class that i aced, i am in the process of teaching myself C++, i am interested in 3d computer graphics, such as pov-ray, and anim8or, but i am not a good artist, usually i can make things look half the way i want them to, unless defined mathematically like in pov-ray

not a professional, just an 18 year old with a love of programming. gonna be a freshman fall 2008 going to major in computer science

i have always been good at math and science, in either class if the professor made a game of review i would typically win, but i am not as interested in bio as i am in physics and chem. SciWizEH is short for science wizard EH (town acronym). alot of people i know have called me "too smart"

code snippet
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());
        //set visible
    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){
 * 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
        //draw the first point
        //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()];

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

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.