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.



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 between 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
 The Game of Chaos


import javafx.animation.AnimationTimer
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.layout.Pane
import javafx.scene.paint.Color
import javafx.scene.shape.Circle
import javafx.stage.Stage
 
class ChaosGame extends Application {
 
    final randomNumberGenerator = new Random()
 
    @Override
    void start(Stage primaryStage) {
        primaryStage.title = 'Chaos Game'
        primaryStage.scene = getScene()
        primaryStage.show()
    }
 
    def getScene() {
        def colors = [Color.RED, Color.GREEN, Color.BLUE]
 
        final width = 640, height = 640, margin = 60
        final size = width - 2 * margin
 
        def points = [
                new Circle(width / 2, margin, 1, colors[0]),
                new Circle(margin, size, 1, colors[1]),
                new Circle(margin + size, size, 1, colors[2])
        ]
 
        def pane = new Pane()
        pane.style = '-fx-background-color: black;'
        points.each {
            pane.children.add it
        }
 
        def currentPoint = new Circle().with {
            centerX = randomNumberGenerator.nextInt(size - margin) + margin
            centerY =  randomNumberGenerator.nextInt(size - margin) + margin
            it
        }
 
        ({
 
            def newPoint = generatePoint(currentPoint, points, colors)
            pane.children.add newPoint
            currentPoint = newPoint
 
        } as AnimationTimer).start()
 
        new Scene(pane, width, height)
    }
 
    def generatePoint(currentPoint, points, colors) {
        def selection = randomNumberGenerator.nextInt 3
        new Circle().with {
            centerX = (currentPoint.centerX + points[selection].centerX) / 2
            centerY = (currentPoint.centerY + points[selection].centerY) / 2
            radius = 1
            fill = colors[selection]
            it
        }
    }
 
    static main(args) {
        launch(ChaosGame)
    }
}