diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameFileHandler.java b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameFileHandler.java index 78e9c65a1a2f6a256b02531a439dff7ac40e3140..544ced9e628b644a2c59fe20e4d73d09849985e8 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameFileHandler.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameFileHandler.java @@ -37,42 +37,71 @@ public class ChaosGameFileHandler { scanner.useDelimiter("\\s*,\\s*|\\s*\\r?\\n+\\s*|(?m)^#.*$"); String firstLine = scanner.nextLine().trim(); - if (!firstLine.equals("Affine2D")) { - throw new IllegalStateException("Invalid Chaos Game, expected 'Affine2D'."); - } + if (firstLine.equals("Affine2D")) { + + // Read the minimum vector + if (scanner.hasNextDouble()) { + double x0min = scanner.nextDouble(); + double x1min = scanner.nextDouble(); + minimumVector = new Vector2D(x0min, x1min); + } else { + throw new IllegalArgumentException("Expected minimum vector coordinates."); + } - // Read the minimum vector - if (scanner.hasNextDouble()) { - double x0min = scanner.nextDouble(); - double x1min = scanner.nextDouble(); - minimumVector = new Vector2D(x0min, x1min); - } else { - throw new IllegalArgumentException("Expected minimum vector coordinates."); - } + // Read the maximum vector + if (scanner.hasNextDouble()) { + double x0max = scanner.nextDouble(); + double x1max = scanner.nextDouble(); + maximumVector = new Vector2D(x0max, x1max); + } else { + throw new IllegalArgumentException("Expected maximum vector coordinates."); + } - // Read the maximum vector - if (scanner.hasNextDouble()) { - double x0max = scanner.nextDouble(); - double x1max = scanner.nextDouble(); - maximumVector = new Vector2D(x0max, x1max); - } else { - throw new IllegalArgumentException("Expected maximum vector coordinates."); - } - // Read the transforms - while (scanner.hasNextDouble()) { - double a00 = scanner.nextDouble(); - double a01 = scanner.nextDouble(); - double a10 = scanner.nextDouble(); - double a11 = scanner.nextDouble(); - double b0 = scanner.nextDouble(); - double b1 = scanner.nextDouble(); - - Matrix2x2 matrix2x2 = new Matrix2x2(a00, a01, a10, a11); - Vector2D vector2D = new Vector2D(b0, b1); - Transform2D transform2D = new AffineTransform2D(matrix2x2, vector2D); - transforms.add(transform2D); - } + + // Read the transforms + while (scanner.hasNextDouble()) { + double a00 = scanner.nextDouble(); + double a01 = scanner.nextDouble(); + double a10 = scanner.nextDouble(); + double a11 = scanner.nextDouble(); + double b0 = scanner.nextDouble(); + double b1 = scanner.nextDouble(); + + Matrix2x2 matrix2x2 = new Matrix2x2(a00, a01, a10, a11); + Vector2D vector2D = new Vector2D(b0, b1); + Transform2D transform2D = new AffineTransform2D(matrix2x2, vector2D); + transforms.add(transform2D); + + }} else if(firstLine.equals("Julia")){ + // Read the minimum vector + if (scanner.hasNextDouble()) { + double x0min = scanner.nextDouble(); + double x1min = scanner.nextDouble(); + minimumVector = new Vector2D(x0min, x1min); + } else { + throw new IllegalArgumentException("Expected minimum vector coordinates."); + } + + // Read the maximum vector + if (scanner.hasNextDouble()) { + double x0max = scanner.nextDouble(); + double x1max = scanner.nextDouble(); + maximumVector = new Vector2D(x0max, x1max); + } else { + throw new IllegalArgumentException("Expected maximum vector coordinates."); + } + + // Read the transforms + while(scanner.hasNextDouble()){ + double cReal = scanner.nextDouble(); + double cImag = scanner.nextDouble(); + Complex complex = new Complex(cReal,cImag); + JuliaTransform juliaTransform = new JuliaTransform(complex,1); + transforms.add(juliaTransform); + } + + } else{throw new IllegalStateException("Invalid Chaos Game, expected 'Affine2D' or 'Juli'.");} } catch (IOException e) { throw new IOException("Failure to read file", e); } @@ -124,3 +153,4 @@ public class ChaosGameFileHandler { } } + diff --git a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java index f8854ca44b1a9b45532ce932485cefc6c563003a..b220186db8cc94ca68f16c1780963341565a7462 100644 --- a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java +++ b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java @@ -26,7 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; class ChaosGameFileHandlerTest { @Test - void testReadFromFile() throws IOException { + void testReadFromFileAffine2D() throws IOException { String fileContent = """ Affine2D 0.0, 0.0 @@ -64,6 +64,40 @@ class ChaosGameFileHandlerTest { } } + @Test + void testReadFromFileJulia() throws IOException { + String fileContent = """ + Julia + -1.6, -1 + 1.6, 1 + -.74543, .11301 + """; + + Path tempFile = Files.createTempFile("test", ".txt"); + Files.writeString(tempFile, fileContent); + ChaosGameFileHandler fileHandler = new ChaosGameFileHandler(); + ChaosGameDescription description = fileHandler.readFromFile(tempFile.toString()); + + Vector2D minCoords = description.getMinCoords(); + Vector2D maxCoords = description.getMaxCoords(); + + assertEquals(-1.6, minCoords.getX0()); + assertEquals(-1, minCoords.getX1()); + assertEquals(1.6, maxCoords.getX0()); + assertEquals(1, maxCoords.getX1()); + + for (var transform : description.getTransforms()) { + assertEquals(JuliaTransform.class, transform.getClass()); + JuliaTransform julia = (JuliaTransform) transform; + Complex complex = julia.getC1(); + + + assertEquals(-.74543, complex.getX0()); + assertEquals(.11301, complex.getX1()); + + } + } + @Test void testAffine2DWriteToFile() throws IOException { // Create a ChaosGameDescription with three transforms