diff --git a/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGame.java b/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGame.java index 113afb5f991ef0a9cfc5b5a2b2859cc15936958f..399ebea2df5cb56835b590b26a9c1fbaa26a9b01 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGame.java +++ b/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGame.java @@ -20,10 +20,12 @@ public class ChaosGame { /** * Creates a new ChaosGame with the given description, width, and height. + * * @param description the description * @param width the width * @param height the height */ + public ChaosGame(ChaosGameDescription description, int width, int height) { if (description == null) { throw new NullPointerException( @@ -69,6 +71,13 @@ public class ChaosGame { } } + /** + * Adds an observer to the list of observers. + * The observer will be updated when the chaos game changes. + * + * @param observer the observer to add. + */ + public void addObserver(ChaosGameObserver observer) { if (!observers.contains(observer)) { @@ -76,16 +85,34 @@ public class ChaosGame { } } + /** + * Removes an observer from the list of observers. + * The observer will no longer be updated when the chaos game changes. + * + * @param observer the observer to remove from the list. + */ + public void removeObserver(ChaosGameObserver observer) { observers.remove(observer); } + /** + * Updates all observers about the changes in the chaos game. + * Method is called when the chaos game changes. + */ + protected void updateObservers() { for (ChaosGameObserver observer : observers) { observer.update(); } } + /** + * Sets a new game description for the chaos game. + * + * @param newDescription the new description to set. + */ + public void setNewGameDescription(ChaosGameDescription newDescription) { this.description = newDescription; this.canvas = new ChaosCanvas( diff --git a/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGameDescriptionFactory.java b/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGameDescriptionFactory.java index 97b83cdb10c6e68cd405888fe65259d4e6c13f3b..c8759c4bf3da08209b3fc96a398d52420a4d7bdf 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGameDescriptionFactory.java +++ b/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGameDescriptionFactory.java @@ -7,8 +7,22 @@ import edu.ntnu.idatt2003.model.transformations.AffineTransform2D; import edu.ntnu.idatt2003.model.transformations.JuliaTransform; import java.util.List; +/** + * Represents a factory for creating ChaosGameDescription objects. + * The factory creates different types of chaos games based on the description, + * such as Julia, Barnsley, and Sierpinski. + * It is also used to create the chaos game based on the description. + */ public class ChaosGameDescriptionFactory { + /** + * Returns a ChaosGameDescription object based on the description and the complex number c. + * + * @param description the description of the chaos game. + * @param c the complex number used in the Julia transformation. + * @return a ChaosGameDescription object based on the description. + * + */ public static ChaosGameDescription get(String description, Complex c) { String type = description.toUpperCase(); @@ -20,6 +34,13 @@ public class ChaosGameDescriptionFactory { }; } + /** + * Creates a Julia chaos game based on the complex number c. + * + * @param c the complex number used in the Julia transformation. + * @return a ChaosGameDescription object representing the Julia chaos game. + */ + private static ChaosGameDescription createJulia(Complex c) { return new ChaosGameDescription( List.of(new JuliaTransform(c, 1)), @@ -28,6 +49,12 @@ public class ChaosGameDescriptionFactory { ); } + /** + * Creates a Sierpinski chaos game. + * + * @return a ChaosGameDescription object representing the Sierpinski chaos game. + */ + private static ChaosGameDescription createSierpinski() { return new ChaosGameDescription( List.of( @@ -40,6 +67,12 @@ public class ChaosGameDescriptionFactory { ); } + /** + * Creates a Barnsley chaos game. + * + * @return a ChaosGameDescription object representing the Barnsley chaos game. + */ + private static ChaosGameDescription createBarnsley() { return new ChaosGameDescription( List.of( diff --git a/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGameFileHandler.java b/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGameFileHandler.java index 7922b6b31016d699e4ab6c8d76a4e1ccc87d2b9a..7ed55e365bec69b342b6954d3135e3c6010ef253 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGameFileHandler.java +++ b/src/main/java/edu/ntnu/idatt2003/model/chaos/ChaosGameFileHandler.java @@ -15,9 +15,22 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +/** + * Handles reading and writing of chaos game descriptions to and from files. + * This class supports different transformations such as Affine2D and Julia. + */ + public class ChaosGameFileHandler { + /** + * Reads a chaos game description from a file. + * + * @param filePath filePath to the file containing the chaos game description. + * @return a ChaosGameDescription object representing the chaos game description. + * @throws RuntimeException if the file could not be read. + */ + public ChaosGameDescription readFromFile(String filePath) { try { File file = new File(filePath); @@ -29,6 +42,14 @@ public class ChaosGameFileHandler { } } + /** + * Processes the content of the buffered reader to create a chaos game description. + * + * @param bufferedReader the buffered reader containing the chaos game description. + * @return a ChaosGameDescription object representing the chaos game description. + * @throws IOException if the file could not be read. + */ + private ChaosGameDescription processFile(BufferedReader bufferedReader) throws IOException { String transformationType = skipComments(bufferedReader.readLine()); @@ -47,11 +68,26 @@ public class ChaosGameFileHandler { return new ChaosGameDescription(transformations, minCoords, maxCoords); } + /** + * Skips comments in a line. + * + * @param line the line to skip comments in. + * @return the line without comments. + */ + private String skipComments(String line) { String[] parts = line.split("#"); return parts[0].trim(); } + /** + * Writes a chaos game description to a file. + * + * @param description the chaos game object to write to file. + * @param path the path to the file to write to. + * @throws IOException if the file could not be written. + */ + public void writeToFile(ChaosGameDescription description, String path) throws IOException { try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) { @@ -69,6 +105,15 @@ public class ChaosGameFileHandler { } } + /** + * Writes the type of transformation to the file. + * + * @param transform the transformation to write to file. + * @param writer the writer to use for writing to file. + * @throws IOException if the file could not be written. + * @throws IllegalArgumentException if the transformation type is not supported. + */ + private void writeTransformationType(Transform2D transform, BufferedWriter writer) throws IOException { @@ -82,12 +127,32 @@ public class ChaosGameFileHandler { writer.newLine(); } + /** + * Writes the coordinates to the file. + * + * @param coords the coordinates to write to file. + * @param label the label to write to file. + * @param writer the writer to use for writing to file. + * @throws IOException if the file could not be written. + */ + private void writeCoordinates(Vector2D coords, String label, BufferedWriter writer) throws IOException { writer.write(coords.getX0() + ", " + coords.getX1() + " # " + label); writer.newLine(); } + /** + * Writes the details of the transformation to the file. + * The details are written based on the type of transformation. + * + * @param transform the transformation to write to file. + * @param count the order number of the transformation. + * @param writer the writer to use for outputting data. + * @throws IOException if the file could not be written. + * @throws IllegalArgumentException if the transformation type is not supported. + */ + private void writeTransformDetails(Transform2D transform, int count, BufferedWriter writer) throws IOException { @@ -102,6 +167,15 @@ public class ChaosGameFileHandler { writer.newLine(); } + /** + * Writes the details of an affine transformation to the file. + * + * @param affine the affine transformation to write to file. + * @param count the order number of the transformation. + * @param writer the writer to use for outputting data. + * @throws IOException if the file could not be written. + */ + private void writeAffineTransformDetails( AffineTransform2D affine, int count, BufferedWriter writer) throws IOException { @@ -114,6 +188,14 @@ public class ChaosGameFileHandler { writer.write(details); } + /** + * Writes the details of a Julia transformation to the file. + * + * @param julia the Julia transformation to write to file. + * @param writer the writer to use for outputting data. + * @throws IOException if the file could not be written. + */ + private void writeJuliaTransformDetails(JuliaTransform julia, BufferedWriter writer) throws IOException { Complex complexPoint = julia.getPoint(); @@ -122,6 +204,14 @@ public class ChaosGameFileHandler { writer.write(details); } + /** + * Selects the transformation based on the type of transformation. + * + * @param typeOfTransformation the type of transformation to select. + * @param line the line containing the transformation details. + * @return the transformation object. + * @throws IllegalArgumentException if the transformation type is not supported. + */ private Transform2D selectTransformation(String typeOfTransformation, String line) throws IllegalArgumentException { @@ -134,6 +224,13 @@ public class ChaosGameFileHandler { }; } + /** + * Parses a Vector2D from a given line of text. + * + * @param line the line to parse the vector from. + * @return the parsed Vector2D object. + */ + private Vector2D parseVector(String line) { String numbers = skipComments(line); String[] vectorParts = numbers.split(","); @@ -144,6 +241,13 @@ public class ChaosGameFileHandler { return new Vector2D(x0, x1); } + /** + * Parses a Julia transformation from a given line of text. + * + * @param line the line to parse the transformation from. + * @return the parsed Julia transformation object. + */ + private Transform2D parseJuliaTransformation(String line) { String numbers = skipComments(line); String[] parts = numbers.split(","); @@ -155,6 +259,13 @@ public class ChaosGameFileHandler { return new JuliaTransform(complexNumber, 1); } + /** + * Parses an affine transformation from a given line of text. + * + * @param line the line to parse the transformation from. + * @return the parsed AffineTransform2D object. + */ + private Transform2D parseAffineTransformation(String line) { String numbers = skipComments(line); String[] transformParts = numbers.split(","); @@ -170,7 +281,4 @@ public class ChaosGameFileHandler { return new AffineTransform2D((matrix), vector); } - - - } diff --git a/src/main/java/edu/ntnu/idatt2003/model/math/Complex.java b/src/main/java/edu/ntnu/idatt2003/model/math/Complex.java index 9ae198a81343bac5a5a364afedbdff93ed7a356e..df272111fbcfed59cdbb6b9481c010d99d2b5b35 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/math/Complex.java +++ b/src/main/java/edu/ntnu/idatt2003/model/math/Complex.java @@ -4,6 +4,7 @@ package edu.ntnu.idatt2003.model.math; * Represents a complex number in 2D space. * The real part is the x0 coordinate and the imaginary part is the x1 coordinate. */ + public class Complex extends Vector2D { /** @@ -12,11 +13,11 @@ public class Complex extends Vector2D { * @param realPart the real part * @param imaginaryPart the imaginary part */ + public Complex(double realPart, double imaginaryPart) { super(realPart, imaginaryPart); } - /** * Returns the square root of this complex number. * @@ -32,7 +33,6 @@ public class Complex extends Vector2D { ); } - /** * Adds the given complex number to this complex number. * @@ -47,7 +47,6 @@ public class Complex extends Vector2D { return new Complex(this.getX0() + otherComplex.getX0(), this.getX1() + otherComplex.getX1()); } - /** * Subtracts the given complex number from this complex number. * diff --git a/src/main/java/edu/ntnu/idatt2003/model/math/Matrix2x2.java b/src/main/java/edu/ntnu/idatt2003/model/math/Matrix2x2.java index 5ba72b6a9cc38288d122b2289360cd4e9bc601ea..0b106e5ff5a12c06fabb6ec069aeb030b7bd413c 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/math/Matrix2x2.java +++ b/src/main/java/edu/ntnu/idatt2003/model/math/Matrix2x2.java @@ -3,13 +3,13 @@ package edu.ntnu.idatt2003.model.math; /** * Represents a 2x2 matrix. */ + public class Matrix2x2 { private double a00; private double a01; private double a10; private double a11; - /** * Creates a new 2x2 matrix with the given elements. * @@ -18,6 +18,7 @@ public class Matrix2x2 { * @param a10 second row first column * @param a11 second row second column */ + public Matrix2x2(double a00, double a01, double a10, double a11) { if (Double.isNaN(a00) || Double.isNaN(a01) || Double.isNaN(a10) || Double.isNaN(a11)) { throw new IllegalArgumentException("Matrix parameters must be a valid number."); @@ -28,7 +29,6 @@ public class Matrix2x2 { this.a11 = a11; } - public double getA00() { return a00; } diff --git a/src/main/java/edu/ntnu/idatt2003/model/math/Vector2D.java b/src/main/java/edu/ntnu/idatt2003/model/math/Vector2D.java index fd74998ffc04f66123a0386396b523cb5fd0c708..ecc42dd9c4fe332673b601e3be7eba3b93a333fb 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/math/Vector2D.java +++ b/src/main/java/edu/ntnu/idatt2003/model/math/Vector2D.java @@ -3,11 +3,11 @@ package edu.ntnu.idatt2003.model.math; /** * Represents a 2D vector. */ + public class Vector2D { private double x0; private double x1; - /** * Creates a new 2D vector with the given elements. * @@ -22,33 +22,33 @@ public class Vector2D { this.x1 = x1; } - /** * Returns the first element of this vector. * * @return the first element. */ + public double getX0() { return x0; } - /** * Returns the second element of this vector. * * @return the second element */ + public double getX1() { return x1; } - /** * Adds the given vector to this vector. * * @param other the vector to add. * @return the resulting vector */ + public Vector2D add(Vector2D other) { if (other == null) { throw new NullPointerException("Vector to be added with can not be null"); @@ -56,13 +56,13 @@ public class Vector2D { return new Vector2D(this.x0 + other.x0, this.x1 + other.x1); } - /** * Subtracts the given vector from this vector. * * @param other the vector to subtract * @return the resulting vector */ + public Vector2D subtract(Vector2D other) { if (other == null) { throw new NullPointerException("Vector to be subtracted with can not be null"); diff --git a/src/main/java/edu/ntnu/idatt2003/model/transformations/AffineTransform2D.java b/src/main/java/edu/ntnu/idatt2003/model/transformations/AffineTransform2D.java index be8102bf80872006dfa20ac7e0a92c71226e7e7a..246ac7976c664b0d80dec03f576eb1fd6542b3b9 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/transformations/AffineTransform2D.java +++ b/src/main/java/edu/ntnu/idatt2003/model/transformations/AffineTransform2D.java @@ -6,17 +6,18 @@ import edu.ntnu.idatt2003.model.math.Vector2D; /** * Represents an affine transformation in 2D space. */ + public class AffineTransform2D implements Transform2D { private Matrix2x2 matrix; private Vector2D vector; - /** * Creates a new AffineTransform2D with the given matrix and vector. * * @param matrix 2x2 matrix * @param vector 2D vector */ + public AffineTransform2D(Matrix2x2 matrix, Vector2D vector) { if (matrix == null || vector == null) { throw new NullPointerException("Neither matrix or vector parameter can be null."); @@ -25,13 +26,13 @@ public class AffineTransform2D implements Transform2D { this.vector = vector; } - /** * Transforms the given point using this transformation. * * @param point the point to transform * @return the transformed point */ + @Override public Vector2D transform(Vector2D point) { if (point == null) { @@ -40,11 +41,22 @@ public class AffineTransform2D implements Transform2D { return matrix.multiply(point).add(vector); } + /** + * Returns the matrix of this transformation. + * + * @return the matrix + */ public Matrix2x2 getMatrix() { return matrix; } + /** + * Returns the vector of this transformation. + * + * @return the vector + */ + public Vector2D getVector() { return vector; } diff --git a/src/main/java/edu/ntnu/idatt2003/model/transformations/JuliaTransform.java b/src/main/java/edu/ntnu/idatt2003/model/transformations/JuliaTransform.java index 5130cd8340fee0ee5f647c0621bbd7cf4cce4f41..651347debcd474224a6fbbafcc9b562894fd394d 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/transformations/JuliaTransform.java +++ b/src/main/java/edu/ntnu/idatt2003/model/transformations/JuliaTransform.java @@ -6,17 +6,18 @@ import edu.ntnu.idatt2003.model.math.Vector2D; /** * Represents a Julia transformation in 2D space. */ + public class JuliaTransform implements Transform2D { private final Complex point; private final int sign; - /** * Creates a new JuliaTransform with the given point and sign. * * @param point the point. * @param sign the sign. */ + public JuliaTransform(Complex point, int sign) { if (sign != -1 && sign != 1) { throw new IllegalArgumentException("The sign can only be the values -1 or 1."); @@ -25,13 +26,13 @@ public class JuliaTransform implements Transform2D { this.sign = sign; } - /** * Transforms the given point using this transformation. * * @param point the point to transform. * @return the transformed point. */ + @Override public Vector2D transform(Vector2D point) { if (point == null) { @@ -49,8 +50,13 @@ public class JuliaTransform implements Transform2D { return new Vector2D(sqrtResult.getX0(), sqrtResult.getX1()); } + /** + * Returns the point of this transformation. + * + * @return the point. + */ + public Complex getPoint() { return point; } -} - +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/model/transformations/Transform2D.java b/src/main/java/edu/ntnu/idatt2003/model/transformations/Transform2D.java index 64af3568154b247844fb16a557617fd94ac9f62a..dcfb749c4dde2712938f4892233c1e8ed1808b6f 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/transformations/Transform2D.java +++ b/src/main/java/edu/ntnu/idatt2003/model/transformations/Transform2D.java @@ -5,6 +5,7 @@ import edu.ntnu.idatt2003.model.math.Vector2D; /** * Represents a 2D transformation. */ + public interface Transform2D { Vector2D transform(Vector2D point); } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java new file mode 100644 index 0000000000000000000000000000000000000000..3c2528dbb5dcd922695faf12009864977e47054a --- /dev/null +++ b/src/main/java/module-info.java @@ -0,0 +1,7 @@ +module edu.ntnu.idatt2003 { + requires javafx.controls; + requires javafx.fxml; + + opens edu.ntnu.idatt2003 to javafx.fxml; + exports edu.ntnu.idatt2003; +}