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

Created ChaosCanvasTest and added some getters to ChaosCanvas.

parent 655ca676
No related branches found
No related tags found
No related merge requests found
......@@ -56,15 +56,12 @@ public class ChaosCanvas {
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()))
);*/
// Convert the 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())));
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())));
}
/**
......@@ -96,13 +93,36 @@ public class ChaosCanvas {
}
/**
* Get the canvas array.
* @return the canvas array
* Get the width and height of the canvas in the form of an array where the first
* index stores the width and the second stores the height.
*
* @return an array of two numbers containing both the width and height.
*/
public int[][] getCanvasArray() {
return canvas;
public int[] getSize() {
return new int[]{this.width, this.height};
}
/**
* Get the vector storing the minimum coordinates.
*
* @return the vector.
*/
public Vector2D getMinCoords() {return this.minCoords; }
/**
* Get the vector storing the maximum coordinates.
*
* @return the vector.
*/
public Vector2D getMaxCoords() { return this.maxCoords; }
/**
* Get the affine transform representing the conversion from coordinates to indices.
*
* @return the transform.
*/
public AffineTransform2D getTransformCoordsToIndices() { return this.transformCoordsToIndices; }
/**
* Clear the canvas of all content.
*/
......@@ -114,6 +134,14 @@ public class ChaosCanvas {
}
}
/**
* Get the array defining the size of the canvas.
* @return the array.
*/
public int[][] getCanvas() {
return this.canvas;
}
/**
* Prints the current state of the canvas to the console.
* Populated points are represented by 'x', unpopulated points by ' '.
......
package edu.ntnu.stud.chaosgame.game;
import edu.ntnu.stud.chaosgame.controller.ChaosGameObserver;
import edu.ntnu.stud.chaosgame.model.data.Vector2D;
import edu.ntnu.stud.chaosgame.model.game.ChaosCanvas;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ChaosCanvasTest {
private ChaosCanvas canvas;
/**
* Instantiate a canvas before each test
*/
@BeforeEach
private void setUp() {
Vector2D minCoords = new Vector2D(0, 0);
Vector2D maxCoords = new Vector2D(1, 1);
this.canvas = new ChaosCanvas(100, 100, minCoords, maxCoords);
}
/**
* Test whether creating the canvas worked properly, and simultanteously test whether
* the getters work as expected.
*/
@Test
void shouldCreateCanvas() {
ChaosCanvas testCanvas = new ChaosCanvas(100, 100,
new Vector2D(0,0), new Vector2D(1,1));
Assertions.assertEquals(testCanvas.getSize()[0], 100);
Assertions.assertEquals(testCanvas.getSize()[1], 100);
Assertions.assertEquals(testCanvas.getMinCoords().getX0(), 0);
Assertions.assertEquals(testCanvas.getMinCoords().getX1(), 0);
Assertions.assertEquals(testCanvas.getMaxCoords().getX0(), 1);
Assertions.assertEquals(testCanvas.getMaxCoords().getX1(), 1);
//Assertions.assertEquals(testCanvas.getTransformCoordsToIndices(), );
}
/**
* Test whether an arbitrary point on the canvas is obtained correctly and hence
* equal to 0.
*/
@Test
void shouldGetPixel() {
Assertions.assertEquals(this.canvas.getPixel(new Vector2D(0.5, 0.5)), 0);
}
/**
* Test whether putting a new pixel in an arbitrary location on the canvas works
* as expected.
*/
@Test
void shouldPutPixel() {
Vector2D point = new Vector2D(0.2, 0.3);
this.canvas.putPixel(point);
// Test whether new point was added
Assertions.assertEquals(this.canvas.getPixel(point), 1);
// Ensure another, arbitrary point was not also added
Assertions.assertEquals(this.canvas.getPixel(new Vector2D(0.1, 0.4)), 0);
}
/**
* Test whether clearing the canvas works as expected.
*/
@Test
void shouldClearCanvas() {
// Put pixels throughout a part of the canvas.
for (int i = 1; i < 101; i++) {
this.canvas.putPixel(new Vector2D( 1.0 / i, 1.0 / i));
// Check that the of the points where a pixel was added, are not equal to 0.
Assertions.assertNotEquals(0, this.canvas.getPixel(new Vector2D(1.0 / i, 1.0 / i)));
}
// Clear the canvas
this.canvas.clearCanvas();
// Ensure that each point where a pixel was added, is now equal to 0.
for (int i = 1; i < 101; i++) {
Assertions.assertEquals(this.canvas.getPixel(new Vector2D(1.0 / i, 1.0 / i)), 0);
}
}
}
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