Skip to content

Commit c1467a8

Browse files
authored
Add Barnsley Farn java implementation (#830)
* Add Barnsley Farn java implementation * Change matrix formatting * Change comment formatting * Small formatting issue * Various small fixes * Fixed spacing * Move Matrix multiplication to Point class
1 parent 841cb11 commit c1467a8

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

contents/barnsley/barnsley.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ The biggest differences between the two code implementations is that the Barnsle
125125
{% method %}
126126
{% sample lang="jl" %}
127127
[import, lang:"julia"](code/julia/barnsley.jl)
128+
{% sample lang="java" %}
129+
[import, lang:"java"](code/java/Barnsley.java)
128130
{% endmethod %}
129131

130132
### Bibliography
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import java.io.FileWriter;
2+
import java.io.IOException;
3+
import java.util.Random;
4+
5+
public class Barnsley {
6+
7+
private static class Point {
8+
public double x, y, z;
9+
10+
public Point(double x, double y, double z) {
11+
this.x = x;
12+
this.y = y;
13+
this.z = z;
14+
}
15+
16+
public Point(double[] coordinates) {
17+
this.x = coordinates[0];
18+
this.y = coordinates[1];
19+
this.z = coordinates[2];
20+
}
21+
22+
public Point matrixMultiplication(double[][] matrix) {
23+
double[] results = new double[3];
24+
for (int i = 0; i < 3; i++) {
25+
results[i] = matrix[i][0] * x + matrix[i][1] * y + matrix[i][2] * z;
26+
}
27+
return new Point(results);
28+
}
29+
}
30+
31+
// This is a function that reads in the Hutchinson operator and corresponding
32+
// probabilities and outputs a randomly selected transform
33+
// This works by choosing a random number and then iterating through all
34+
// probabilities until it finds an appropriate bin
35+
public static double[][] selectArray(double[][][] hutchinsonOp, double[] probabilities) {
36+
Random rng = new Random();
37+
// Random number to be binned
38+
double rand = rng.nextDouble();
39+
40+
// This checks to see if a random number is in a bin, if not, that
41+
// probability is subtracted from the random number and we check the
42+
// next bin in the list
43+
for (int i = 0; i < probabilities.length; i++) {
44+
if (rand < probabilities[i])
45+
return hutchinsonOp[i];
46+
rand -= probabilities[i];
47+
}
48+
// This return will never be reached, as the loop above ensures that at some point rand will be smaller
49+
// than a probability. However, Java does not know this and thus this return is needed for compilation.
50+
return null;
51+
}
52+
53+
// This is a general function to simulate a chaos game
54+
// n is the number of iterations
55+
// initialLocation is the starting point of the chaos game
56+
// hutchinsonOp is the set of functions to iterate through
57+
// probabilities is the set of probabilities corresponding to the likelihood
58+
// of choosing their corresponding function in hutchinsonOp
59+
public static Point[] chaosGame(int n, Point initialLocation, double[][][] hutchinsonOp, double[] probabilities) {
60+
// Initializing output points
61+
Point[] outputPoints = new Point[n];
62+
Point point = initialLocation;
63+
64+
for (int i = 0; i < n; i++) {
65+
outputPoints[i] = point;
66+
point = point.matrixMultiplication(selectArray(hutchinsonOp, probabilities));
67+
}
68+
69+
return outputPoints;
70+
}
71+
72+
public static void main(String[] args) {
73+
double[][][] barnsleyHutchinson = {
74+
{{0.0, 0.0, 0.0},
75+
{0.0, 0.16, 0.0},
76+
{0.0, 0.0, 1.0}},
77+
{{0.85, 0.04, 0.0},
78+
{-0.04, 0.85, 1.60},
79+
{0.0, 0.0, 1.0}},
80+
{{0.20, -0.26, 0.0},
81+
{0.23, 0.22, 1.60},
82+
{0.0, 0.0, 1.0}},
83+
{{-0.15, 0.28, 0.0},
84+
{0.26, 0.24, 0.44},
85+
{0.0, 0.0, 1.0}}
86+
};
87+
double[] barnsleyProbabilities = new double[]{0.01, 0.85, 0.07, 0.07};
88+
Point[] outputPoints = chaosGame(10000, new Point(0.0, 0.0, 1.0), barnsleyHutchinson, barnsleyProbabilities);
89+
try (FileWriter fw = new FileWriter("barnsley.dat")) {
90+
for (Point p : outputPoints) {
91+
fw.write(p.x + "\t" + p.y + "\n");
92+
}
93+
} catch (IOException e) {
94+
e.printStackTrace();
95+
}
96+
}
97+
98+
}

0 commit comments

Comments
 (0)