diff --git a/src/main/java/org/example/chaosgame/chaos/ChaosCanvas.java b/src/main/java/org/example/chaosgame/chaos/ChaosCanvas.java index a89bf59f165059316f90f589e856ad08ed334b31..f6f1dcf4bb774fd35fbf7080d8db1ac2cb99e67f 100644 --- a/src/main/java/org/example/chaosgame/chaos/ChaosCanvas.java +++ b/src/main/java/org/example/chaosgame/chaos/ChaosCanvas.java @@ -4,6 +4,16 @@ import org.example.chaosgame.linalg.Matrix2x2; import org.example.chaosgame.linalg.Vector2D; import org.example.chaosgame.transformations.AffineTransform2D; +/** + * A class representing a canvas for the chaos game. + * The canvas is a 2D grid of pixels, where each pixel is represented by an integer value. + * The canvas has a coordinate system, where the origin is in the lower left corner of the canvas, + * and the x-axis and y-axis are horizontal and vertical, respectively. + * The canvas has a minimum and maximum coordinate, which defines the extent of the canvas. + * The canvas also has an Affine transformation that maps coordinates, {@link Vector2D}, + * to indices in the canvas array. This is used to map points to pixels in the canvas. + */ + public class ChaosCanvas { private final int width; private final int height; @@ -12,6 +22,21 @@ public class ChaosCanvas { private final Vector2D maxCoords; private final AffineTransform2D transformCoordsToIndices; + /** + * Creates a new ChaosCanvas with the given width, height, minimum and maximum coordinates. + * The canvas is initialized with all pixel values set to 0. + * The Affine transformation is calculated based on the width, height, + * minimum and maximum coordinates. + * + * @param width The width of the canvas + * + * @param height The height of the canvas + * + * @param minCoords The minimum coordinates of the canvas + * + * @param maxCoords The maximum coordinates of the canvas + * + */ public ChaosCanvas(int width, int height, Vector2D minCoords, Vector2D maxCoords) { @@ -20,16 +45,18 @@ public class ChaosCanvas { this.minCoords = minCoords; this.maxCoords = maxCoords; this.transformCoordsToIndices = new AffineTransform2D( - new Matrix2x2(0.0, ((height - 1) / (minCoords.getY() - maxCoords.getY())), + new Matrix2x2(0.0, ((height - 1) + / (minCoords.getY() - maxCoords.getY())), ((width - 1) / (maxCoords.getX() - minCoords.getX())), 0.0), - new Vector2D((((height - 1.0) * maxCoords.getY()) / (maxCoords.getY() - minCoords.getY())), + new Vector2D((((height - 1.0) * maxCoords.getY()) + / (maxCoords.getY() - minCoords.getY())), ((width - 1.0) * minCoords.getX()) / (minCoords.getX() - maxCoords.getX()) )); this.canvas = new int[height][width]; } - public int[][] getCanvasArray(){ + public int[][] getCanvasArray() { return canvas; } @@ -49,14 +76,20 @@ public class ChaosCanvas { return maxCoords; } - public int getPixel(Vector2D point){ + public int getPixel(Vector2D point) { Vector2D indices = transformCoordsToIndices.transform(point); int x = (int) indices.getX(); int y = (int) indices.getY(); return canvas[x][y]; } - public void putPixel (Vector2D point){ + /** + * Increments the pixel value at the given point by 1. + * If the point is outside the canvas, the method does nothing. + * + * @param point The point to put a pixel at + */ + public void putPixel(Vector2D point) { Vector2D indices = transformCoordsToIndices.transform(point); int x = (int) indices.getX(); int y = (int) indices.getY(); @@ -65,9 +98,9 @@ public class ChaosCanvas { } } - public void clearCanvas(){ - for (int i = 0; i < height; i++){ - for (int j = 0; j < width; j++){ + public void clearCanvas() { + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { canvas[i][j] = 0; } } diff --git a/src/main/java/org/example/chaosgame/chaos/ChaosGame.java b/src/main/java/org/example/chaosgame/chaos/ChaosGame.java index 401e3f4b80f6994c326e3c8b0c2f57d726ef5d81..8ec78afcddfb88de5045eeacb6c587155339ca4d 100644 --- a/src/main/java/org/example/chaosgame/chaos/ChaosGame.java +++ b/src/main/java/org/example/chaosgame/chaos/ChaosGame.java @@ -1,25 +1,36 @@ package org.example.chaosgame.chaos; -import javafx.application.Platform; -import javafx.scene.image.PixelWriter; -import javafx.scene.image.WritableImage; -import javafx.scene.paint.Color; -import org.example.chaosgame.linalg.Matrix2x2; -import org.example.chaosgame.linalg.Vector2D; -import org.example.chaosgame.transformations.AffineTransform2D; -import org.example.chaosgame.transformations.Transform2D; - -import java.util.List; import java.util.Random; +import org.example.chaosgame.linalg.Vector2D; +/** + * Class for running a chaos game. + * The chaos game is a method for generating fractals. + * The game is played by starting with a point and then randomly + * selecting a transformation from a set of transformations. + * The selected transformation is then applied to the current point. + * The new point is then drawn on the canvas. + * This process is repeated a selected amount of steps. + */ public class ChaosGame { private final ChaosCanvas canvas; private final ChaosGameDescription description; private Vector2D currentPoint = new Vector2D(0.0, 0.0); public final Random random = new Random(); + + /** + * Constructor for ChaosGame. + * + * @param description Description of the chaos game + * + * @param width Width of the canvas + * + * @param height Height of the canvas + */ public ChaosGame(ChaosGameDescription description, int width, int height) { this.description = description; - this.canvas = new ChaosCanvas(width, height, description.getMinCoords(), description.getMaxCoords()); + this.canvas = new ChaosCanvas(width, height, + description.getMinCoords(), description.getMaxCoords()); } @@ -27,23 +38,37 @@ public class ChaosGame { return canvas; } - - public void runSteps (int steps){ - for (int i = 0; i < steps; i++){ + /** + * Method for running the chaos game. Randomly selects a transformation + * from the description and applies it to the current point. + * + * @param steps Number of steps to run + */ + public void runSteps(int steps) { + for (int i = 0; i < steps; i++) { int transformIndex = random.nextInt(description.getTransforms().size()); currentPoint = description.getTransforms().get(transformIndex).transform(currentPoint); canvas.putPixel(currentPoint); } } - public void runStepsBarnsley (int steps){ - for (int i = 0; i < steps; i++){ + + /** + * Method for running the Barnsley chaos game. Randomly selects a transformation + * from the description and applies it to the current point. + * The Barnsley chaos game has a different probability distribution + * for selecting transformations. + * + * @param steps Number of steps to run + */ + public void runStepsBarnsley(int steps) { + for (int i = 0; i < steps; i++) { int test = random.nextInt(100); - if (test < 1){ + if (test < 1) { currentPoint = description.getTransforms().getFirst().transform(currentPoint); - } else if (test < 86){ + } else if (test < 86) { currentPoint = description.getTransforms().get(1).transform(currentPoint); - } else if (test < 93){ + } else if (test < 93) { currentPoint = description.getTransforms().get(2).transform(currentPoint); } else { currentPoint = description.getTransforms().get(3).transform(currentPoint); diff --git a/src/main/java/org/example/chaosgame/chaos/ChaosGameDescription.java b/src/main/java/org/example/chaosgame/chaos/ChaosGameDescription.java index d8ef8fcde27fbbda56ad83c57e843147364eb355..b01e96cbe5ed8a182627a39df5e9af5b8faee57e 100644 --- a/src/main/java/org/example/chaosgame/chaos/ChaosGameDescription.java +++ b/src/main/java/org/example/chaosgame/chaos/ChaosGameDescription.java @@ -1,16 +1,31 @@ package org.example.chaosgame.chaos; +import java.util.List; import org.example.chaosgame.linalg.Vector2D; import org.example.chaosgame.transformations.Transform2D; -import java.util.List; +/** + * This class represents a chaos game description. + * It contains the minimum and maximum coordinates of the game area, + * and a list of transformations to apply to the points. + */ public class ChaosGameDescription { private final Vector2D minCoords; private final Vector2D maxCoords; private final List<Transform2D> transforms; - public ChaosGameDescription(Vector2D minCoords, Vector2D maxCoords, List<Transform2D> transforms) { + /** + * Constructor for ChaosGameDescription. + * + * @param minCoords Minimum coordinates of the game area + * + * @param maxCoords Maximum coordinates of the game area + * + * @param transforms List of transformations to apply to the points + */ + public ChaosGameDescription(Vector2D minCoords, Vector2D maxCoords, + List<Transform2D> transforms) { this.minCoords = minCoords; this.maxCoords = maxCoords; this.transforms = transforms; diff --git a/src/main/java/org/example/chaosgame/linalg/Complex.java b/src/main/java/org/example/chaosgame/linalg/Complex.java index 009d52be552fd00bac73ed3fecbdf98a91874985..5e036b4f78dc945f34eb1e3b74cf62c410a89f31 100644 --- a/src/main/java/org/example/chaosgame/linalg/Complex.java +++ b/src/main/java/org/example/chaosgame/linalg/Complex.java @@ -14,11 +14,11 @@ public class Complex extends Vector2D { * Constructor for Complex class. * super(x, y) is used to call the constructor of the superclass, Vector2D. * - * @param x x-coordinate. - * @param y y-coordinate. + * @param real x-coordinate. + * @param imaginary y-coordinate. */ - public Complex(double x, double y) { - super(x, y); + public Complex(double real, double imaginary) { + super(real, imaginary); } /** diff --git a/src/main/java/org/example/chaosgame/transformations/AffineTransform2D.java b/src/main/java/org/example/chaosgame/transformations/AffineTransform2D.java index b1b46b251b30f900f3f64c882ddcc5ff55d2c2b6..db3a2d699d6dfac7def408215ad382576ac11495 100644 --- a/src/main/java/org/example/chaosgame/transformations/AffineTransform2D.java +++ b/src/main/java/org/example/chaosgame/transformations/AffineTransform2D.java @@ -3,7 +3,13 @@ package org.example.chaosgame.transformations; import org.example.chaosgame.linalg.Matrix2x2; import org.example.chaosgame.linalg.Vector2D; -public class AffineTransform2D implements Transform2D{ +/** + * Represents an affine transformation in 2D space. + * The transformation is represented by a 2x2 matrix and a 2D vector. + * The transformation is applied to a 2D point by first multiplying the point with the matrix + * and then adding the vector. + */ +public class AffineTransform2D implements Transform2D { private final Matrix2x2 matrix; private final Vector2D vector; @@ -12,6 +18,14 @@ public class AffineTransform2D implements Transform2D{ this.vector = vector; } + /** + * Transforms a 2D point using this affine transformation. + * Overridden from the Transform2D interface. + * + * @param point the point to transform + * + * @return the transformed point + */ @Override public Vector2D transform(Vector2D point) { return matrix.multiply(point).add(vector); diff --git a/src/main/java/org/example/chaosgame/transformations/JuliaTransform.java b/src/main/java/org/example/chaosgame/transformations/JuliaTransform.java index f388a1d3f4a0c54290917d1ae47489124c8a396e..4b8f2c065cb4bf89769db0637559262bd44a7a6b 100644 --- a/src/main/java/org/example/chaosgame/transformations/JuliaTransform.java +++ b/src/main/java/org/example/chaosgame/transformations/JuliaTransform.java @@ -3,7 +3,16 @@ package org.example.chaosgame.transformations; import org.example.chaosgame.linalg.Complex; import org.example.chaosgame.linalg.Vector2D; -public class JuliaTransform implements Transform2D{ +/** + * Class for the Julia transformation. + * The transformation is given by the formula: + * <br> + * <span style="font-family: Courier"> + * z → ±√̅z̅ ̅-̅ ̅c + *</span> + * + */ +public class JuliaTransform implements Transform2D { private final Complex point; private final int sign; diff --git a/src/main/java/org/example/chaosgame/transformations/Transform2D.java b/src/main/java/org/example/chaosgame/transformations/Transform2D.java index 5aab73454d2121204a9d2f2ba77633d876968975..461b9dbf5c569007c4cf08cd805495f589097660 100644 --- a/src/main/java/org/example/chaosgame/transformations/Transform2D.java +++ b/src/main/java/org/example/chaosgame/transformations/Transform2D.java @@ -2,7 +2,10 @@ package org.example.chaosgame.transformations; import org.example.chaosgame.linalg.Vector2D; -public interface Transform2D{ +/** + * Interface for 2D transformations. + */ +public interface Transform2D { public Vector2D transform(Vector2D point); }