Main Page | Report this Page

 




 

 

Science Forum Index  »  Fractals Science Forum  »  Sample Sierspinki Triangle Program
Page 1 of 1    

Sample Sierspinki Triangle Program

Author Message
Brenton Fletcher
Posted: Sun Aug 10, 2003 9:01 pm
Guest
To view this message properly, open it through your email box, with HTML messages turned on.


Sierspinki Triangle

A Sierspinki Triangle is generated in five steps:

First plot the vertices of a triangle on the screen.

1: Randomly pick a point within the triangle (we'll call it a)

2: Randomly pick one of the vertices (we'll call it b).

3: Plot the midpoint of a and b (we'll call this c).

4: set a to c.

5: repeat steps two to five.

Algorithm sourced from "Math Projects For Young Scientists" pp.99-100



I implemented this in Java: The part that is bold is the main implementation of the algorithm:

import java.applet.Applet;

import java.awt.*;

import java.awt.geom.*;

import java.util.ArrayList;

import java.util.Random;



//applet that draws a Seirspinki Triangle

public class TriangularFractalGenerator extends Applet

{

private int MAXIMUM_ITERATIONS;

private int MAX_X, MAX_Y;



///store the vertices for drawing

private ArrayList vertices = new ArrayList();

private Polygon triangle;



public void init()

{

MAXIMUM_ITERATIONS = Integer.parseInt(getParameter("iterations"));

MAX_X = Integer.parseInt(getParameter("width"));

MAX_Y = Integer.parseInt(getParameter("height"));



//////add the vertices of the invisible triangle

vertices.add(new Point2D.Double(MAX_X / 2.00, 50)); //add top vertice

vertices.add(new Point2D.Double(50, MAX_Y - 50)); //add left vertice

vertices.add(new Point2D.Double(MAX_X - 50, MAX_Y - 50)); //add right vertice



//////nessacery setup for creating a triangle Polygon

Point2D.Double topVertice = (Point2D.Double)vertices.get(0);

Point2D.Double leftVertice = (Point2D.Double)vertices.get(1);

Point2D.Double rightVertice = (Point2D.Double)vertices.get(2);



int[] triangleX = {(int)topVertice.getX(), (int)leftVertice.getX(),

//first line

(int)leftVertice.getX(), (int)rightVertice.getX()}; //second line; third

//line is automatically added



int[] triangleY = {(int)topVertice.getY(), (int)leftVertice.getY(),

//first line

(int)leftVertice.getY(),(int)rightVertice.getY()}; //second line; third

//line is automatically added



//////declare triangle; this i used to check wather or not a random point is

//////within the triangle

triangle = new Polygon(triangleX, triangleY, 3);



//////step 1: create a random point within the triangle

Point2D.Double a = randomPoint(triangle, MAX_X, MAX_Y);



//////random number generator

Random randomGenerator = new Random();



//////iterate to produce the triangle

for(int i = 0; i < MAXIMUM_ITERATIONS;="MAXIMUM_ITERATIONS;" i++)

{

//step 2 randomly select one of the vertices of the triangle

Point2D.Double b = (Point2D.Double)vertices.get(

randomGenerator.nextInt(3));

//step 3: get the midpoint of a and b, then add

//midpoint to list of points to draw

Point2D.Double c = midpoint(a, b);

vertices.add(c);

//step 4: set a to c

a = c;

}

}



public void paint(Graphics g)

{

Graphics2D g2 = (Graphics2D)g;



//////iterate

for(int i = 0; i < vertices.size();="vertices.size();" i++)

{

/////////get a new element

Point2D.Double point = (Point2D.Double)vertices.get(i);



/////////create a pixel out of point

Rectangle2D.Double pixel = new Rectangle2D.Double(point.getX(),

point.getY(), 1, 1);

/////////draw the current element

g2.draw(pixel);

}

}



///get the midpoint of a and b

public static Point2D.Double midpoint(Point2D.Double a, Point2D.Double b)

{

double x = (a.getX() + b.getX()) / 2.0;

double y = (a.getY() + b.getY()) / 2.0;



return new Point2D.Double(x, y);

}



///make a new random point outside shape, mumimum(0, 0), maximum(maxX, maxY)

public static Point2D.Double randomPoint(Shape shape, int maxX, int maxY)

{



Random randomGenerator = new Random();



Point2D.Double randomPoint;

//////loop until a random point is found that is outside shape

do

{

//create random point that is at most the bottom-right corner of shape

randomPoint = new Point2D.Double(

randomGenerator.nextInt(maxX + 1),

randomGenerator.nextInt(maxY + 1));

}

//while randomPoint is inside shape

while(shape.contains(randomPoint));



return randomPoint;

}

}

A compiled version of this applet is at http://themaster5000.netfirms.com/Applets/page5.htm





I would greatly appreaciate comment for this program.





B.
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Sat Jul 31, 2010 4:30 pm