-
-
Notifications
You must be signed in to change notification settings - Fork 360
Add Barnsley Farn java implementation #830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leios
merged 7 commits into
algorithm-archivists:master
from
stormofice:barnsley-java-implementation
Aug 24, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3e98dc
Add Barnsley Farn java implementation
stormofice af34bc2
Change matrix formatting
stormofice df42909
Change comment formatting
stormofice d147092
Small formatting issue
stormofice a7f0d1e
Various small fixes
stormofice e1f39bd
Fixed spacing
stormofice 0b65d81
Move Matrix multiplication to Point class
stormofice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Random; | ||
|
||
public class Barnsley { | ||
|
||
private static class Point { | ||
public double x, y, z; | ||
|
||
public Point(double x, double y, double z) { | ||
this.x = x; | ||
this.y = y; | ||
this.z = z; | ||
} | ||
|
||
public Point(double[] coordinates) { | ||
this.x = coordinates[0]; | ||
this.y = coordinates[1]; | ||
this.z = coordinates[2]; | ||
} | ||
|
||
public Point matrixMultiplication(double[][] matrix) { | ||
double[] results = new double[3]; | ||
for (int i = 0; i < 3; i++) { | ||
results[i] = matrix[i][0] * x + matrix[i][1] * y + matrix[i][2] * z; | ||
} | ||
return new Point(results); | ||
} | ||
} | ||
|
||
// This is a function that reads in the Hutchinson operator and corresponding | ||
// probabilities and outputs a randomly selected transform | ||
// This works by choosing a random number and then iterating through all | ||
// probabilities until it finds an appropriate bin | ||
public static double[][] selectArray(double[][][] hutchinsonOp, double[] probabilities) { | ||
Random rng = new Random(); | ||
// Random number to be binned | ||
double rand = rng.nextDouble(); | ||
|
||
// This checks to see if a random number is in a bin, if not, that | ||
// probability is subtracted from the random number and we check the | ||
// next bin in the list | ||
for (int i = 0; i < probabilities.length; i++) { | ||
if (rand < probabilities[i]) | ||
return hutchinsonOp[i]; | ||
rand -= probabilities[i]; | ||
} | ||
// This return will never be reached, as the loop above ensures that at some point rand will be smaller | ||
// than a probability. However, Java does not know this and thus this return is needed for compilation. | ||
return null; | ||
stormofice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// This is a general function to simulate a chaos game | ||
// n is the number of iterations | ||
// initialLocation is the starting point of the chaos game | ||
// hutchinsonOp is the set of functions to iterate through | ||
// probabilities is the set of probabilities corresponding to the likelihood | ||
// of choosing their corresponding function in hutchinsonOp | ||
public static Point[] chaosGame(int n, Point initialLocation, double[][][] hutchinsonOp, double[] probabilities) { | ||
// Initializing output points | ||
Point[] outputPoints = new Point[n]; | ||
Point point = initialLocation; | ||
|
||
for (int i = 0; i < n; i++) { | ||
outputPoints[i] = point; | ||
point = point.matrixMultiplication(selectArray(hutchinsonOp, probabilities)); | ||
} | ||
|
||
return outputPoints; | ||
} | ||
|
||
public static void main(String[] args) { | ||
double[][][] barnsleyHutchinson = { | ||
{{0.0, 0.0, 0.0}, | ||
{0.0, 0.16, 0.0}, | ||
{0.0, 0.0, 1.0}}, | ||
{{0.85, 0.04, 0.0}, | ||
{-0.04, 0.85, 1.60}, | ||
{0.0, 0.0, 1.0}}, | ||
{{0.20, -0.26, 0.0}, | ||
{0.23, 0.22, 1.60}, | ||
{0.0, 0.0, 1.0}}, | ||
{{-0.15, 0.28, 0.0}, | ||
{0.26, 0.24, 0.44}, | ||
{0.0, 0.0, 1.0}} | ||
}; | ||
double[] barnsleyProbabilities = new double[]{0.01, 0.85, 0.07, 0.07}; | ||
Point[] outputPoints = chaosGame(10000, new Point(0.0, 0.0, 1.0), barnsleyHutchinson, barnsleyProbabilities); | ||
try (FileWriter fw = new FileWriter("barnsley.dat")) { | ||
for (Point p : outputPoints) { | ||
fw.write(p.x + "\t" + p.y + "\n"); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.