Skip to content
Snippets Groups Projects
Commit 89aa5433 authored by Håvard Daleng's avatar Håvard Daleng
Browse files

Finished CLI-app

parent 5a1a3311
No related branches found
No related tags found
No related merge requests found
......@@ -22,12 +22,22 @@ public class Main {
Vector2D minCoords = new Vector2D(0, 0);
Vector2D maxCoords = new Vector2D(1, 1);
Matrix2x2 transformMatrix = new Matrix2x2(0, 0.5, 0, 0.5);
Vector2D translationVector = new Vector2D(0.5, 0);
AffineTransform2D affineTransform = new AffineTransform2D(transformMatrix, translationVector);
Matrix2x2 transformMatrix1 = new Matrix2x2(0.5, 0, 0, 0.5);
Vector2D translationVector1 = new Vector2D(0, 0);
Vector2D translationVector2 = new Vector2D(0.25, 0.5);
Vector2D translationVector3 = new Vector2D(0.5, 0);
AffineTransform2D affineTransform1 = new AffineTransform2D(transformMatrix1, translationVector1);
AffineTransform2D affineTransform2 = new AffineTransform2D(transformMatrix1, translationVector2);
AffineTransform2D affineTransform3 = new AffineTransform2D(transformMatrix1, translationVector3);
List<Transform2D> transforms = new ArrayList<>();
transforms.add(affineTransform);
transforms.add(affineTransform1);
transforms.add(affineTransform2);
transforms.add(affineTransform3);
ChaosGameDescription description = null; // Declare description
ChaosGame game = null;
......@@ -36,13 +46,14 @@ public class Main {
//ChaosGameDescription description = new ChaosGameFileHandler().readFromFile(path);
description = new ChaosGameDescription(minCoords, maxCoords, transforms);
System.out.println("MaxCoords-object of description: " + description.getMaxCoords());
ChaosCanvas canvas = new ChaosCanvas(100, 100, minCoords, maxCoords); // Create canvas
ChaosCanvas canvas = new ChaosCanvas(100, 100, description.getMinCoords(), description.getMaxCoords()); // Create canvas
game = new ChaosGame(description, canvas);
} catch (Exception e) {
System.out.println(e.getMessage()); // TODO: Alter error handling
}
game.runSteps(100);
game.runSteps(100000);
}
......
......@@ -56,6 +56,7 @@ public class Matrix2x2 {
public double getA00() {
return a00;
}
/**
* Getter method for {@link edu.ntnu.stud.chaosgame.game.ChaosGameDescription}
* @return a matrix component.
......@@ -63,6 +64,7 @@ public class Matrix2x2 {
public double getA01() {
return a01;
}
/**
* Getter method for {@link edu.ntnu.stud.chaosgame.game.ChaosGameDescription}
* @return a matrix component.
......@@ -70,6 +72,7 @@ public class Matrix2x2 {
public double getA10() {
return a10;
}
/**
* Getter method for {@link edu.ntnu.stud.chaosgame.game.ChaosGameDescription}
* @return a matrix component.
......
......@@ -13,17 +13,17 @@ public class ChaosCanvas {
/**
* Table representing the canvas.
*/
private int[][] canvas;
private final int[][] canvas;
/**
* Width of the canvas
*/
private int width;
private final int width;
/**
* Height of the canvas
*/
private int height;
private final int height;
/**
* The minimum coordinates of the canvas.
......@@ -54,14 +54,17 @@ public class ChaosCanvas {
this.minCoords = minCoords;
this.maxCoords = maxCoords;
this.canvas = new int[width][height];
this.canvas = new int[height][width];
// Define the affine transformation that maps coordinates to indices in the canvas.
/*this.transformCoordsToIndices = new AffineTransform2D(
new Matrix2x2(0, (height - 1) / (minCoords.getX1() - maxCoords.getX1()), (width - 1) / (maxCoords.getX0() - minCoords.getX0()), 0),
new Vector2D((((height - 1) * maxCoords.getX1()) / (maxCoords.getX1() - minCoords.getX1())),
((width - 1) * minCoords.getX0()) / (minCoords.getX0() - maxCoords.getX0()))
);*/
this.transformCoordsToIndices = new AffineTransform2D(
new Matrix2x2(0, height - 1, width - 1, 0),
new Vector2D((((height - 1) * maxCoords.getX0()) / maxCoords.getX1()),
((width - 1) * minCoords.getX1()) / maxCoords.getX1())
);
new Matrix2x2(0, (height-1) / (minCoords.getX1() - maxCoords.getX1()), (width-1) / (maxCoords.getX0() - minCoords.getX0()), 0),
new Vector2D(((height-1)* maxCoords.getX1()) / (maxCoords.getX1() - minCoords.getX1()), (width-1)* minCoords.getX0() / (minCoords.getX0() - maxCoords.getX0())));
}
/**
......@@ -71,7 +74,7 @@ public class ChaosCanvas {
* @return the pixel
*/
public int getPixel(Vector2D point) {
return canvas[(int) transformCoordsToIndices.transform(point).getX0()]
return canvas[ (int) transformCoordsToIndices.transform(point).getX0()]
[(int) transformCoordsToIndices.transform(point).getX1()];
}
......@@ -99,4 +102,20 @@ public class ChaosCanvas {
}
}
/**
* Prints the current state of the canvas to the console.
* Populated points are represented by 'x', unpopulated points by ' '.
*/
public void printCanvas() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (canvas[i][j] == 1) {
System.out.print('x');
} else {
System.out.print(' ');
}
}
System.out.println();
}
}
}
......@@ -61,10 +61,11 @@ public class ChaosGame {
public void runSteps(int n) {
for (int i = 0; i < n; i++) {
int j = this.random.nextInt(0, this.numOfTransforms);
j = 0;
this.currentPoint = this.description.getTransforms().get(j).transform(currentPoint);
System.out.println(currentPoint.getX0() + " " + currentPoint.getX1());
this.canvas.putPixel(currentPoint);
}
this.canvas.printCanvas();
}
}
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