Skip to content
Snippets Groups Projects
Commit 96a33706 authored by Vetle Solheim Hodne's avatar Vetle Solheim Hodne
Browse files

Merge branch 'javadoc' into 'main'

Add JavaDoc and fix Google Checkstyle warnings

See merge request !8
parents ebfdeddf 0b142d56
No related branches found
No related tags found
1 merge request!8Add JavaDoc and fix Google Checkstyle warnings
Pipeline #265746 passed
......@@ -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,10 +45,12 @@ 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];
......@@ -56,6 +83,12 @@ public class ChaosCanvas {
return canvas[x][y];
}
/**
* 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();
......
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,7 +38,12 @@ public class ChaosGame {
return canvas;
}
/**
* 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());
......@@ -36,6 +52,15 @@ public class ChaosGame {
}
}
/**
* 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);
......
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;
......
......@@ -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);
}
/**
......
......@@ -3,6 +3,12 @@ package org.example.chaosgame.transformations;
import org.example.chaosgame.linalg.Matrix2x2;
import org.example.chaosgame.linalg.Vector2D;
/**
* 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);
......
......@@ -3,6 +3,15 @@ package org.example.chaosgame.transformations;
import org.example.chaosgame.linalg.Complex;
import org.example.chaosgame.linalg.Vector2D;
/**
* Class for the Julia transformation.
* The transformation is given by the formula:
* <br>
* <span style="font-family: Courier">
* z → &#177;&radic;&#x305;z&#x305; &#x305;-&#x305; &#x305;c
*</span>
*
*/
public class JuliaTransform implements Transform2D {
private final Complex point;
private final int sign;
......
......@@ -2,6 +2,9 @@ package org.example.chaosgame.transformations;
import org.example.chaosgame.linalg.Vector2D;
/**
* Interface for 2D transformations.
*/
public interface Transform2D {
public Vector2D transform(Vector2D point);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment