diff --git a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/UserInterface.java b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/UserInterface.java new file mode 100644 index 0000000000000000000000000000000000000000..710a07c757411ee532fb1eccf768a2654398489e --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/UserInterface.java @@ -0,0 +1,96 @@ +package edu.ntnu.idatt2003.mappevurderingprog2.models; + +import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame; +import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGameDescription; +import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGameFileHandler; +import java.util.Scanner; + +public class UserInterface { + private ChaosGameDescription description; + private ChaosGame game; + private final ChaosGameFileHandler fileHandler = new ChaosGameFileHandler(); + private final Scanner scanner = new Scanner(System.in); + public void start() { + System.out.println("CLI Application for Chaos Game"); + while (true) { + System.out.println("\n1. Read description from file"); + System.out.println("2. Write description to file"); + System.out.println("3. Run a number of iterations"); + System.out.println("4. Print ASCII fractal to console"); + System.out.println("5. Exit"); + System.out.print("Enter choice: "); + int choice = scanner.nextInt(); + scanner.nextLine(); + + switch (choice) { + case 1: + readDescriptionFromFile(); + break; + case 2: + writeDescriptionToFile(); + break; + case 3: + runIterations(); + break; + case 4: + printFractal(); + break; + case 5: + System.out.println("Exiting..."); + return; + default: + System.out.println("Invalid choice. Please try again."); + } + } + } + + private void readDescriptionFromFile() { + System.out.print("Enter the name of the text file (e.g., JuliaTransformation.txt): "); + String fileName = scanner.nextLine(); + String filePath = "src/main/resources/transformations/" + fileName; + + try { + description = fileHandler.readTransformationsFromFile(filePath); + System.out.println("Description read successfully from: " + filePath); + } catch (Exception e) { + System.out.println("Error reading file: " + e.getMessage()); + } + } + + private void writeDescriptionToFile() { + if (description == null) { + System.out.println("No description to write. Please read a description first."); + return; + } + System.out.print("Enter the name of the text file to write to (e.g., JuliaTransformation.txt): "); + String fileName = scanner.nextLine(); + String filePath = "src/main/resources/transformations/" + fileName; + + try { + fileHandler.writeTransformationsToFile(description, filePath); + System.out.println("Description written successfully to: " + filePath); + } catch (Exception e) { + System.out.println("Error writing to file: " + e.getMessage()); + } + } + + private void runIterations() { + if (description == null) { + System.out.println("No description loaded. Please read a description first."); + return; + } + System.out.print("Enter the number of iterations to run: "); + int iterations = scanner.nextInt(); + game = new ChaosGame(description, 50, 50); + game.runSteps(iterations); + System.out.println("Iterations completed."); + } + + private void printFractal() { + if (game == null || game.getCanvas() == null) { + System.out.println("No game to print. Please run iterations first."); + return; + } + game.getCanvas().printCanvas(); + } +} \ No newline at end of file diff --git a/src/main/resources/transformations/BarnsleyFern.txt b/src/main/resources/transformations/BarnsleyFern.txt new file mode 100644 index 0000000000000000000000000000000000000000..647a7a0b44406f0aacef5788d039dcc120639504 --- /dev/null +++ b/src/main/resources/transformations/BarnsleyFern.txt @@ -0,0 +1,7 @@ +Affine2D +0.0, 0.0 +1.0, 1.0 +0.000000, 0.000000, 0.000000, 0.160000, 0.0, 0.0 +0.850000, 0.040000, -0.040000, 0.850000, 0.0, 1.6 +0.200000, -0.260000, 0.230000, 0.220000, 0.0, 1.6 +-0.150000, 0.280000, 0.260000, 0.240000, 0.0, 0.44 \ No newline at end of file diff --git a/src/main/resources/transformations/JuliaTransformation.txt b/src/main/resources/transformations/JuliaTransformation.txt new file mode 100644 index 0000000000000000000000000000000000000000..d5b7598f3c808e59c78b2252a371ad5792858589 --- /dev/null +++ b/src/main/resources/transformations/JuliaTransformation.txt @@ -0,0 +1,4 @@ +Julia +-1.6, -1.0 +1.6, 1.0 +-0.74543, 0.11301, -1 diff --git a/src/main/resources/transformations/SierpinskiTriangle.txt b/src/main/resources/transformations/SierpinskiTriangle.txt new file mode 100644 index 0000000000000000000000000000000000000000..ad501cb8dcaceb9a3af075d54be5903e416beaed --- /dev/null +++ b/src/main/resources/transformations/SierpinskiTriangle.txt @@ -0,0 +1,6 @@ +Affine2D +0.0, 0.0 +1.0, 1.0 +0.500000, 0.000000, 0.000000, 0.500000, 0.0, 0.0 +0.500000, 0.000000, 0.000000, 0.500000, 0.5, 0.0 +0.500000, 0.000000, 0.000000, 0.500000, 0.25, 0.5 diff --git a/src/test/java/edu/ntnu/idatt2003/mappevurderingprog2/JuliaTranformNegativeTest.java b/src/test/java/edu/ntnu/idatt2003/mappevurderingprog2/JuliaTranformNegativeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9a6925e3962091da1cbb8c8d5f8548437b2ec485 --- /dev/null +++ b/src/test/java/edu/ntnu/idatt2003/mappevurderingprog2/JuliaTranformNegativeTest.java @@ -0,0 +1,60 @@ +package edu.ntnu.idatt2003.mappevurderingprog2; + +import edu.ntnu.idatt2003.mappevurderingprog2.models.Complex; +import edu.ntnu.idatt2003.mappevurderingprog2.models.JuliaTransform; +import edu.ntnu.idatt2003.mappevurderingprog2.models.Vector2D; +import org.junit.jupiter.api.Test; + +public class JuliaTranformNegativeTest { + @Test + public void juliaTransformConstructorNegativeTest() { + int i = 1; + try { + JuliaTransform juliaTransform = new JuliaTransform(null, i); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + + @Test + public void getPointNegativeTest() { + Complex complex = new Complex(1, 1); + int i = 1; + JuliaTransform juliaTransform = new JuliaTransform(complex, i); + assert juliaTransform.getPoint().getRealPart() != 2; + assert juliaTransform.getPoint().getImaginaryPart() != 2; + } + + @Test + public void getSignNegativeTest() { + Complex complex = new Complex(1, 1); + int i = 1; + JuliaTransform juliaTransform = new JuliaTransform(complex, i); + assert juliaTransform.getSign() != 2; + } + + @Test + public void transformNegativeTest1() { + Complex juliaPoint = new Complex(0.5, -0.5); + int sign = 1; + JuliaTransform juliaTransform = new JuliaTransform(juliaPoint, sign); + try { + Vector2D result = juliaTransform.transform(null); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + + @Test + public void transformNegativeTest2() { + Vector2D complexToTransform = new Vector2D(1, 1); + Complex juliaPoint = new Complex(0.5, -0.5); + int sign = 1; + JuliaTransform juliaTransform = new JuliaTransform(juliaPoint, sign); + try { + Vector2D result = juliaTransform.transform(complexToTransform); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } +} \ No newline at end of file