Skip to content
Snippets Groups Projects
Commit 4386eed7 authored by Magnus Eik's avatar Magnus Eik
Browse files

Create ChaosGameDescription and ChaosGameFileHandler

Also add getter methods in transform classes.
parent cd406b91
No related branches found
No related tags found
No related merge requests found
......@@ -6,3 +6,5 @@
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# GitHub Copilot persisted chat sessions
/copilot/chatSessions
package edu.ntnu.stud.chaosgame.game;
public class ChaosCanvas {
}
package edu.ntnu.stud.chaosgame.game;
public class ChaosGame {
}
package edu.ntnu.stud.chaosgame.game;
import edu.ntnu.stud.chaosgame.model.Vector2D;
import edu.ntnu.stud.chaosgame.transformations.Transform2D;
import java.util.List;
public class ChaosGameDescription {
private Vector2D minCoords;
private Vector2D maxCoords;
private List<Transform2D> transforms;
/**
* Constructor for ChaosGameDescription
* @param minCoords Inputs a {@link Vector2D} vector of minimum coordinates for the chaos game.
* @param maxCoords Inputs a {@link Vector2D} vector of maximum coordinates for the chaos game.
* @param transforms Inputs a list of transformations {@link Transform2D} used in the chaos game.
*/
public ChaosGameDescription(Vector2D minCoords, Vector2D maxCoords, List<Transform2D> transforms) {
this.minCoords = minCoords;
this.maxCoords = maxCoords;
this.transforms = transforms;
}
/**
* Getter method for transforms
* @return Returns a list of transforms in the chaos game.
*/
public List<Transform2D> getTransforms(){
return transforms;
}
/**
* Getter method for minimum coordinates.
* @return Returns a Vector2D containing the minimum coordinates.
*/
public Vector2D getMinCoords(){
return minCoords;
}
/**
* Getter method for maximum coordinates.
* @return Returns a Vector2D containing the maximum coordinates.
*/
public Vector2D getMaxCoords(){
return maxCoords;
}
}
package edu.ntnu.stud.chaosgame.game;
import edu.ntnu.stud.chaosgame.model.Complex;
import edu.ntnu.stud.chaosgame.model.Matrix2x2;
import edu.ntnu.stud.chaosgame.model.Vector2D;
import edu.ntnu.stud.chaosgame.transformations.AffineTransform2D;
import edu.ntnu.stud.chaosgame.transformations.JuliaTransform;
import edu.ntnu.stud.chaosgame.transformations.Transform2D;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import java.util.logging.Logger;
import java.io.BufferedWriter;
public class ChaosGameFileHandler {
/**
* Creates a ChaosGameDescription based on the file found at the input path.
* @param path A String input which gives the path to a file describing a chaos game.
* @return A {@link ChaosGameDescription} description of a chaos game.
*/
public ChaosGameDescription readFromFile(String path) throws IOException {
Vector2D minimumVector;
Vector2D maximumVector;
List<Transform2D> transforms = new ArrayList<>();
Path paths = Paths.get(path);
try (Scanner scanner = new Scanner(Files.newInputStream(paths))){
scanner.useLocale(Locale.ENGLISH); // useLocale ensures periods in decimal numbers
scanner.useDelimiter("\\s*,\\s*|\\r?|#[^\\n]"); // useDelimiter with regular expression skips comments
String firstLine = scanner.nextLine().trim();
if (!firstLine.startsWith("Affine2D") && !firstLine.startsWith("Julia")){
throw new IllegalStateException("Invalid Chaos Game");
}
double x0min = scanner.nextDouble();
double x1min = scanner.nextDouble();
minimumVector = new Vector2D(x0min,x1min);
double x0max = scanner.nextDouble();
double x1max = scanner.nextDouble();
maximumVector = new Vector2D(x0max,x1max);
while(scanner.hasNextDouble()){
if (firstLine.startsWith("Affine2D")){
for (int i = 0; i < 3; i++) {
double a00 = scanner.nextDouble();
double a01 = scanner.nextDouble();
double a10 = scanner.nextDouble();
double a11 = scanner.nextDouble();
Matrix2x2 matrix2x2 = new Matrix2x2(a00, a01, a10, a11);
double b0 = scanner.nextDouble();
double b1 = scanner.nextDouble();
Vector2D vector2D = new Vector2D(b0, b1);
Transform2D transform2D = new AffineTransform2D(matrix2x2, vector2D);
transforms.add(transform2D);
}
}else if(firstLine.startsWith("Julia")){
double realOfComplex = scanner.nextDouble();
double imaginaryOfComplex = scanner.nextDouble();
Complex complex = new Complex(realOfComplex,imaginaryOfComplex);
Transform2D juliaTransformPositive = new JuliaTransform(complex,1);
Transform2D juliaTransformNegative = new JuliaTransform(complex,-1);
transforms.add(juliaTransformPositive);
transforms.add(juliaTransformNegative);
}
}
return new ChaosGameDescription(minimumVector,maximumVector,transforms);
}
catch (IOException e){
throw new IOException("Failure to read file",e);
}
}
/**
* Reads from a file
* @param description A {@link ChaosGameDescription} description of the chaos game that should be written to file.
* @param path A String describing the path to the file that should be written to.
*/
public void writeToFile(ChaosGameDescription description, String path){
List<Transform2D> transforms = description.getTransforms();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))){
if (transforms.getFirst() instanceof AffineTransform2D){
writer.write("Affine2D\n");
} else if (transforms.getFirst() instanceof JuliaTransform){
writer.write("Julia\n");
}
Vector2D minVector2D = description.getMinCoords();
Vector2D maxVector2D = description.getMaxCoords();
writer.write(minVector2D.getX0() + "," + minVector2D.getX1());
writer.write(maxVector2D + ", " + minVector2D);
for(Transform2D transform : transforms){
if (transform instanceof AffineTransform2D affineTransform2D) {
Matrix2x2 matrix2x2 = affineTransform2D.getMatrix();
Vector2D vector2D = affineTransform2D.getVector();
writer.write(matrix2x2.getA00() + ", " + matrix2x2.getA01() + ", " + matrix2x2.getA10() + ", "
+ matrix2x2.getA11() + ", " + vector2D.getX0() + ", " + vector2D.getX1());
} else if (transform instanceof JuliaTransform juliaTransform){
Complex complex = juliaTransform.getC1();
writer.write(complex.getX0() + ", " + complex.getX1() + ", ");
}
}
} catch (IOException e) {
// Maybe replace with logger later based on SolarLint
System.out.println("File writing failure on path:" + path + "with error message:" + e.getMessage());
}
}
}
......@@ -49,4 +49,23 @@ public class Matrix2x2 {
return new Vector2D(a00 * v.getX0() + a01 * v.getX1(), a10 * v.getX0() + a11 * v.getX1());
}
/**
*
* @return
*/
public double getA00() {
return a00;
}
public double getA01() {
return a01;
}
public double getA10() {
return a10;
}
public double getA11() {
return a11;
}
}
......@@ -13,11 +13,11 @@ public class AffineTransform2D extends Transform2D {
/**
* The matrix{@link Matrix2x2} which performs the matrix-multiplication part of the affine transformation.
*/
Matrix2x2 matrix;
private Matrix2x2 matrix;
/**
* The vector{@link Vector2D} which is added as part of the affine transformation.
*/
Vector2D vector;
private Vector2D vector;
/**
* Create a type of affine transformation.
......@@ -38,4 +38,14 @@ public class AffineTransform2D extends Transform2D {
return matrix.multiply(point).add(vector);
}
/**
* Getter method to use with {@link edu.ntnu.stud.chaosgame.game.ChaosGameFileHandler}
* @return The matrix for the transformation
*/
public Matrix2x2 getMatrix(){return this.matrix;}
public Vector2D getVector() {
return vector;
}
}
......@@ -44,4 +44,10 @@ public class JuliaTransform extends Transform2D {
return new Complex(temp1.getX0(), temp1.getX1()).sqrt();
}
/**
* Getter method to use with {@link edu.ntnu.stud.chaosgame.game.ChaosGameFileHandler}
* @return The complex number used in the transformation.
*/
public Complex getC1(){return this.c1;}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment