Chaos game

From Rosetta Code
Revision as of 10:43, 2 March 2016 by rosettacode>Fwend (→‎{{header|Java}}: 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


Java

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 = {Color.red, Color.green, Color.blue};
   Random r = new Random();
   public ChaosGame() {
       Dimension dim = new Dimension(640, 640);
       setPreferredSize(dim);
       setBackground(Color.white);
       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++)
                   addPoint();
               repaint();
           }
       }).start();
   }
   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) {
           System.out.println(e);
       }
   }
   void drawPoints(Graphics2D g) {
       for (ColoredPoint p : stack) {
           g.setColor(colors[p.colorIndex]);
           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);
   }
   @Override
   public void paintComponent(Graphics gg) {
       super.paintComponent(gg);
       Graphics2D g = (Graphics2D) gg;
       g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);
       drawPoints(g);
   }
   public static void main(String[] args) {
       SwingUtilities.invokeLater(() -> {
           JFrame f = new JFrame();
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           f.setTitle("Chaos Game");
           f.setResizable(false);
           f.add(new ChaosGame(), BorderLayout.CENTER);
           f.pack();
           f.setLocationRelativeTo(null);
           f.setVisible(true);
       });
   }

}</lang>

zkl

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 http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl

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);
  if(N.inc()<50){
     do(500){ 

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

     }
     bitmap.writeJPGFile("chaosGame.jpg");
  }
  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>

Output:

http://www.zenkinetic.com/Images/RosettaCode/chaosGame.jpg