From 18fcfd4150b3dacc984fa6290909dd75d15ea4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Daleng?= <142524365+MrMarHVD@users.noreply.github.com> Date: Mon, 1 Apr 2024 16:38:34 +0200 Subject: [PATCH] Improved putPixel()-method to account for negative indices, which previously cause error when trying to produce Barnsley fern. --- src/main/java/edu/ntnu/stud/chaosgame/Main.java | 11 +++++++---- .../stud/chaosgame/model/game/ChaosCanvas.java | 16 ++++++++++++++-- .../resources/descriptions/factory/desc_2.txt | 7 ++++--- .../{Sierpinski.txt => output/output_1.txt} | 0 .../{output.txt => templates/sierpinski.txt} | 0 5 files changed, 25 insertions(+), 9 deletions(-) rename src/main/resources/descriptions/{Sierpinski.txt => output/output_1.txt} (100%) rename src/main/resources/descriptions/{output.txt => templates/sierpinski.txt} (100%) diff --git a/src/main/java/edu/ntnu/stud/chaosgame/Main.java b/src/main/java/edu/ntnu/stud/chaosgame/Main.java index befa3b7..6378a74 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/Main.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/Main.java @@ -17,16 +17,19 @@ public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); System.out.println("Hi"); - String path = "src/main/resources/descriptions/sierpinski.txt"; + String path = "src/main/resources/descriptions/templates/sierpinski.txt"; ChaosGameDescription description = null; // Declare description ChaosGame game = null; + ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory(); + try { //ChaosGameDescription description = new ChaosGameFileHandler().readFromFile(path); //description = new ChaosGameDescription(minCoords, maxCoords, transforms); - description = new ChaosGameFileHandler().readFromFile(path); + //description = new ChaosGameFileHandler().readFromFile(path); + description = factory.getDescriptions().get(1); ChaosCanvas canvas = new ChaosCanvas(100, 100, description.getMinCoords(), description.getMaxCoords()); // Create canvas game = new ChaosGame(description, canvas); @@ -54,8 +57,8 @@ public class Main { if (Objects.equals(sc.next(), "1")) { ChaosGameFileHandler handler = new ChaosGameFileHandler(); - handler.writeToFile(description, "src/main/resources/descriptions/output.txt"); - System.out.println("Wrote to file output.txt."); + handler.writeToFile(description, "src/main/resources/descriptions/output/output_1.txt"); + System.out.println("Wrote to file output_1.txt."); } else { exit = true; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java index f8826be..5710a8c 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java @@ -78,9 +78,21 @@ public class ChaosCanvas { [(int) transformCoordsToIndices.transform(point).getX1()]; } + /** + * Place a pixel on the canvas. + * + * @param point the point where the pixel is to be placed. + */ public void putPixel(Vector2D point) { - canvas[(int) transformCoordsToIndices.transform(point).getX0()] - [(int) transformCoordsToIndices.transform(point).getX1()] = 1; + int xOffset = (int) Math.abs(minCoords.getX0()); + int yOffset = (int) Math.abs(minCoords.getX1()); + + int xIndex = (int) transformCoordsToIndices.transform(point).getX0() + xOffset; + int yIndex = (int) transformCoordsToIndices.transform(point).getX1() + yOffset; + + if (xIndex >= 0 && xIndex < width && yIndex >= 0 && yIndex < height) { + canvas[xIndex][yIndex] = 1; + } } /** diff --git a/src/main/resources/descriptions/factory/desc_2.txt b/src/main/resources/descriptions/factory/desc_2.txt index e98ca28..989c78f 100644 --- a/src/main/resources/descriptions/factory/desc_2.txt +++ b/src/main/resources/descriptions/factory/desc_2.txt @@ -1,6 +1,7 @@ Affine2D 0.0, 0.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.25, 0.5 -0.5, 0.0, 0.0, 0.5, 0.5, 0.0 +0.0, 0.0, 0.0, 0.16, 0.0, 0.0 +0.85, 0.04, -0.04, 0.85, 0.0, 1.6 +0.2, -0.26, 0.23, 0.22, 0.0, 0.16 +-0.15, 0.28, 0.26, 0.24, 0, 0.44 diff --git a/src/main/resources/descriptions/Sierpinski.txt b/src/main/resources/descriptions/output/output_1.txt similarity index 100% rename from src/main/resources/descriptions/Sierpinski.txt rename to src/main/resources/descriptions/output/output_1.txt diff --git a/src/main/resources/descriptions/output.txt b/src/main/resources/descriptions/templates/sierpinski.txt similarity index 100% rename from src/main/resources/descriptions/output.txt rename to src/main/resources/descriptions/templates/sierpinski.txt -- GitLab