diff --git a/.idea/.gitignore b/.idea/.gitignore index 13566b81b018ad684f3a35fee301741b2734c8f4..a9d7db9c0a81b2db47ca92e4e180b30090b27632 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -6,3 +6,5 @@ # Datasource local storage ignored files /dataSources/ /dataSources.local.xml +# GitHub Copilot persisted chat sessions +/copilot/chatSessions diff --git a/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosCanvas.java b/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosCanvas.java new file mode 100644 index 0000000000000000000000000000000000000000..bae6e16076d90cefcd6d1ed82403a37c818c6b8d --- /dev/null +++ b/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosCanvas.java @@ -0,0 +1,4 @@ +package edu.ntnu.stud.chaosgame.game; + +public class ChaosCanvas { +} diff --git a/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGame.java b/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGame.java new file mode 100644 index 0000000000000000000000000000000000000000..8090e64daf796983244860263cddb74daf9d6a72 --- /dev/null +++ b/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGame.java @@ -0,0 +1,4 @@ +package edu.ntnu.stud.chaosgame.game; + +public class ChaosGame { +} diff --git a/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescription.java b/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescription.java new file mode 100644 index 0000000000000000000000000000000000000000..4b55b0caea6a5dc044d9b5561fc6038faf9a76ea --- /dev/null +++ b/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescription.java @@ -0,0 +1,47 @@ +package edu.ntnu.stud.chaosgame.game; + +import edu.ntnu.stud.chaosgame.model.Vector2D; +import edu.ntnu.stud.chaosgame.transformations.Transform2D; + +import java.util.List; + +public class ChaosGameDescription { + private Vector2D minCoords; + private Vector2D maxCoords; + private List<Transform2D> transforms; + + /** + * Constructor for ChaosGameDescription + * @param minCoords Inputs a {@link Vector2D} vector of minimum coordinates for the chaos game. + * @param maxCoords Inputs a {@link Vector2D} vector of maximum coordinates for the chaos game. + * @param transforms Inputs a list of transformations {@link Transform2D} used in the chaos game. + */ + public ChaosGameDescription(Vector2D minCoords, Vector2D maxCoords, List<Transform2D> transforms) { + this.minCoords = minCoords; + this.maxCoords = maxCoords; + this.transforms = transforms; + } + + /** + * Getter method for transforms + * @return Returns a list of transforms in the chaos game. + */ + public List<Transform2D> getTransforms(){ + return transforms; + } + /** + * Getter method for minimum coordinates. + * @return Returns a Vector2D containing the minimum coordinates. + */ + public Vector2D getMinCoords(){ + return minCoords; + } + + /** + * Getter method for maximum coordinates. + * @return Returns a Vector2D containing the maximum coordinates. + */ + public Vector2D getMaxCoords(){ + return maxCoords; + } +} diff --git a/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandler.java b/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..d7fd4c9fb9aab35cf1c137c1da2d9a9a011625e1 --- /dev/null +++ b/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandler.java @@ -0,0 +1,126 @@ +package edu.ntnu.stud.chaosgame.game; + +import edu.ntnu.stud.chaosgame.model.Complex; +import edu.ntnu.stud.chaosgame.model.Matrix2x2; +import edu.ntnu.stud.chaosgame.model.Vector2D; +import edu.ntnu.stud.chaosgame.transformations.AffineTransform2D; +import edu.ntnu.stud.chaosgame.transformations.JuliaTransform; +import edu.ntnu.stud.chaosgame.transformations.Transform2D; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Scanner; +import java.util.logging.Logger; +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); + } + } + + /** + * 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()); + } + + } +} diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/Matrix2x2.java b/src/main/java/edu/ntnu/stud/chaosgame/model/Matrix2x2.java index 95ba011cf7d8e6453ecdd2da0df0fac1683b4013..58c165ba1037597f8e2949a2c0f310273bd84785 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/Matrix2x2.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/Matrix2x2.java @@ -49,4 +49,23 @@ public class Matrix2x2 { return new Vector2D(a00 * v.getX0() + a01 * v.getX1(), a10 * v.getX0() + a11 * v.getX1()); } + /** + * + * @return + */ + public double getA00() { + return a00; + } + + public double getA01() { + return a01; + } + + public double getA10() { + return a10; + } + + public double getA11() { + return a11; + } } diff --git a/src/main/java/edu/ntnu/stud/chaosgame/transformations/AffineTransform2D.java b/src/main/java/edu/ntnu/stud/chaosgame/transformations/AffineTransform2D.java index 2c07e71c3c713cec753ab073b718882ad32e1852..a3ff094ffa1756328ee2ca28a7b27d01bd8cf8ce 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/transformations/AffineTransform2D.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/transformations/AffineTransform2D.java @@ -13,11 +13,11 @@ public class AffineTransform2D extends Transform2D { /** * The matrix{@link Matrix2x2} which performs the matrix-multiplication part of the affine transformation. */ - Matrix2x2 matrix; + private Matrix2x2 matrix; /** * The vector{@link Vector2D} which is added as part of the affine transformation. */ - Vector2D vector; + private Vector2D vector; /** * Create a type of affine transformation. @@ -38,4 +38,14 @@ public class AffineTransform2D extends Transform2D { return matrix.multiply(point).add(vector); } + + /** + * Getter method to use with {@link edu.ntnu.stud.chaosgame.game.ChaosGameFileHandler} + * @return The matrix for the transformation + */ + public Matrix2x2 getMatrix(){return this.matrix;} + + public Vector2D getVector() { + return vector; + } } diff --git a/src/main/java/edu/ntnu/stud/chaosgame/transformations/JuliaTransform.java b/src/main/java/edu/ntnu/stud/chaosgame/transformations/JuliaTransform.java index 9b0e1f419036eeaa6fb289220c368d12d1cd90bb..3833208512150e938d679017b794a16d408efb5e 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/transformations/JuliaTransform.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/transformations/JuliaTransform.java @@ -44,4 +44,10 @@ public class JuliaTransform extends Transform2D { return new Complex(temp1.getX0(), temp1.getX1()).sqrt(); } + /** + * Getter method to use with {@link edu.ntnu.stud.chaosgame.game.ChaosGameFileHandler} + * @return The complex number used in the transformation. + */ + public Complex getC1(){return this.c1;} + }