Skip to content
Snippets Groups Projects
Commit 8137ed07 authored by Håvard Daleng's avatar Håvard Daleng
Browse files

Tried to use filehandler.

parent 89aa5433
No related branches found
No related tags found
No related merge requests found
......@@ -20,12 +20,12 @@ public class Main {
System.out.println("Hi");
String path = "src/main/resources/descriptions/TestDescription.txt";
Vector2D minCoords = new Vector2D(0, 0);
Vector2D maxCoords = new Vector2D(1, 1);
Matrix2x2 transformMatrix1 = new Matrix2x2(0.5, 0, 0, 0.5);
Vector2D translationVector1 = new Vector2D(0, 0);
Vector2D translationVector2 = new Vector2D(0.25, 0.5);
Vector2D translationVector3 = new Vector2D(0.5, 0);
......@@ -40,11 +40,13 @@ public class Main {
transforms.add(affineTransform3);
ChaosGameDescription description = null; // Declare description
ChaosGame game = null;
try {
//ChaosGameDescription description = new ChaosGameFileHandler().readFromFile(path);
description = new ChaosGameDescription(minCoords, maxCoords, transforms);
//description = new ChaosGameDescription(minCoords, maxCoords, transforms);
description = new ChaosGameFileHandler().readFromFile(path);
System.out.println("MaxCoords-object of description: " + description.getMaxCoords());
ChaosCanvas canvas = new ChaosCanvas(100, 100, description.getMinCoords(), description.getMaxCoords()); // Create canvas
game = new ChaosGame(description, canvas);
......@@ -52,7 +54,7 @@ public class Main {
} catch (Exception e) {
System.out.println(e.getMessage()); // TODO: Alter error handling
}
game.runSteps(100000);
//game.runSteps(100000);
......
......@@ -21,106 +21,106 @@ import java.io.BufferedWriter;
public class ChaosGameFileHandler {
/**
* Creates a ChaosGameDescription based on the file found at the input path.
* @param path A String input which gives the path to a file describing a chaos game.
* @return A {@link ChaosGameDescription} description of a chaos game.
*/
public ChaosGameDescription readFromFile(String path) throws IOException {
Vector2D minimumVector;
Vector2D maximumVector;
List<Transform2D> transforms = new ArrayList<>();
Path paths = Paths.get(path);
try (Scanner scanner = new Scanner(Files.newInputStream(paths))){
scanner.useLocale(Locale.ENGLISH); // useLocale ensures periods in decimal numbers
scanner.useDelimiter("\\s*,\\s*|\\r?|#[^\\n]"); // useDelimiter with regular expression skips comments
String firstLine = scanner.nextLine().trim();
if (!firstLine.startsWith("Affine2D") && !firstLine.startsWith("Julia")){
throw new IllegalStateException("Invalid Chaos Game");
}
double x0min = scanner.nextDouble();
double x1min = scanner.nextDouble();
minimumVector = new Vector2D(x0min,x1min);
double x0max = scanner.nextDouble();
double x1max = scanner.nextDouble();
maximumVector = new Vector2D(x0max,x1max);
while(scanner.hasNextDouble()){
if (firstLine.startsWith("Affine2D")){
for (int i = 0; i < 3; i++) {
double a00 = scanner.nextDouble();
double a01 = scanner.nextDouble();
double a10 = scanner.nextDouble();
double a11 = scanner.nextDouble();
Matrix2x2 matrix2x2 = new Matrix2x2(a00, a01, a10, a11);
double b0 = scanner.nextDouble();
double b1 = scanner.nextDouble();
Vector2D vector2D = new Vector2D(b0, b1);
Transform2D transform2D = new AffineTransform2D(matrix2x2, vector2D);
transforms.add(transform2D);
}
}else if(firstLine.startsWith("Julia")){
double realOfComplex = scanner.nextDouble();
double imaginaryOfComplex = scanner.nextDouble();
Complex complex = new Complex(realOfComplex,imaginaryOfComplex);
Transform2D juliaTransformPositive = new JuliaTransform(complex,1);
Transform2D juliaTransformNegative = new JuliaTransform(complex,-1);
transforms.add(juliaTransformPositive);
transforms.add(juliaTransformNegative);
}
}
return new ChaosGameDescription(minimumVector,maximumVector,transforms);
}
catch (IOException e){
throw new IOException("Failure to read file",e);
/**
* Creates a ChaosGameDescription based on the file found at the input path.
* @param path A String input which gives the path to a file describing a chaos game.
* @return A {@link ChaosGameDescription} description of a chaos game.
*/
public ChaosGameDescription readFromFile(String path) throws IOException {
Vector2D minimumVector;
Vector2D maximumVector;
List<Transform2D> transforms = new ArrayList<>();
Path paths = Paths.get(path);
try (Scanner scanner = new Scanner(Files.newInputStream(paths))){
scanner.useLocale(Locale.ENGLISH); // useLocale ensures periods in decimal numbers
scanner.useDelimiter("\\s*,\\s*|\\r?|#[^\\n]"); // useDelimiter with regular expression skips comments
String firstLine = scanner.nextLine().trim();
if (!firstLine.startsWith("Affine2D") && !firstLine.startsWith("Julia")){
throw new IllegalStateException("Invalid Chaos Game");
}
double x0min = scanner.nextDouble();
double x1min = scanner.nextDouble();
minimumVector = new Vector2D(x0min,x1min);
double x0max = scanner.nextDouble();
double x1max = scanner.nextDouble();
maximumVector = new Vector2D(x0max,x1max);
while(scanner.hasNextDouble()){
if (firstLine.startsWith("Affine2D")){
for (int i = 0; i < 3; i++) {
double a00 = scanner.nextDouble();
double a01 = scanner.nextDouble();
double a10 = scanner.nextDouble();
double a11 = scanner.nextDouble();
Matrix2x2 matrix2x2 = new Matrix2x2(a00, a01, a10, a11);
double b0 = scanner.nextDouble();
double b1 = scanner.nextDouble();
Vector2D vector2D = new Vector2D(b0, b1);
Transform2D transform2D = new AffineTransform2D(matrix2x2, vector2D);
transforms.add(transform2D);
}
} else if(firstLine.startsWith("Julia")){
double realOfComplex = scanner.nextDouble();
double imaginaryOfComplex = scanner.nextDouble();
Complex complex = new Complex(realOfComplex,imaginaryOfComplex);
Transform2D juliaTransformPositive = new JuliaTransform(complex,1);
Transform2D juliaTransformNegative = new JuliaTransform(complex,-1);
transforms.add(juliaTransformPositive);
transforms.add(juliaTransformNegative);
}
}
return new ChaosGameDescription(minimumVector,maximumVector,transforms);
}
/**
* Writes to a file
* @param description A {@link ChaosGameDescription} description of the chaos game that should be written to file.
* @param path A String describing the path to the file that should be written to.
*/
public void writeToFile(ChaosGameDescription description, String path){
List<Transform2D> transforms = description.getTransforms();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))){
if (transforms.getFirst() instanceof AffineTransform2D){
writer.write("Affine2D\n");
} else if (transforms.getFirst() instanceof JuliaTransform){
writer.write("Julia\n");
}
Vector2D minVector2D = description.getMinCoords();
Vector2D maxVector2D = description.getMaxCoords();
writer.write(minVector2D.getX0() + "," + minVector2D.getX1());
writer.write(maxVector2D + ", " + minVector2D);
for(Transform2D transform : transforms){
if (transform instanceof AffineTransform2D affineTransform2D) {
Matrix2x2 matrix2x2 = affineTransform2D.getMatrix();
Vector2D vector2D = affineTransform2D.getVector();
writer.write(matrix2x2.getA00() + ", " + matrix2x2.getA01() + ", " + matrix2x2.getA10() + ", "
+ matrix2x2.getA11() + ", " + vector2D.getX0() + ", " + vector2D.getX1());
} else if (transform instanceof JuliaTransform juliaTransform){
Complex complex = juliaTransform.getC1();
writer.write(complex.getX0() + ", " + complex.getX1() + ", ");
}
}
} catch (IOException e) {
// Maybe replace with logger later based on SolarLint
System.out.println("File writing failure on path:" + path + "with error message:" + e.getMessage());
catch (IOException e){
throw new IOException("Failure to read file",e);
}
}
/**
* Reads from a file
* @param description A {@link ChaosGameDescription} description of the chaos game that should be written to file.
* @param path A String describing the path to the file that should be written to.
*/
public void writeToFile(ChaosGameDescription description, String path){
List<Transform2D> transforms = description.getTransforms();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))){
if (transforms.getFirst() instanceof AffineTransform2D){
writer.write("Affine2D\n");
} else if (transforms.getFirst() instanceof JuliaTransform){
writer.write("Julia\n");
}
Vector2D minVector2D = description.getMinCoords();
Vector2D maxVector2D = description.getMaxCoords();
writer.write(minVector2D.getX0() + "," + minVector2D.getX1());
writer.write(maxVector2D + ", " + minVector2D);
for(Transform2D transform : transforms){
if (transform instanceof AffineTransform2D affineTransform2D) {
Matrix2x2 matrix2x2 = affineTransform2D.getMatrix();
Vector2D vector2D = affineTransform2D.getVector();
writer.write(matrix2x2.getA00() + ", " + matrix2x2.getA01() + ", " + matrix2x2.getA10() + ", "
+ matrix2x2.getA11() + ", " + vector2D.getX0() + ", " + vector2D.getX1());
} else if (transform instanceof JuliaTransform juliaTransform){
Complex complex = juliaTransform.getC1();
writer.write(complex.getX0() + ", " + complex.getX1() + ", ");
}
}
} catch (IOException e) {
// Maybe replace with logger later based on SolarLint
System.out.println("File writing failure on path:" + path + "with error message:" + e.getMessage());
}
}
}
Affine2D
0.0,0.0
1.0,1.0
1.0,0.0,0.0,1.0,0.0,0.0
0.0,1.0,1.0,0.0,0.0,0.0
0.5,0.5,0.5,0.5,0.0,0.0
\ No newline at end of file
-1.0, -1.0
1.0, 1.0
0.5, 0.0, 0.0, 0.5, 0.0, 0.0
0.5, 0.0, 0.0, 0.5, 0.5, 0.0
0.5, 0.0, 0.0, 0.5, 0.0, 0.5
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment