| 14 | |
| 15 | // This is a function to simulate a "chaos game" |
| 16 | public static Point[] chaosGame(int n, Point[] shapePoints) { |
| 17 | Random rng = new Random(); |
| 18 | |
| 19 | // Initialize output vector |
| 20 | Point[] outputPoints = new Point[n]; |
| 21 | |
| 22 | // Choose first point randomly |
| 23 | Point point = new Point(rng.nextDouble(), rng.nextDouble()); |
| 24 | |
| 25 | for (int i = 0; i < n; i++) { |
| 26 | outputPoints[i] = point; |
| 27 | |
| 28 | // Clone point to get a new reference |
| 29 | point = new Point(point.x, point.y); |
| 30 | |
| 31 | // Retrieve random shape point |
| 32 | Point temp = shapePoints[rng.nextInt(shapePoints.length)]; |
| 33 | // Calculate midpoint |
| 34 | point.x = 0.5 * (point.x + temp.x); |
| 35 | point.y = 0.5 * (point.y + temp.y); |
| 36 | } |
| 37 | |
| 38 | return outputPoints; |
| 39 | } |
| 40 | |
| 41 | public static void main(String[] args) throws Exception { |
| 42 | // This will generate a Sierpinski triangle with a chaos game of n points for an |