All posts tagged with Java, page 1 of 3
I remember when...Sierpinski Fractal
One of the first challenges I set myself after mastering the course work in my first year of uni (2002-3) was to write a fractal applet. Over the years I seem to have lost all my old code so I was pleased to happen upon this old post of mine (complete with misspelled factal name). The resulting Applet is also still online here. [There are several applets on the page and they take a few seconds to load so be patient.]
Message
Brenton Fletcher
Posted: Sun Aug 10, 2003 9:01 pm
Guest
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 addedint[] 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
05:34 PM on Friday, 28/08/2009
Number generator applets
While I was studying Java through RMIT (2002-3) I wrote several mathematical applets and number generators which are still available on my old, old, old site.
I received the grade of High Distinction for each and every programming assignment I completed at university (and consequently for each of those units received High Distinction grade). I never worked in any of the programming languages learned at university as I went on to teach myself CSS, PHP, JavaScript etc which formed the basis of my first jobs. I am now working in Ruby on Rails solely on back ends.
04:39 PM on Tuesday, 25/08/2009
Squares 2
The second piece of digital art I created using Processing in 2005 was Squares 2. It’s similar to Squares 1, but the right and bottom sides of the square are set randomly.
04:31 PM on Tuesday, 25/08/2009
Trees 1 (Digital Art)
The fourth digital artwork I wrote using Processing back in 2005 was Trees1. It generates a tree using recursion. Each call to the function divides the line up into four other lines. The same function is then called on those lines. The saturation and brightness is lightened, and the lines are thinned to represent branches, with each call to the function.
04:25 PM on Tuesday, 25/08/2009
Trees 3, 3b, 3c
The fifth piece of digital artwork I created using Processing in 2005 this sketch draws a kind of ‘abstract tree’. Each click on the sketch creates a new randomly generated sketch, with a different background and tree. It uses a recursive function that draws a line, then calls itself 1 – 3 times with the angle, saturation, and brightness modified by a random amount, and a random distance.
Take a look >>
Take a look (random start position) >> (might have to wait a few seconds)
Take a look (white background) >>
04:24 PM on Tuesday, 25/08/2009
