Skip to content

Commit 4a74c1e

Browse files
authored
Add IFS java implementation (#817)
1 parent dce740f commit 4a74c1e

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

contents/IFS/IFS.md

+4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Here, instead of tracking children of children, we track a single individual tha
140140
[import:18-29, lang:"c"](code/c/IFS.c)
141141
{% sample lang="coco" %}
142142
[import:4-16, lang:"coconut"](code/coconut/IFS.coco)
143+
{% sample lang="java" %}
144+
[import:16-39, lang:"java"](code/java/IFS.java)
143145
{% endmethod %}
144146

145147
If we set the initial point to the on the equilateral triangle we saw before, we can see the Sierpinski triangle again after a few thousand iterations, as shown below:
@@ -220,6 +222,8 @@ In addition, we have written the chaos game code to take in a set of points so t
220222
[import, lang:"c"](code/c/IFS.c)
221223
{%sample lang="coco" %}
222224
[import, lang:"coconut"](code/coconut/IFS.coco)
225+
{%sample lang="java" %}
226+
[import, lang:"java"](code/java/IFS.java)
223227
{% endmethod %}
224228

225229
### Bibliography

contents/IFS/code/java/IFS.java

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.io.FileWriter;
2+
import java.util.Random;
3+
4+
public class IFS {
5+
6+
private static class Point {
7+
double x, y;
8+
9+
public Point(double x, double y) {
10+
this.x = x;
11+
this.y = y;
12+
}
13+
}
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
43+
// initial triangle with three points on the vertices of an equilateral triangle:
44+
// A = (0.0, 0.0)
45+
// B = (0.5, sqrt(0.75))
46+
// C = (1.0, 0.0)
47+
// It will output the file sierpinski.dat, which can be plotted after
48+
Point[] shapePoints = new Point[]{
49+
new Point(0.0, 0.0),
50+
new Point(0.5, Math.sqrt(0.75)),
51+
new Point(1.0, 0.0)
52+
};
53+
Point[] outputPoints = chaosGame(10000, shapePoints);
54+
55+
FileWriter fw = new FileWriter("sierpinski.dat");
56+
for (Point p : outputPoints)
57+
fw.write(p.x + "\t" + p.y + "\n");
58+
fw.close();
59+
}
60+
61+
}

0 commit comments

Comments
 (0)