Chaos game

From Rosetta Code
Revision as of 17:06, 2 March 2016 by rosettacode>Craigd (→‎{{header|zkl}}: added image)
Chaos game is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The Chaos Game is a method of generating the attractor of an iterated function system (IFS). One of the best-known and simplest examples creates a fractal, using a polygon and an initial point selected at random.

The task: play the Chaos Game using the corners of an equilateral triangle as the reference points. Add a starting point at random (preferably inside the triangle). Then add the next point halfway the starting point and one of the reference points. This reference point is chosen at random.

After a sufficient number of iterations, the image of a Sierpinski Triangle should emerge.

See also


Works with: Java version 8

<lang java>import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer;

public class ChaosGame extends JPanel {

   class ColoredPoint extends Point {
       int colorIndex;
       ColoredPoint(int x, int y, int idx) {
           super(x, y);
           colorIndex = idx;
   Stack<ColoredPoint> stack = new Stack<>();
   Point[] points = new Point[3];
   Color[] colors = {,,};
   Random r = new Random();
   public ChaosGame() {
       Dimension dim = new Dimension(640, 640);
       int margin = 60;
       int size = dim.width - 2 * margin;
       points[0] = new Point(dim.width / 2, margin);
       points[1] = new Point(margin, size);
       points[2] = new Point(margin + size, size);
       stack.push(new ColoredPoint(-1, -1, 0));
       new Timer(10, (ActionEvent e) -> {
           if (stack.size() < 50_000) {
               for (int i = 0; i < 1000; i++)
   private void addPoint() {
       try {
           int colorIndex = r.nextInt(3);
           Point p1 = stack.peek();
           Point p2 = points[colorIndex];
           stack.add(halfwayPoint(p1, p2, colorIndex));
       } catch (EmptyStackException e) {
   void drawPoints(Graphics2D g) {
       for (ColoredPoint p : stack) {
           g.fillOval(p.x, p.y, 1, 1);
   ColoredPoint halfwayPoint(Point a, Point b, int idx) {
       return new ColoredPoint((a.x + b.x) / 2, (a.y + b.y) / 2, idx);
   public void paintComponent(Graphics gg) {
       Graphics2D g = (Graphics2D) gg;
   public static void main(String[] args) {
       SwingUtilities.invokeLater(() -> {
           JFrame f = new JFrame();
           f.setTitle("Chaos Game");
           f.add(new ChaosGame(), BorderLayout.CENTER);



The Sierpinski triangle is drawn upside down since the pixmap coordinate system Y-axis is inverted. Also, this is a half assed animated process - a bunch of pixels are drawn every couple of seconds and the pixmap written [to the file system]. So, if you open the output file ("chaosGame.jpg") it will [auto] update and show the progression of the image.

Uses the PPM class from

Translation of: Java

<lang zkl>w,h:=640,640; bitmap:=PPM(w,h,0xFF|FF|FF); // White background colors:=T(0xFF|00|00,0x00|FF|00,0x00|00|FF); // red,green,blue

margin,size:=60, w - 2*margin; points:=T(T(w/2, margin), T(margin,size), T(margin + size,size) ); N,done:=Atomic.Int(0),Atomic.Bool(False);

Thread.HeartBeat('wrap(hb){ // a thread

  var a=List(-1,-1);

colorIndex:=(0).random(3); // (0..2) b,p:=points[colorIndex], halfwayPoint(a,b); x,y:=p; bitmap[x,y]=colors[colorIndex]; a=p;

  else{ hb.cancel(); done.set(); }  // stop thread and signal done

},2).go(); // run every 2 seconds, starting now

fcn halfwayPoint([(ax,ay)], [(bx,by)]){ T((ax + bx)/2, (ay + by)/2) }

done.wait(); // don't exit until thread is done println("Done");</lang>